mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: don't match field name if a JSON struct tag is present.
Fixes #3566. R=rsc CC=golang-dev https://golang.org/cl/6139048
This commit is contained in:
parent
dae2992c98
commit
c3c8e35af2
2 changed files with 17 additions and 4 deletions
|
|
@ -504,11 +504,16 @@ func (d *decodeState) object(v reflect.Value) {
|
||||||
}
|
}
|
||||||
// First, tag match
|
// First, tag match
|
||||||
tagName, _ := parseTag(tag)
|
tagName, _ := parseTag(tag)
|
||||||
|
if tagName != "" {
|
||||||
if tagName == key {
|
if tagName == key {
|
||||||
f = sf
|
f = sf
|
||||||
ok = true
|
ok = true
|
||||||
break // no better match possible
|
break // no better match possible
|
||||||
}
|
}
|
||||||
|
// There was a tag, but it didn't match.
|
||||||
|
// Ignore field names.
|
||||||
|
continue
|
||||||
|
}
|
||||||
// Second, exact field name match
|
// Second, exact field name match
|
||||||
if sf.Name == key {
|
if sf.Name == key {
|
||||||
f = sf
|
f = sf
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,10 @@ type T struct {
|
||||||
Z int `json:"-"`
|
Z int `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type U struct {
|
||||||
|
Alphabet string `json:"alpha"`
|
||||||
|
}
|
||||||
|
|
||||||
type tx struct {
|
type tx struct {
|
||||||
x int
|
x int
|
||||||
}
|
}
|
||||||
|
|
@ -72,6 +76,10 @@ var unmarshalTests = []unmarshalTest{
|
||||||
// Z has a "-" tag.
|
// Z has a "-" tag.
|
||||||
{`{"Y": 1, "Z": 2}`, new(T), T{Y: 1}, nil},
|
{`{"Y": 1, "Z": 2}`, new(T), T{Y: 1}, nil},
|
||||||
|
|
||||||
|
{`{"alpha": "abc", "alphabet": "xyz"}`, new(U), U{Alphabet: "abc"}, nil},
|
||||||
|
{`{"alpha": "abc"}`, new(U), U{Alphabet: "abc"}, nil},
|
||||||
|
{`{"alphabet": "xyz"}`, new(U), U{}, nil},
|
||||||
|
|
||||||
// syntax errors
|
// syntax errors
|
||||||
{`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}},
|
{`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}},
|
||||||
{`[1, 2, 3+]`, nil, nil, &SyntaxError{"invalid character '+' after array element", 9}},
|
{`[1, 2, 3+]`, nil, nil, &SyntaxError{"invalid character '+' after array element", 9}},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue