encoding/json: unclear documentation for how
json.Unmarshaler interact with
What version of Go are you using (
The text was updated successfully, but these errors were encountered:
You're unmarshaling into a pointer, and as per the docs you quoted, that gets set to nil when unmarshalling
The "by convention" bit is from f444b48. It does seem inconsistent at first sight, but note that it says "to approximate". My guess is that that's the best option we have - if you read the commit message, you'll see why we must always call
If we made the two cases explicitly consistent, then
I'm definitely open to suggestions to make the docs clearer. Though I don't think explaining all this nuance in detail in the godoc is a good idea :) cc @dsnet
Hi @mvdan, thanks for getting back to me so quickly!
I had a look at f444b48 and I think I my confusion stemmed from not having a clear understanding of the different approaches to unmarshaling null into values and pointers.
As I understand it unmarshaling null into a pointer sets that pointer to nil, whereas unmarshaling null into a value leaves the value untouched.
I think this behaviour could be made a little more explicit in the doc for
Also I'm not 100% sure I follow the reasoning for raw message not working if UnmarshalJSON is not called on nulls, could you elaborate a bit?
RawMessage is a slice, so if the regular rules were followed, then unmarshaling
Did you verify that this is the case?
The way you modified the first sentence makes sense to me. Just beware that it's not just about pointers - the regular rules include all nilable type kinds that json knows how to work with: interfaces, maps, pointers, and slices. So it's slightly wrong if you talk about "pointer" and "non-pointer". Perhaps it would be more correct to modify the first sentence to say nilable or "pointer-like". Would like to hear @dsnet's thoughts too.
Hi @mvdan, thank you for clarifying that.
This is the code l used to verify this - https://play.golang.org/p/jD7nQmcKlwL
Thanks for pointing out that it's more than just pointer or non pointer. I actually don't think nillable can be used either, since we can see from the code example that the
Given this, I think the following could be a more correct attempt to make the docs clearer.
The documented rules in
As such, the 4th paragraph says:
While, it is the 11th paragraph that says:
Therefore, the presence of an
I don't think the 11th paragraph should have anything about