1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-20 20:54:55 +02:00

#48 should return error if concrete tpye unknown

This commit is contained in:
Tao Wen 2017-06-09 17:06:27 +08:00
parent 26708bccc9
commit 84ad508437
3 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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))
}

View File

@ -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) {