From 84ad508437a8ecb660dcc77205fe9bc0f591c5b0 Mon Sep 17 00:00:00 2001 From: Tao Wen <taowen@gmail.com> Date: Fri, 9 Jun 2017 17:06:27 +0800 Subject: [PATCH] #48 should return error if concrete tpye unknown --- feature_reflect_native.go | 4 ++++ jsoniter_interface_test.go | 15 +++++++++++++++ jsoniter_string_test.go | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/feature_reflect_native.go b/feature_reflect_native.go index e2c0acf..4578c1a 100644 --- a/feature_reflect_native.go +++ b/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 diff --git a/jsoniter_interface_test.go b/jsoniter_interface_test.go index 98ce470..2fe0b90 100644 --- a/jsoniter_interface_test.go +++ b/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)) +} \ No newline at end of file diff --git a/jsoniter_string_test.go b/jsoniter_string_test.go index 5b574b3..affd535 100644 --- a/jsoniter_string_test.go +++ b/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) {