encoding/json: existing entries in map are ignored when unmarshaling #16242
Labels
Milestone
Comments
I imagine we won't be able to do anything more than document this, for compatibility reasons. |
Somebody left a comment saying that this was a dup of #12972, but I don't see the comment here. Deleted on purpose? |
This seems pretty clear to me:
Step 1: pick the map to use. |
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
)?1.6.2
What operating system and processor architecture are you using (
go env
)?Linux 4.6 // amd64
What did you do?
https://play.golang.org/p/5EmXhFLhOj
What did you expect to see?
According to the docs: "Unmarshal reuses the existing map, keeping existing entries". This sounds like it should attempt to unmarshal into existing values, if there are any.
What did you see instead?
New values were created and replaced existing ones in the map.
The documentation is clear that specific types are stored when unmarshaling into an interface value (eg. "map[string]interface{}, for JSON objects"), but this only happened when unmarshaling into a map. With an equivalent struct, existing interface values were preserved and used.
Concretely, in the output from the example, the map is being transformed from
map[a: *Val{I: 1}, ...]
tomap[a: map[i: 2], ...]
, whereas the struct goes from{A: *Val{I: 1}}
to{A: *Val{I: 2}}
.The text was updated successfully, but these errors were encountered: