diff --git a/decode.go b/decode.go index e4efc48..d092057 100644 --- a/decode.go +++ b/decode.go @@ -18,7 +18,6 @@ func (d Delim) String() string { type decoder interface { decode([]byte, int64, uintptr) (int64, error) decodeStream(*stream, uintptr) error - setDisallowUnknownFields(bool) } type Decoder struct { @@ -99,7 +98,6 @@ func (d *Decoder) decode(src []byte, header *interfaceHeader) error { cachedDecoder.set(typeptr, compiledDec) dec = compiledDec } - dec.setDisallowUnknownFields(d.disallowUnknownFields) if _, err := dec.decode(src, 0, ptr); err != nil { return err } @@ -164,7 +162,6 @@ func (d *Decoder) Decode(v interface{}) error { cachedDecoder.set(typeptr, compiledDec) dec = compiledDec } - dec.setDisallowUnknownFields(d.disallowUnknownFields) if err := d.prepareForDecode(); err != nil { return err } @@ -252,7 +249,7 @@ func (d *Decoder) Token() (Token, error) { // is a struct and the input contains object keys which do not match any // non-ignored, exported fields in the destination. func (d *Decoder) DisallowUnknownFields() { - d.disallowUnknownFields = true + d.s.disallowUnknownFields = true } func (d *Decoder) InputOffset() int64 { diff --git a/decode_array.go b/decode_array.go index 419a406..5a0eac7 100644 --- a/decode_array.go +++ b/decode_array.go @@ -16,10 +16,6 @@ func newArrayDecoder(dec decoder, elemType *rtype, alen int) *arrayDecoder { } } -func (d *arrayDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.valueDecoder.setDisallowUnknownFields(disallowUnknownFields) -} - func (d *arrayDecoder) decodeStream(s *stream, p uintptr) error { for { switch s.char() { diff --git a/decode_bool.go b/decode_bool.go index 644b763..de1edc5 100644 --- a/decode_bool.go +++ b/decode_bool.go @@ -58,8 +58,6 @@ func falseBytes(s *stream) error { return nil } -func (d *boolDecoder) setDisallowUnknownFields(_ bool) {} - func (d *boolDecoder) decodeStream(s *stream, p uintptr) error { s.skipWhiteSpace() for { diff --git a/decode_float.go b/decode_float.go index 5420a50..6da1cd6 100644 --- a/decode_float.go +++ b/decode_float.go @@ -47,8 +47,6 @@ func floatBytes(s *stream) []byte { return s.buf[start:s.cursor] } -func (d *floatDecoder) setDisallowUnknownFields(_ bool) {} - func (d *floatDecoder) decodeStreamByte(s *stream) ([]byte, error) { for { switch s.char() { diff --git a/decode_int.go b/decode_int.go index b4a4ed6..3db1f45 100644 --- a/decode_int.go +++ b/decode_int.go @@ -49,8 +49,6 @@ var ( } ) -func (d *intDecoder) setDisallowUnknownFields(_ bool) {} - func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) { for { switch s.char() { diff --git a/decode_interface.go b/decode_interface.go index 17f4c25..c94234a 100644 --- a/decode_interface.go +++ b/decode_interface.go @@ -6,9 +6,8 @@ import ( ) type interfaceDecoder struct { - typ *rtype - dummy unsafe.Pointer // for escape value - disallowUnknownFields bool + typ *rtype + dummy unsafe.Pointer // for escape value } func newInterfaceDecoder(typ *rtype) *interfaceDecoder { @@ -17,10 +16,6 @@ func newInterfaceDecoder(typ *rtype) *interfaceDecoder { } } -func (d *interfaceDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.disallowUnknownFields = disallowUnknownFields -} - func (d *interfaceDecoder) numDecoder(s *stream) decoder { if s.useNumber { return newNumberDecoder(func(p uintptr, v Number) { @@ -46,13 +41,11 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error { var v map[interface{}]interface{} ptr := unsafe.Pointer(&v) d.dummy = ptr - dec := newMapDecoder( + if err := newMapDecoder( interfaceMapType, newInterfaceDecoder(d.typ), newInterfaceDecoder(d.typ), - ) - dec.setDisallowUnknownFields(d.disallowUnknownFields) - if err := dec.decodeStream(s, uintptr(ptr)); err != nil { + ).decodeStream(s, uintptr(ptr)); err != nil { return err } *(*interface{})(unsafe.Pointer(p)) = v @@ -61,13 +54,11 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error { var v []interface{} ptr := unsafe.Pointer(&v) d.dummy = ptr // escape ptr - dec := newSliceDecoder( + if err := newSliceDecoder( newInterfaceDecoder(d.typ), d.typ, d.typ.Size(), - ) - dec.setDisallowUnknownFields(d.disallowUnknownFields) - if err := dec.decodeStream(s, uintptr(ptr)); err != nil { + ).decodeStream(s, uintptr(ptr)); err != nil { return err } *(*interface{})(unsafe.Pointer(p)) = v @@ -135,7 +126,6 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, e newInterfaceDecoder(d.typ), newInterfaceDecoder(d.typ), ) - dec.setDisallowUnknownFields(d.disallowUnknownFields) cursor, err := dec.decode(buf, cursor, uintptr(ptr)) if err != nil { return 0, err @@ -151,7 +141,6 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, e d.typ, d.typ.Size(), ) - dec.setDisallowUnknownFields(d.disallowUnknownFields) cursor, err := dec.decode(buf, cursor, uintptr(ptr)) if err != nil { return 0, err diff --git a/decode_map.go b/decode_map.go index 8d1bd7d..84f55b7 100644 --- a/decode_map.go +++ b/decode_map.go @@ -26,11 +26,6 @@ func makemap(*rtype, int) unsafe.Pointer //go:noescape func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer) -func (d *mapDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.keyDecoder.setDisallowUnknownFields(disallowUnknownFields) - d.valueDecoder.setDisallowUnknownFields(disallowUnknownFields) -} - func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) { header := (*interfaceHeader)(unsafe.Pointer(&key)) return d.keyDecoder.decode(buf, cursor, uintptr(header.ptr)) diff --git a/decode_number.go b/decode_number.go index 12ccea3..0f1c856 100644 --- a/decode_number.go +++ b/decode_number.go @@ -16,8 +16,6 @@ func newNumberDecoder(op func(uintptr, Number)) *numberDecoder { } } -func (d *numberDecoder) setDisallowUnknownFields(_ bool) {} - func (d *numberDecoder) decodeStream(s *stream, p uintptr) error { bytes, err := d.floatDecoder.decodeStreamByte(s) if err != nil { diff --git a/decode_ptr.go b/decode_ptr.go index 7bbebab..4ce625d 100644 --- a/decode_ptr.go +++ b/decode_ptr.go @@ -16,10 +16,6 @@ func newPtrDecoder(dec decoder, typ *rtype) *ptrDecoder { //go:linkname unsafe_New reflect.unsafe_New func unsafe_New(*rtype) uintptr -func (d *ptrDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.dec.setDisallowUnknownFields(disallowUnknownFields) -} - func (d *ptrDecoder) decodeStream(s *stream, p uintptr) error { newptr := unsafe_New(d.typ) if err := d.dec.decodeStream(s, newptr); err != nil { diff --git a/decode_slice.go b/decode_slice.go index a0bfba3..411350f 100644 --- a/decode_slice.go +++ b/decode_slice.go @@ -56,10 +56,6 @@ func copySlice(elemType *rtype, dst, src reflect.SliceHeader) int //go:linkname newArray reflect.unsafe_NewArray func newArray(*rtype, int) unsafe.Pointer -func (d *sliceDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.valueDecoder.setDisallowUnknownFields(disallowUnknownFields) -} - func (d *sliceDecoder) decodeStream(s *stream, p uintptr) error { for { switch s.char() { diff --git a/decode_stream.go b/decode_stream.go index 8f771d3..ed63bbc 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -10,13 +10,14 @@ const ( ) type stream struct { - buf []byte - length int64 - r io.Reader - offset int64 - cursor int64 - allRead bool - useNumber bool + buf []byte + length int64 + r io.Reader + offset int64 + cursor int64 + allRead bool + useNumber bool + disallowUnknownFields bool } func (s *stream) buffered() io.Reader { diff --git a/decode_string.go b/decode_string.go index 198ae1a..2512271 100644 --- a/decode_string.go +++ b/decode_string.go @@ -11,8 +11,6 @@ func newStringDecoder() *stringDecoder { return &stringDecoder{} } -func (d *stringDecoder) setDisallowUnknownFields(_ bool) {} - func (d *stringDecoder) decodeStream(s *stream, p uintptr) error { bytes, err := d.decodeStreamByte(s) if err != nil { diff --git a/decode_struct.go b/decode_struct.go index dbd7827..5bbdcda 100644 --- a/decode_struct.go +++ b/decode_struct.go @@ -11,9 +11,8 @@ type structFieldSet struct { } type structDecoder struct { - fieldMap map[string]*structFieldSet - keyDecoder *stringDecoder - disallowUnknownFields bool + fieldMap map[string]*structFieldSet + keyDecoder *stringDecoder } func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder { @@ -23,13 +22,6 @@ func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder { } } -func (d *structDecoder) setDisallowUnknownFields(disallowUnknownFields bool) { - d.disallowUnknownFields = disallowUnknownFields - for _, field := range d.fieldMap { - field.dec.setDisallowUnknownFields(disallowUnknownFields) - } -} - func (d *structDecoder) decodeStream(s *stream, p uintptr) error { s.skipWhiteSpace() if s.char() == nul { @@ -65,7 +57,7 @@ func (d *structDecoder) decodeStream(s *stream, p uintptr) error { if err := field.dec.decodeStream(s, p+field.offset); err != nil { return err } - } else if d.disallowUnknownFields { + } else if s.disallowUnknownFields { return fmt.Errorf("json: unknown field %q", k) } else { if err := s.skipValue(); err != nil { @@ -121,8 +113,6 @@ func (d *structDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, erro return 0, err } cursor = c - } else if d.disallowUnknownFields { - return 0, fmt.Errorf("json: unknown field %q", k) } else { c, err := skipValue(buf, cursor) if err != nil { diff --git a/decode_uint.go b/decode_uint.go index a38dc80..5ab2277 100644 --- a/decode_uint.go +++ b/decode_uint.go @@ -24,8 +24,6 @@ func (d *uintDecoder) parseUint(b []byte) uint64 { return sum } -func (d *uintDecoder) setDisallowUnknownFields(_ bool) {} - func (d *uintDecoder) decodeStreamByte(s *stream) ([]byte, error) { for { switch s.char() { diff --git a/decode_unmarshal_json.go b/decode_unmarshal_json.go index ba5a3ed..f84f850 100644 --- a/decode_unmarshal_json.go +++ b/decode_unmarshal_json.go @@ -12,8 +12,6 @@ func newUnmarshalJSONDecoder(typ *rtype) *unmarshalJSONDecoder { return &unmarshalJSONDecoder{typ: typ} } -func (d *unmarshalJSONDecoder) setDisallowUnknownFields(_ bool) {} - func (d *unmarshalJSONDecoder) decodeStream(s *stream, p uintptr) error { s.skipWhiteSpace() start := s.cursor diff --git a/decode_unmarshal_text.go b/decode_unmarshal_text.go index d93e18c..73ea335 100644 --- a/decode_unmarshal_text.go +++ b/decode_unmarshal_text.go @@ -13,8 +13,6 @@ func newUnmarshalTextDecoder(typ *rtype) *unmarshalTextDecoder { return &unmarshalTextDecoder{typ: typ} } -func (d *unmarshalTextDecoder) setDisallowUnknownFields(_ bool) {} - func (d *unmarshalTextDecoder) decodeStream(s *stream, p uintptr) error { s.skipWhiteSpace() start := s.cursor