Skip to content
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: existing entries in map are ignored when unmarshaling #16242

Closed
thatguystone opened this issue Jul 1, 2016 · 3 comments
Closed
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@thatguystone
Copy link

  1. What version of Go are you using (go version)?
    1.6.2

  2. What operating system and processor architecture are you using (go env)?
    Linux 4.6 // amd64

  3. What did you do?
    https://play.golang.org/p/5EmXhFLhOj

  4. 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.

  5. 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}, ...] to map[a: map[i: 2], ...], whereas the struct goes from {A: *Val{I: 1}} to {A: *Val{I: 2}}.

@bradfitz
Copy link
Contributor

bradfitz commented Jul 1, 2016

I imagine we won't be able to do anything more than document this, for compatibility reasons.

@bradfitz bradfitz added this to the Go1.8Maybe milestone Jul 1, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Jul 2, 2016

Somebody left a comment saying that this was a dup of #12972, but I don't see the comment here. Deleted on purpose?

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 10, 2016
@rsc
Copy link
Contributor

rsc commented Oct 20, 2016

This seems pretty clear to me:

To unmarshal a JSON object into a map, Unmarshal first establishes a map to
use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
reuses the existing map, keeping existing entries. Unmarshal then stores
key-value pairs from the JSON object into the map. The map's key type must
either be a string, an integer, or implement encoding.TextUnmarshaler.

Step 1: pick the map to use.
Step 2: replace entries.

@rsc rsc closed this as completed Oct 20, 2016
@golang golang locked and limited conversation to collaborators Oct 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants