Json – Golang: JSON: How to unmarshal array of strings into []int64

gojson

Golang encoding/json package lets you use ,string struct tag in order to marshal/unmarshal string values (like "309230") into int64 field. Example:

Int64String int64 `json:",string"`

However, this doesn't work for slices, ie. []int64:

Int64Slice []int64 `json:",string"` // Doesn't work.

Is there any way to marshal/unmarshal JSON string arrays into []int64 field?


Quote from https://golang.org/pkg/encoding/json:

The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs:

Best Answer

For anyone interested, I found a solution using a custom type having MarshalJSON() and UnmarshalJSON() methods defined.

type Int64StringSlice []int64

func (slice Int64StringSlice) MarshalJSON() ([]byte, error) {
    values := make([]string, len(slice))
    for i, value := range []int64(slice) {
        values[i] = fmt.Sprintf(`"%v"`, value)
    }

    return []byte(fmt.Sprintf("[%v]", strings.Join(values, ","))), nil
}

func (slice *Int64StringSlice) UnmarshalJSON(b []byte) error {
    // Try array of strings first.
    var values []string
    err := json.Unmarshal(b, &values)
    if err != nil {
        // Fall back to array of integers:
        var values []int64
        if err := json.Unmarshal(b, &values); err != nil {
            return err
        }
        *slice = values
        return nil
    }
    *slice = make([]int64, len(values))
    for i, value := range values {
        value, err := strconv.ParseInt(value, 10, 64)
        if err != nil {
            return err
        }
        (*slice)[i] = value
    }
    return nil
}

The above solution marshals []int64 into JSON string array. Unmarshaling works from both JSON string and integer arrays, ie.:

{"bars": ["1729382256910270462", "309286902808622", "23"]}

{"bars": [1729382256910270462, 309286902808622, 23]}

See example at https://play.golang.org/p/BOqUBGR3DXm