Forráskód Böngészése

#48 should return error if concrete tpye unknown

Tao Wen 8 éve
szülő
commit
84ad508437
3 módosított fájl, 21 hozzáadás és 2 törlés
  1. 4 0
      feature_reflect_native.go
  2. 15 0
      jsoniter_interface_test.go
  3. 2 2
      jsoniter_string_test.go

+ 4 - 0
feature_reflect_native.go

@@ -297,6 +297,10 @@ type nonEmptyInterfaceCodec struct {
 
 func (codec *nonEmptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
 	nonEmptyInterface := (*nonEmptyInterface)(ptr)
+	if nonEmptyInterface.itab == nil {
+		iter.reportError("read non-empty interface", "do not know which concrete type to decode to")
+		return
+	}
 	var i interface{}
 	e := (*emptyInterface)(unsafe.Pointer(&i))
 	e.typ = nonEmptyInterface.itab.typ

+ 15 - 0
jsoniter_interface_test.go

@@ -4,6 +4,7 @@ import (
 	"github.com/json-iterator/go/require"
 	"testing"
 	"unsafe"
+	"encoding/json"
 )
 
 func Test_write_array_of_interface(t *testing.T) {
@@ -138,3 +139,17 @@ func Test_encode_object_contain_non_empty_interface(t *testing.T) {
 	should.Nil(err)
 	should.Equal(`{"Field":"hello"}`, str)
 }
+
+
+func Test_nil_non_empty_interface(t *testing.T) {
+	CleanEncoders()
+	CleanDecoders()
+	type TestObject struct {
+		Field []MyInterface
+	}
+	should := require.New(t)
+	obj := TestObject{}
+	b := []byte(`{"Field":["AAA"]}`)
+	should.NotNil(json.Unmarshal(b, &obj))
+	should.NotNil(Unmarshal(b, &obj))
+}

+ 2 - 2
jsoniter_string_test.go

@@ -108,8 +108,8 @@ func Test_write_val_string(t *testing.T) {
 func Test_decode_slash(t *testing.T) {
 	should := require.New(t)
 	var obj interface{}
-	should.NotNil(json.Unmarshal([]byte(`"\"`), &obj))
-	should.NotNil(UnmarshalFromString(`"\"`, &obj))
+	should.NotNil(json.Unmarshal([]byte("\\"), &obj))
+	should.NotNil(UnmarshalFromString("\\", &obj))
 }
 
 func Benchmark_jsoniter_unicode(b *testing.B) {