mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
93d92d51dd
commit
6e3f3af4e0
2 changed files with 24 additions and 10 deletions
|
|
@ -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})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue