The error's text is close to useless for even a moderately complex JSON object, but at least it's possible to catch this condition now.
The error's text is close to useless for even a moderately complex JSON object, but at least it's possible to catch this condition now.
If you then break the API's backwards compatibility by, say, removing a field, then your API will visibly break, which is correct, something you'd otherwise not detect.
The solution to maintaining backwards compatibility is a combination of explicit versioning, erroring like this, good documentation, and trying to support old features for as long as possible.
Definitely never liked Go's silent ignoring of JSON keys. The workarounds (e.g. overriding the unmarshal code to apply a check) have always been very invasive.
Edit: Maybe you meant in the client, since you wrote "against". If so, I would still argue that strictness is good, as you do want your client to break (or at least warn) if something changes.
That's a different issue. Fields going missing is definitely something I want to cause big problems.
I do mean consumers (e.g. unmarshalling JSON responses from APIs), as the typical mechanism to support backwards compatibility is by adding keys. Otherwise you can't upgrade ever without entirely new API versions, and you force lockstep for deployments. Needing new data is a common thing - maybe you have user endpoint that's not returning <some_flag> that you added that you now want in some new service. Bumping APIs for every added field would get unwieldy fast and you'll have services scattered among many API 'versions'.
It's just like in protobuf. In protobuf (or most other service-to-service serialization libs), adding a new field is not backwards incompatible.
Huh? I don't know the details of what you're (de)serializing, but the standard strategy for default values that are not zero values is to have unpack into a pointer. For example, if you have a message like
{ "foo": 23, "bar": 42 }
and the "bar" field has a default value of 5, you unpack like: var msg struct {
Foo int
Bar *int
}
err := json.Unmarshal(data, &msg)
//handle error
if msg.Bar == nil {
defaultValue := 5
msg.Bar = &defaultValue
}
If you cannot change the type of the message to have a pointer field, you can implement json.Unmarshaler for the receiving type such that UnmarshalJSON deserializes into a temporary type, then copies the values into the recipient: type Message struct {
Foo int
Bar int
}
func (m *Message) UnmarshalJSON(in []byte) error {
var msg struct {
Foo int
Bar *int
}
err := json.Unmarshal(data, &msg)
if err != nil {
return err
}
m.Foo = msg.Foo
m.Bar = 5
if msg.Bar != nil {
m.Bar = *msg.Bar
}
return nil
}
Now you can deserialize into Message (or into a struct containing a Message or *Message or []Message or whatever) and it will fill in the correct default value. type Test struct {
Foo string
Bar string
}
func NewTest() *Test {
return &Test{Foo: "a"}
}
func main() {
t := NewTest()
if err := json.Unmarshal([]byte(`{"Bar": "b"}`), t); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", t)
}
Output: &main.Test{Foo:"a", Bar:"b"}