I want to unmarshal
var j = []byte(`[{"major":1},{"minor":0}]`)
into
type Version struct {
Major int `json:"major"`
Minor int `json:"minor"`
}
using custom unmarshaler by looping the inner slice:
func (h *Version) UnmarshalJSON(b []byte) error {
var wrapper []json.RawMessage
err := json.Unmarshal(b, &wrapper)
if err == nil {
for _, v := range wrapper {
if err = json.Unmarshal(v, &h); err != nil {
break
}
}
}
return err
}
The inner UnmarshalJSON
triggers
json: cannot unmarshal object into Go value of type []json.RawMessage
which is strange because the target is a *Version
. What is wrong here? Play: https://play.golang.org/p/Av59IkYTioS