Pārlūkot izejas kodu

#157 number can be null

Tao Wen 8 gadi atpakaļ
vecāks
revīzija
db32ee8c2d

+ 1 - 1
feature_iter_skip_strict.go

@@ -64,7 +64,7 @@ func (iter *Iterator) trySkipString() bool {
 		} else if c == '\\' {
 			return false
 		} else if c < ' ' {
-			iter.ReportError("ReadString",
+			iter.ReportError("trySkipString",
 				fmt.Sprintf(`invalid control character found: %d`, c))
 			return true // already failed
 		}

+ 3 - 3
feature_iter_string.go

@@ -47,7 +47,7 @@ func (iter *Iterator) readStringSlowPath() (ret string) {
 			str = append(str, c)
 		}
 	}
-	iter.ReportError("ReadString", "unexpected end of input")
+	iter.ReportError("readStringSlowPath", "unexpected end of input")
 	return
 }
 
@@ -104,7 +104,7 @@ func (iter *Iterator) readEscapedChar(c byte, str []byte) []byte {
 	case 't':
 		str = append(str, '\t')
 	default:
-		iter.ReportError("ReadString",
+		iter.ReportError("readEscapedChar",
 			`invalid escape char after \`)
 		return nil
 	}
@@ -139,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
 		}
 		return copied
 	}
-	iter.ReportError("ReadString", `expects " or n`)
+	iter.ReportError("ReadStringAsSlice", `expects " or n`)
 	return
 }
 

+ 52 - 0
feature_reflect_native.go

@@ -31,6 +31,10 @@ type intCodec struct {
 }
 
 func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*int)(ptr)) = 0
+		return
+	}
 	*((*int)(ptr)) = iter.ReadInt()
 }
 
@@ -50,6 +54,10 @@ type uintptrCodec struct {
 }
 
 func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uintptr)(ptr)) = 0
+		return
+	}
 	*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
 }
 
@@ -69,6 +77,10 @@ type int8Codec struct {
 }
 
 func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint8)(ptr)) = 0
+		return
+	}
 	*((*int8)(ptr)) = iter.ReadInt8()
 }
 
@@ -88,6 +100,10 @@ type int16Codec struct {
 }
 
 func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*int16)(ptr)) = 0
+		return
+	}
 	*((*int16)(ptr)) = iter.ReadInt16()
 }
 
@@ -107,6 +123,10 @@ type int32Codec struct {
 }
 
 func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*int32)(ptr)) = 0
+		return
+	}
 	*((*int32)(ptr)) = iter.ReadInt32()
 }
 
@@ -126,6 +146,10 @@ type int64Codec struct {
 }
 
 func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*int64)(ptr)) = 0
+		return
+	}
 	*((*int64)(ptr)) = iter.ReadInt64()
 }
 
@@ -145,6 +169,10 @@ type uintCodec struct {
 }
 
 func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint)(ptr)) = 0
+		return
+	}
 	*((*uint)(ptr)) = iter.ReadUint()
 }
 
@@ -164,6 +192,10 @@ type uint8Codec struct {
 }
 
 func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint8)(ptr)) = 0
+		return
+	}
 	*((*uint8)(ptr)) = iter.ReadUint8()
 }
 
@@ -183,6 +215,10 @@ type uint16Codec struct {
 }
 
 func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint16)(ptr)) = 0
+		return
+	}
 	*((*uint16)(ptr)) = iter.ReadUint16()
 }
 
@@ -202,6 +238,10 @@ type uint32Codec struct {
 }
 
 func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint32)(ptr)) = 0
+		return
+	}
 	*((*uint32)(ptr)) = iter.ReadUint32()
 }
 
@@ -221,6 +261,10 @@ type uint64Codec struct {
 }
 
 func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*uint64)(ptr)) = 0
+		return
+	}
 	*((*uint64)(ptr)) = iter.ReadUint64()
 }
 
@@ -240,6 +284,10 @@ type float32Codec struct {
 }
 
 func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*float32)(ptr)) = 0
+		return
+	}
 	*((*float32)(ptr)) = iter.ReadFloat32()
 }
 
@@ -259,6 +307,10 @@ type float64Codec struct {
 }
 
 func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.ReadNil() {
+		*((*float64)(ptr)) = 0
+		return
+	}
 	*((*float64)(ptr)) = iter.ReadFloat64()
 }
 

+ 2 - 2
jsoniter_customize_test.go

@@ -316,7 +316,7 @@ func Test_recursive_empty_interface_customization(t *testing.T) {
 }
 
 type GeoLocation struct {
-	Id             string     `json:"id,omitempty" db:"id"`
+	Id string `json:"id,omitempty" db:"id"`
 }
 
 func (p *GeoLocation) MarshalJSON() ([]byte, error) {
@@ -337,4 +337,4 @@ func Test_marshal_and_unmarshal_on_non_pointer(t *testing.T) {
 	err = Unmarshal([]byte("[1]"), &locations)
 	should.Nil(err)
 	should.Equal("hello", locations[0].Id)
-}
+}

+ 39 - 1
jsoniter_int_test.go

@@ -20,7 +20,45 @@ func Test_read_uint64_invalid(t *testing.T) {
 	should.NotNil(iter.Error)
 }
 
-func Test_read_int8(t *testing.T) {
+func Test_read_int_from_null(t *testing.T) {
+
+	type TestObject struct {
+		F1  int8
+		F2  int16
+		F3  int32
+		F4  int64
+		F5  int
+		F6  uint8
+		F7  uint16
+		F8  uint32
+		F9  uint64
+		F10 uint
+		F11 float32
+		F12 float64
+		F13 uintptr
+	}
+
+	should := require.New(t)
+	obj := TestObject{}
+	err := Unmarshal([]byte(`{
+	"f1":null,
+	"f2":null,
+	"f3":null,
+	"f4":null,
+	"f5":null,
+	"f6":null,
+	"f7":null,
+	"f8":null,
+	"f9":null,
+	"f10":null,
+	"f11":null,
+	"f12":null,
+	"f13":null
+	}`), &obj)
+	should.Nil(err)
+}
+
+func _int8(t *testing.T) {
 	inputs := []string{`127`, `-128`}
 	for _, input := range inputs {
 		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {

+ 1 - 1
jsoniter_invalid_test.go

@@ -1,11 +1,11 @@
 package jsoniter
 
 import (
+	"bytes"
 	"encoding/json"
 	"github.com/stretchr/testify/require"
 	"io"
 	"testing"
-	"bytes"
 )
 
 func Test_missing_object_end(t *testing.T) {