mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
#48 should return error if concrete tpye unknown
This commit is contained in:
parent
26708bccc9
commit
84ad508437
@ -297,6 +297,10 @@ type nonEmptyInterfaceCodec struct {
|
|||||||
|
|
||||||
func (codec *nonEmptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *nonEmptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
nonEmptyInterface := (*nonEmptyInterface)(ptr)
|
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{}
|
var i interface{}
|
||||||
e := (*emptyInterface)(unsafe.Pointer(&i))
|
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||||
e.typ = nonEmptyInterface.itab.typ
|
e.typ = nonEmptyInterface.itab.typ
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_write_array_of_interface(t *testing.T) {
|
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.Nil(err)
|
||||||
should.Equal(`{"Field":"hello"}`, str)
|
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))
|
||||||
|
}
|
@ -108,8 +108,8 @@ func Test_write_val_string(t *testing.T) {
|
|||||||
func Test_decode_slash(t *testing.T) {
|
func Test_decode_slash(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
var obj interface{}
|
var obj interface{}
|
||||||
should.NotNil(json.Unmarshal([]byte(`"\"`), &obj))
|
should.NotNil(json.Unmarshal([]byte("\\"), &obj))
|
||||||
should.NotNil(UnmarshalFromString(`"\"`, &obj))
|
should.NotNil(UnmarshalFromString("\\", &obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_unicode(b *testing.B) {
|
func Benchmark_jsoniter_unicode(b *testing.B) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user