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 · 5 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@robpike
Copy link
Contributor

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 cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 13, 2021
@cagedmantis
Copy link
Contributor

/cc @rsc @mvdan

@mvdan
Copy link
Member

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

@cristaloleg
Copy link

Kindly ping on this. Looks like this will not be available in 1.17 too already.

@mvdan looks like PR is ready, there is 1 question is 1% perf acceptable, am I right?

@mvdan
Copy link
Member

mvdan commented Apr 22, 2021

The PR isn't ready - there are conflicts, and some edge cases that Joe brought up require extra work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants