That is a tuple, and it's perfectly fine to unmarshal tuple into a structure, except that encoding/json doesn't support that.
However we can use the following helper function, which iterates over the fields of the structure and unmarshals them:
// UnmarshalJSONTuple unmarshals JSON list (tuple) into a struct.
func UnmarshalJSONTuple(text []byte, obj interface{}) (err error) {
var list []json.RawMessage
err = json.Unmarshal(text, &list)
if err != nil {
return
}
objValue := reflect.ValueOf(obj).Elem()
if len(list) > objValue.Type().NumField() {
return fmt.Errorf("tuple has too many fields (%v) for %v",
len(list), objValue.Type().Name())
}
for i, elemText := range list {
err = json.Unmarshal(elemText, objValue.Field(i).Addr().Interface())
if err != nil {
return
}
}
return
}
So you only need to provide the UnmarshalJSON method:
func (this *MyType) UnmarshalJSON(text []byte) (err error) {
return UnmarshalJSONTuple(text, this)
}
Here is the complete example: http://play.golang.org/p/QVA-1ynn15