mirror of https://github.com/goccy/go-json.git
Fix UnmarshalTypeError for int decoder
This commit is contained in:
parent
85577616f8
commit
d88eb0986e
|
@ -44,15 +44,15 @@ func (d *Decoder) compile(typ *rtype, structName, fieldName string) (decoder, er
|
||||||
case reflect.Uintptr:
|
case reflect.Uintptr:
|
||||||
return d.compileUint(structName, fieldName)
|
return d.compileUint(structName, fieldName)
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
return d.compileInt(structName, fieldName)
|
return d.compileInt(typ, structName, fieldName)
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
return d.compileInt8(structName, fieldName)
|
return d.compileInt8(typ, structName, fieldName)
|
||||||
case reflect.Int16:
|
case reflect.Int16:
|
||||||
return d.compileInt16(structName, fieldName)
|
return d.compileInt16(typ, structName, fieldName)
|
||||||
case reflect.Int32:
|
case reflect.Int32:
|
||||||
return d.compileInt32(structName, fieldName)
|
return d.compileInt32(typ, structName, fieldName)
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
return d.compileInt64(structName, fieldName)
|
return d.compileInt64(typ, structName, fieldName)
|
||||||
case reflect.Uint:
|
case reflect.Uint:
|
||||||
return d.compileUint(structName, fieldName)
|
return d.compileUint(structName, fieldName)
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
|
@ -104,32 +104,32 @@ func (d *Decoder) compilePtr(typ *rtype, structName, fieldName string) (decoder,
|
||||||
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
|
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) compileInt(structName, fieldName string) (decoder, error) {
|
func (d *Decoder) compileInt(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||||
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) {
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
||||||
*(*int)(p) = int(v)
|
*(*int)(p) = int(v)
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) compileInt8(structName, fieldName string) (decoder, error) {
|
func (d *Decoder) compileInt8(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||||
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) {
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
||||||
*(*int8)(p) = int8(v)
|
*(*int8)(p) = int8(v)
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) compileInt16(structName, fieldName string) (decoder, error) {
|
func (d *Decoder) compileInt16(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||||
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) {
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
||||||
*(*int16)(p) = int16(v)
|
*(*int16)(p) = int16(v)
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) compileInt32(structName, fieldName string) (decoder, error) {
|
func (d *Decoder) compileInt32(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||||
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) {
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
||||||
*(*int32)(p) = int32(v)
|
*(*int32)(p) = int32(v)
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) compileInt64(structName, fieldName string) (decoder, error) {
|
func (d *Decoder) compileInt64(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||||
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) {
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
||||||
*(*int64)(p) = v
|
*(*int64)(p) = v
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,42 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type intDecoder struct {
|
type intDecoder struct {
|
||||||
|
typ *rtype
|
||||||
op func(unsafe.Pointer, int64)
|
op func(unsafe.Pointer, int64)
|
||||||
structName string
|
structName string
|
||||||
fieldName string
|
fieldName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIntDecoder(structName, fieldName string, op func(unsafe.Pointer, int64)) *intDecoder {
|
func newIntDecoder(typ *rtype, structName, fieldName string, op func(unsafe.Pointer, int64)) *intDecoder {
|
||||||
return &intDecoder{op: op, structName: structName, fieldName: fieldName}
|
return &intDecoder{
|
||||||
|
typ: typ,
|
||||||
|
op: op,
|
||||||
|
structName: structName,
|
||||||
|
fieldName: fieldName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *intDecoder) typeError(buf []byte, offset int64) *UnmarshalTypeError {
|
||||||
|
return &UnmarshalTypeError{
|
||||||
|
Value: fmt.Sprintf("number %s", string(buf)),
|
||||||
|
Type: rtype2type(d.typ),
|
||||||
|
Offset: offset,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *intDecoder) annotateError(cursor int64, err error) {
|
||||||
|
switch e := err.(type) {
|
||||||
|
case *UnmarshalTypeError:
|
||||||
|
e.Struct = d.structName
|
||||||
|
e.Field = d.fieldName
|
||||||
|
case *SyntaxError:
|
||||||
|
e.Offset = cursor
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -102,7 +127,7 @@ func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) {
|
||||||
}
|
}
|
||||||
goto ERROR
|
goto ERROR
|
||||||
default:
|
default:
|
||||||
goto ERROR
|
return nil, d.typeError([]byte{s.char()}, s.totalOffset())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ERROR:
|
ERROR:
|
||||||
|
@ -126,7 +151,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
|
||||||
num := buf[start:cursor]
|
num := buf[start:cursor]
|
||||||
return num, cursor, nil
|
return num, cursor, nil
|
||||||
default:
|
default:
|
||||||
return nil, 0, errInvalidCharacter(buf[cursor], "number(integer)", cursor)
|
return nil, 0, d.typeError([]byte{buf[cursor]}, cursor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor)
|
return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
|
|
||||||
type unmarshalJSONDecoder struct {
|
type unmarshalJSONDecoder struct {
|
||||||
typ *rtype
|
typ *rtype
|
||||||
isDoublePointer bool
|
|
||||||
structName string
|
structName string
|
||||||
fieldName string
|
fieldName string
|
||||||
}
|
}
|
||||||
|
@ -58,18 +57,6 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
src := buf[start:end]
|
src := buf[start:end]
|
||||||
if d.isDoublePointer {
|
|
||||||
newptr := unsafe_New(d.typ.Elem())
|
|
||||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
|
|
||||||
typ: d.typ,
|
|
||||||
ptr: newptr,
|
|
||||||
}))
|
|
||||||
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
|
|
||||||
d.annotateError(cursor, err)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
*(*unsafe.Pointer)(p) = newptr
|
|
||||||
} else {
|
|
||||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
|
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
|
||||||
typ: d.typ,
|
typ: d.typ,
|
||||||
ptr: p,
|
ptr: p,
|
||||||
|
@ -78,6 +65,5 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
|
||||||
d.annotateError(cursor, err)
|
d.annotateError(cursor, err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return end, nil
|
return end, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,6 +149,8 @@ func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) {
|
||||||
return e.compileUint32String(ctx)
|
return e.compileUint32String(ctx)
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
return e.compileUint64String(ctx)
|
return e.compileUint64String(ctx)
|
||||||
|
case reflect.Uintptr:
|
||||||
|
return e.compileUintString(ctx)
|
||||||
}
|
}
|
||||||
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
|
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue