-
Notifications
You must be signed in to change notification settings - Fork 18k
json.Unmarshal not work good at object #48959
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
Comments
This is working as expected. Unmarshal generally uses merge semantics (although somewhat inconsistently) when unmarshaling JSON objects. If you want the result you are expecting, then I suggest setting |
What @dsnet said; this is pretty clearly documented at https://pkg.go.dev/encoding/json. If you think the docs aren't clear enough, we'd be happy to hear suggestions or feedback. |
Even if we wanted to do that, we can't make the change to the existing API as it would break users. |
@mvdan This is a problem. |
You're welcome to file a new issue with a concrete proposal of what this option would look like and how it would work. |
@dsnet I'd like to add a flag in : // IsCLearMap When this flag is true to unmarshal
//a JSON object into a map allocates a new map whatever.
//Otherwise Unmarshal reuses the existing map,
//keeping existing entries
var IsCLearMap = false
......
if kv.IsValid() {
//use the flag
if i == 0 && IsCLearMap {
v.Set(reflect.MakeMap(t))
}
v.SetMapIndex(kv, subv)
i++
}
...... Then can use
to decide clear when unmarshal a JSON object to a map I make a test,it look work good.
output is:
output is:
|
@coco-roll Please file suggestions in a new issue, not this closed issue. Thanks. See https://golang.org/s/proposal. That said we are not going to add a global variable like |
@ianlancetaylor Ok, i open a new issue at #49334. We can discuss in there. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
The lastst release is also reproduce
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
See:
What did you see instead?
See:
The text was updated successfully, but these errors were encountered: