Skip to content
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

encoding/json: need to clarify case sensitivity on Unmarshal documentation? #11673

Closed
PragmaticCypher opened this issue Jul 11, 2015 · 6 comments

Comments

@PragmaticCypher
Copy link

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

@PragmaticCypher PragmaticCypher changed the title json.Unmarshall json.Unmarshal Jul 11, 2015
@ianlancetaylor
Copy link
Contributor

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.
In this case, every JSON key matches the single field. Each is applied in turn, and the last one remains.

@PragmaticCypher
Copy link
Author

@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.

@ianlancetaylor
Copy link
Contributor

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?

@mikioh mikioh changed the title json.Unmarshal encoding/json: need to clarify case sensitivity on Unmarshal documentation? Jul 12, 2015
@PragmaticCypher
Copy link
Author

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?

@ianlancetaylor
Copy link
Contributor

Sure--can you suggest some better wording?

@PragmaticCypher
Copy link
Author

"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." ?

@golang golang locked and limited conversation to collaborators Jul 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants