diff options
-rw-r--r-- | decode.go | 42 |
1 files changed, 26 insertions, 16 deletions
@@ -427,8 +427,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { return dec.newTypeError(t.JSONType(), reflect.PointerTo(typ), err) } case val.CanAddr() && reflect.PointerTo(typ).Implements(textUnmarshalerType): - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } var buf bytes.Buffer if err := dec.decodeString(reflect.PointerTo(typ), &buf); err != nil { @@ -441,8 +441,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { default: switch kind := typ.Kind(); kind { case reflect.Bool: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } b, err := dec.decodeBool(typ) if err != nil { @@ -450,8 +450,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { } val.SetBool(b) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } var buf strings.Builder if err := dec.scanNumber(typ, &buf); err != nil { @@ -463,8 +463,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { } val.SetInt(n) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } var buf strings.Builder if err := dec.scanNumber(typ, &buf); err != nil { @@ -476,8 +476,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { } val.SetUint(n) case reflect.Float32, reflect.Float64: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } var buf strings.Builder if err := dec.scanNumber(typ, &buf); err != nil { @@ -489,8 +489,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { } val.SetFloat(n) case reflect.String: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } var buf strings.Builder if typ == numberType { @@ -559,8 +559,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { val.Set(reflect.ValueOf(v)) } case reflect.Struct: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } index := jsonstruct.IndexStruct(typ) var nameBuf strings.Builder @@ -780,8 +780,8 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { } } case reflect.Array: - if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { - return dec.decodeNull() + if ok, err := dec.maybeDecodeNull(nullOK); ok { + return err } i := 0 n := val.Len() @@ -1212,3 +1212,13 @@ func (dec *Decoder) decodeNull() *DecodeError { dec.expectRune('l', jsonparse.RuneTypeNullL2) return nil } + +func (dec *Decoder) maybeDecodeNull(nullOK bool) (ok bool, err *DecodeError) { + if nullOK { + t := dec.peekRuneType() + if t == jsonparse.RuneTypeNullN { + return true, dec.decodeNull() + } + } + return false, nil +} |