encoding/json: ignore unexported fields in Unmarshal

Go 1.0 behavior was to create an UnmarshalFieldError when a json value name matched an unexported field name. This error will no longer be created and the field will be skipped instead.

Fixes #4660.

R=adg, rsc, bradfitz
CC=golang-dev
https://golang.org/cl/7139049
This commit is contained in:
Rick Arnold 2013-01-22 17:49:07 -05:00 committed by Russ Cox
parent 93d92d51dd
commit 6e3f3af4e0
2 changed files with 24 additions and 10 deletions

View file

@ -106,6 +106,7 @@ func (e *UnmarshalTypeError) Error() string {
// An UnmarshalFieldError describes a JSON object key that
// led to an unexported (and therefore unwritable) struct field.
// (No longer used; kept for compatibility.)
type UnmarshalFieldError struct {
Key string
Type reflect.Type
@ -530,15 +531,6 @@ func (d *decodeState) object(v reflect.Value) {
}
subv = subv.Field(i)
}
} else {
// To give a good error, a quick scan for unexported fields in top level.
st := v.Type()
for i := 0; i < st.NumField(); i++ {
f := st.Field(i)
if f.PkgPath != "" && strings.EqualFold(f.Name, key) {
d.saveError(&UnmarshalFieldError{key, st, f})
}
}
}
}