-
Notifications
You must be signed in to change notification settings - Fork 18k
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
proposal: encoding/json: allow merging unmarshalled map value into existing map value #21857
Comments
I thought I'd seen this before, but I can't find another issue in this tracker. Have you tried alternatives, such as merging the maps afterward? I also assume that if we're to make the decoder incremental, it should be consistent across all types - struct fields and slices too, for example. |
I have tried to merge map afterwards, but I encountered the following issues:
enconding/json do not override struct fields and the structure can be used instead of map. |
Because it wasn't clear to me, this is not about reusing a map during Unmarshal, which Unmarshal already does, but instead about some way to merge a specific values contained in the map already under a given key with the new data in the JSON value with the same key. In general this is not a well-formed operation, so it would have to be opt-in. It seems too specialized to be worth the complexity it would incur in package json. (Package json is already quite complex.) If you really need this, my suggestion would be to write a custom merger or to fork the json package and modify your own copy. |
@rsc I do not agree. When I unmarshal structure, the fields already reused. |
In both the struct and the map case, a specific element is being updated. In your example, you are updating the map element for the key |
in case of struct, elements which are not specified in json keeps they original values. |
@bgaifullin, I see your point, and if we were designing json from scratch, we'd probably sit down and think about whether the struct semantics should be changed or the map semantics should be changed or it's OK for them to be subtly different in this way. But we're not designing json from scratch. We have many existing uses that must be taken into account, and I am sure that we cannot change the default behavior here without breaking some existing uses. That leaves us with adding an option that we must maintain, or encouraging people who need this merge semantics to build their own. For now at least we are choosing the latter. If evidence accumulates that merging into existing map values is needed by many users then we will certainly revisit this. |
@rsc I got it. thank you. |
What version of Go are you using (
go version
)?go version go1.8 darwin/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/Cv7cGN6hdv
What did you expect to see?
I expect that structure will not be re-created during decoding and result will be:
"a": &main.T{S0:"a", S1:"c"}
What did you see instead?
I see that json.Decode overrides values in map by new items.
the result is:
"a": &main.T{S0:"", S1:"c"}
The text was updated successfully, but these errors were encountered: