You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I assumed that after Unmarshal returns, the struct would exactly match the provided JSON. I found this quite surprising and I think the documentation should mention that "Fields that are not present in the JSON document will retain their original values."
The existing functionality does give one an elegant way to set default values by having Unmarshal only update those fields which are present in the JSON document. I do not dispute that it is useful. Still, I would like to see the documentation mention this behaviour explicitly.
The text was updated successfully, but these errors were encountered:
The first CL hasn't been updated after the last round of reviews, and I'm tackling a related documentation issue as well, so I've sent a CL of my own.
dsnet
changed the title
encoding/json.Unmarshal: clarify documentation around pre-populated field values
encoding/json: clarify merge semantics of Unmarshal
Dec 31, 2020
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?Go playground
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Go playground
What did you do?
https://play.golang.org/p/QM3FKwmR9h1
What did you expect to see?
What did you see instead?
In short...
The Unmarshal documentation says:
~ https://golang.org/pkg/encoding/json/#Unmarshal
I assumed that after Unmarshal returns, the struct would exactly match the provided JSON. I found this quite surprising and I think the documentation should mention that "Fields that are not present in the JSON document will retain their original values."
The existing functionality does give one an elegant way to set default values by having Unmarshal only update those fields which are present in the JSON document. I do not dispute that it is useful. Still, I would like to see the documentation mention this behaviour explicitly.
The text was updated successfully, but these errors were encountered: