-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: need to clarify case sensitivity on Unmarshal documentation? #11673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this is working as expected. The package prefers an exact match in the sense of matching the JSON key against the field name. That is, the text is describing which field to use for a JSON key. |
@ianlancetaylor I think the documentation should be updated instead as it is misleading. It puts forward two possibilities for how a field is updated, one 'exact' and the other 'case insensitive', but both are in fact case insensitive matches. |
I'm sorry, I don't understand what you mean. The documentation is describing how it unmarshals into a struct. Given a JSON key, which field will be the value be placed in? It first looks for a field whose name (or tag) exactly matches the value. If it doesn't find one, it looks for a field that matches using a case insensitive comparison. It is not the case that both matches are case insensitive. Can you offer a suggestion as to how to improve the documentation? |
Ok I understand now, please disregard my previous comment. I think the confusion arises because it is not clear from the documentation in which domain (incoming object key or struct field/tag) the operation for matching happens in. I had initially thought the order of preference was with regard to JSON tags, not field names. I can see this being a common misinterpration, maybe it should be clarified? |
Sure--can you suggest some better wording? |
"To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring a destination key with an exact match but also accepting a case-insensitive match." ? |
go version go1.4.2 linux/amd64
According to the documentation for json.Unmarshal:
"To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match."
However the library will use the last instance of the field.
http://play.golang.org/p/LtwChO_tp0
The text was updated successfully, but these errors were encountered: