diff --git a/decode.go b/decode.go index 01af489..6adc722 100644 --- a/decode.go +++ b/decode.go @@ -217,6 +217,7 @@ type decodeState struct { savedError error useNumber bool disallowUnknownFields bool + strict bool } // readIndex returns the position of the last byte read. @@ -703,7 +704,7 @@ func (d *decodeState) object(v reflect.Value) error { if i, ok := fields.nameIndex[string(key)]; ok { // Found an exact name match. f = &fields.list[i] - } else { + } else if !d.strict { // Fall back to the expensive case-insensitive // linear search. for i := range fields.list { diff --git a/stream.go b/stream.go index b278ee4..edc5bc1 100644 --- a/stream.go +++ b/stream.go @@ -36,6 +36,9 @@ func NewDecoder(r io.Reader) *Decoder { // Number instead of as a float64. func (dec *Decoder) UseNumber() { dec.d.useNumber = true } +// UseStrictNames causes the Decoder to unmarshal only to exact field names. +func (dec *Decoder) UseStrictNames() { dec.d.strict = true } + // DisallowUnknownFields causes the Decoder to return an error when the destination // is a struct and the input contains object keys which do not match any // non-ignored, exported fields in the destination.