encoding/json: clarify Unmarshal behavior for string keys that implement encoding.TextUnmarshaler #33298
Milestone
Comments
Oncilla
added a commit
to Oncilla/scion
that referenced
this issue
Jul 29, 2019
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.
Oncilla
added a commit
to Oncilla/scion
that referenced
this issue
Jul 29, 2019
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.
Oncilla
added a commit
to Oncilla/scion
that referenced
this issue
Jul 30, 2019
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.
Oncilla
added a commit
to scionproto/scion
that referenced
this issue
Jul 30, 2019
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: |
benthor
pushed a commit
to benthor/scion
that referenced
this issue
Jul 29, 2020
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.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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: