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: clarify Unmarshal behavior for string keys that implement encoding.TextUnmarshaler #33298
Comments
Currently some make keys are string types that implement the `json.Unmarshaler` interface. Unfortunately, json.Unmarshal will ignore string types' UnmarshalJSON method and simply set the string. (see: golang/go#33298) This PR makes those keys of integer type to ensure the parsing will catch malformed map inputs.
Currently some map keys are string types that implement the `json.Unmarshaler` interface. Unfortunately, json.Unmarshal will ignore string types' UnmarshalJSON method and simply set the string. The same holds for the `encoding.TextUnmarshaler`. (see: golang/go#33298) This PR makes those keys of integer type to ensure the parsing will catch malformed map inputs.
Currently some map keys are string types that implement the `json.Unmarshaler` interface. Unfortunately, json.Unmarshal will ignore string types' UnmarshalJSON method and simply set the string. The same holds for the `encoding.TextUnmarshaler`. (see: golang/go#33298) This PR makes those keys of integer type to ensure the parsing will catch malformed map inputs.
Currently some map keys are string types that implement the `json.Unmarshaler` interface. Unfortunately, json.Unmarshal will ignore string types' UnmarshalJSON method and simply set the string. The same holds for the `encoding.TextUnmarshaler`. (see: golang/go#33298) This PR makes those keys of integer type to ensure the parsing will catch malformed map inputs.
We'd probably take CLs to make the docs clearer, both here and in #28827. |
I sent https://golang.org/cl/188417 to fix #28827 If that seems reasonable, I can send a fix for this one too |
Looking at the code implementing this in I'll send a CL to clarify this behavior in the documentation |
Change https://golang.org/cl/188821 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
https://play.golang.org/p/keu0ix2OtsZ
What did you expect to see?
I expected
json.Unmarshal
to always use theUnmarshalText
method on all key types.The documentation is not very clear about this:
(from https://tip.golang.org/pkg/encoding/json/#Unmarshal)
From that description, I would expect
json.Unmarshal
to useUnmarshalText
on string types that implementencoding.TextUnmarshaler
.Also, I would expect it to call
UnmarshalText
and notUnmarshalJSON
on int keys that implementjson.Unmarshler
.I gather from the related issue #28827 that this cannot be changed for compatibility reasons.
Thus, the documentation should be more explicit what the expected outcome is.
What did you see instead?
When unmarshalling string types that implement the
encoding.TextUnmarshaler
,json.Unmarshal
does not call theUnmarshalText
method.Also,
json.Unmarshal
happily usesUnmarshalJSON
on int keysThe text was updated successfully, but these errors were encountered: