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: clarify what happens when multiple items match the same field #43664

Open
robpike opened this issue Jan 13, 2021 · 3 comments
Open
Milestone

Comments

@robpike
Copy link
Contributor

@robpike robpike commented Jan 13, 2021

This test I added to encoding/json

func TestUnmarshalCase(t *testing.T) {
	var v struct {
		Dist string `json:"dist"`
	}

	Unmarshal([]byte(`{"dist": "ok", "Dist": "KO"}`), &v)
	t.Logf("%+v", v)

	Unmarshal([]byte(`{"Dist": "ok", "dist": "KO"}`), &v)
	t.Logf("%+v", v)
}

prints

    x_test.go:13: {Dist:KO}
    x_test.go:16: {Dist:KO}

The comment says Unmarshal "prefers" exact matches, but if there are no words about what happens if multiple fields match.

It's clearly an odd case, but clarity would be nice. I expected the exact match to win always.

@cagedmantis cagedmantis added this to the Backlog milestone Jan 13, 2021
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Jan 13, 2021

/cc @rsc @mvdan

@mvdan
Copy link
Member

@mvdan mvdan commented Jan 14, 2021

I'm well aware of this problem, unfortunately. See #14750, which this issue is likely a duplicate of. My attempt at fixing this without incurring a performance or memory cost is https://go-review.googlesource.com/c/go/+/224079, which has been stuck in review limbo for a little while :)

@dsnet
Copy link
Member

@dsnet dsnet commented Jan 23, 2021

At the core of this problem is whether we want to favor a streaming decode or whether we want to buffer all the members of a JSON object in order to perform an exact match. The former has significantly better performance, while the latter probably matches user expectation better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants