encoding/json: existing entries in map are ignored when unmarshaling #16242
Labels
Documentation
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
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: