diff options
-rw-r--r-- | decode.go | 38 |
1 files changed, 25 insertions, 13 deletions
@@ -521,7 +521,11 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) { dec.decodeNull() val.Set(reflect.Zero(typ)) default: - val.Set(reflect.ValueOf(dec.decodeAny())) + v, err := dec.decodeAny() + if err != nil { + return err + } + val.Set(reflect.ValueOf(v)) } case reflect.Struct: if nullOK && dec.peekRuneType() == jsonparse.RuneTypeNullN { @@ -797,7 +801,7 @@ func (dec *Decoder) scanNumber(gTyp reflect.Type, out fastio.RuneWriter) { dec.scan(out) } -func (dec *Decoder) decodeAny() any { +func (dec *Decoder) decodeAny() (any, *DecodeError) { c, _ := dec.readRune() dec.unreadRune() switch c { @@ -817,12 +821,16 @@ func (dec *Decoder) decodeAny() any { name := nameBuf.String() dec.structStackPush(typ, name) defer dec.structStackPop() - ret[name] = dec.decodeAny() + v, err := dec.decodeAny() + if err != nil { + return err + } + ret[name] = v return nil }); err != nil { - panic(decodeError(*err)) + return nil, err } - return ret + return ret, nil case '[': ret := []any{} typ := reflect.TypeOf(ret) @@ -831,33 +839,37 @@ func (dec *Decoder) decodeAny() any { defer dec.posStackPop() dec.structStackPush(typ, len(ret)) defer dec.structStackPop() - ret = append(ret, dec.decodeAny()) + v, err := dec.decodeAny() + if err != nil { + return err + } + ret = append(ret, v) return nil }); err != nil { - panic(decodeError(*err)) + return nil, err } - return ret + return ret, nil case '"': var buf strings.Builder dec.decodeString(nil, &buf) - return buf.String() + return buf.String(), nil case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': var buf strings.Builder dec.scan(&buf) num := Number(buf.String()) if dec.useNumber { - return num + return num, nil } f64, err := num.Float64() if err != nil { dec.panicType("number "+buf.String(), float64Type, err) } - return f64 + return f64, nil case 't', 'f': - return dec.decodeBool(nil) + return dec.decodeBool(nil), nil case 'n': dec.decodeNull() - return nil + return nil, nil default: panic("should not happen") } |