Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
encoding/json: unmarshal option to treat omitted fields as null #33854
Json objects have two types of null-like fields, either a null value for
(*1) See #33835 for proper
The behaviour that I want to avoid is having the Golang field skipped over during unmarshalling when the json field is absent. If my Golang field is optional / some nullable type, it doesn't make a huge difference - the field will be skipped, stay at its zero value, which I can define as the same value I'd get unmarshalling
The implementation I prefer is an opt-in
Either way the code would be the same when they're used: after parsing an object into a struct, determine which struct fields were not visited, and unmarshal
The decoder already has a number of options, and there are proposals to add many more. In general, the bar for new API is high, because we have to be careful about what every possible combination of options will do.
Have you thought about ways to accomplish this without new API, or to instead contribute to one of the many existing API proposals?
Sure, it could be implemented as the default behaviour, but then compatibility would break.
I mentioned struct tags above, but would end up writing them on every field.
My three proposals
There is no workaround to achieve the behaviour I'm talking about -