Browse Source

fix #217 when input is null, non-decodable type should not be considered as error, to be compatible with stdlib

Tao Wen 8 năm trước cách đây
mục cha
commit
11c1cce0d8

+ 4 - 0
feature_reflect_native.go

@@ -382,6 +382,10 @@ type nonEmptyInterfaceCodec struct {
 }
 }
 
 
 func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
 func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
+	if iter.WhatIsNext() == NilValue {
+		iter.skipFourBytes('n', 'u', 'l', 'l')
+		return
+	}
 	nonEmptyInterface := (*nonEmptyInterface)(ptr)
 	nonEmptyInterface := (*nonEmptyInterface)(ptr)
 	if nonEmptyInterface.itab == nil {
 	if nonEmptyInterface.itab == nil {
 		iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
 		iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")

+ 7 - 0
jsoniter_invalid_test.go

@@ -191,3 +191,10 @@ func TestEOF(t *testing.T) {
 	err := ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
 	err := ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
 	assert.Equal(t, io.EOF, err)
 	assert.Equal(t, io.EOF, err)
 }
 }
+
+func TestDecodeErrorType(t *testing.T) {
+	should := require.New(t)
+	var err error
+	should.Nil(Unmarshal([]byte("null"), &err))
+	should.NotNil(Unmarshal([]byte("123"), &err))
+}