mirror of
https://github.com/json-iterator/go.git
synced 2025-01-23 18:54:21 +02:00
support integer types
This commit is contained in:
parent
38e256e526
commit
fafa785306
70
jsoniter.go
70
jsoniter.go
@ -95,6 +95,46 @@ const cutoffUint64 = maxUint64 / 10 + 1
|
||||
const maxUint32 = (1 << 32 - 1)
|
||||
const cutoffUint32 = maxUint32 / 10 + 1
|
||||
|
||||
func (iter *Iterator) ReadUint() (ret uint) {
|
||||
val := iter.ReadUint64()
|
||||
converted := uint(val)
|
||||
if uint64(converted) != val {
|
||||
iter.ReportError("ReadUint", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint8() (ret uint8) {
|
||||
val := iter.ReadUint64()
|
||||
converted := uint8(val)
|
||||
if uint64(converted) != val {
|
||||
iter.ReportError("ReadUint8", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint16() (ret uint16) {
|
||||
val := iter.ReadUint64()
|
||||
converted := uint16(val)
|
||||
if uint64(converted) != val {
|
||||
iter.ReportError("ReadUint16", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint32() (ret uint32) {
|
||||
val := iter.ReadUint64()
|
||||
converted := uint32(val)
|
||||
if uint64(converted) != val {
|
||||
iter.ReportError("ReadUint32", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint64() (ret uint64) {
|
||||
c := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
@ -142,6 +182,36 @@ func (iter *Iterator) ReadInt() (ret int) {
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadInt8() (ret int8) {
|
||||
val := iter.ReadInt64()
|
||||
converted := int8(val)
|
||||
if int64(converted) != val {
|
||||
iter.ReportError("ReadInt8", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadInt16() (ret int16) {
|
||||
val := iter.ReadInt64()
|
||||
converted := int16(val)
|
||||
if int64(converted) != val {
|
||||
iter.ReportError("ReadInt16", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadInt32() (ret int32) {
|
||||
val := iter.ReadInt64()
|
||||
converted := int32(val)
|
||||
if int64(converted) != val {
|
||||
iter.ReportError("ReadInt32", "int overflow")
|
||||
return
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadInt64() (ret int64) {
|
||||
c := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
|
@ -26,6 +26,69 @@ func (decoder *intDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int)(ptr)) = iter.ReadInt()
|
||||
}
|
||||
|
||||
type int8Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *int8Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int8)(ptr)) = iter.ReadInt8()
|
||||
}
|
||||
|
||||
type int16Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *int16Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int16)(ptr)) = iter.ReadInt16()
|
||||
}
|
||||
|
||||
type int32Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *int32Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int32)(ptr)) = iter.ReadInt32()
|
||||
}
|
||||
|
||||
type int64Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *int64Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int64)(ptr)) = iter.ReadInt64()
|
||||
}
|
||||
|
||||
type uintDecoder struct {
|
||||
}
|
||||
|
||||
func (decoder *uintDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint)(ptr)) = iter.ReadUint()
|
||||
}
|
||||
|
||||
type uint8Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *uint8Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||
}
|
||||
|
||||
type uint16Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *uint16Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||
}
|
||||
|
||||
type uint32Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *uint32Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||
}
|
||||
|
||||
type uint64Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *uint64Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||
}
|
||||
|
||||
type optionalDecoder struct {
|
||||
valueType reflect.Type
|
||||
valueDecoder Decoder
|
||||
@ -195,6 +258,24 @@ func decoderOfPtr(type_ reflect.Type) (Decoder, error) {
|
||||
return &stringDecoder{}, nil
|
||||
case reflect.Int:
|
||||
return &intDecoder{}, nil
|
||||
case reflect.Int8:
|
||||
return &int8Decoder{}, nil
|
||||
case reflect.Int16:
|
||||
return &int16Decoder{}, nil
|
||||
case reflect.Int32:
|
||||
return &int32Decoder{}, nil
|
||||
case reflect.Int64:
|
||||
return &int64Decoder{}, nil
|
||||
case reflect.Uint:
|
||||
return &uintDecoder{}, nil
|
||||
case reflect.Uint8:
|
||||
return &uint8Decoder{}, nil
|
||||
case reflect.Uint16:
|
||||
return &uint16Decoder{}, nil
|
||||
case reflect.Uint32:
|
||||
return &uint32Decoder{}, nil
|
||||
case reflect.Uint64:
|
||||
return &uint64Decoder{}, nil
|
||||
case reflect.Struct:
|
||||
return decoderOfStruct(type_)
|
||||
case reflect.Slice:
|
||||
|
@ -34,6 +34,96 @@ func Test_reflect_int(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_int8(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := int8(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_int16(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := int16(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_int32(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := int32(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_int64(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := int64(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_uint(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := uint(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_uint8(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := uint8(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_uint16(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := uint16(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_uint32(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := uint32(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_uint64(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := uint64(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reflect_byte(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
val := byte(0)
|
||||
iter.Read(&val)
|
||||
if val != 123 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
type StructOfString struct {
|
||||
field1 string
|
||||
field2 string
|
||||
|
Loading…
x
Reference in New Issue
Block a user