mirror of
https://github.com/json-iterator/go.git
synced 2025-05-13 21:36:29 +02:00
#145 interface {} customizatoin is recursive
This commit is contained in:
parent
39e9d67807
commit
cdbd2ed810
@ -285,14 +285,18 @@ func (iter *Iterator) Read() interface{} {
|
|||||||
case ArrayValue:
|
case ArrayValue:
|
||||||
arr := []interface{}{}
|
arr := []interface{}{}
|
||||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
arr = append(arr, iter.Read())
|
var elem interface{}
|
||||||
|
iter.ReadVal(&elem)
|
||||||
|
arr = append(arr, elem)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return arr
|
return arr
|
||||||
case ObjectValue:
|
case ObjectValue:
|
||||||
obj := map[string]interface{}{}
|
obj := map[string]interface{}{}
|
||||||
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
|
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
|
||||||
obj[field] = iter.Read()
|
var elem interface{}
|
||||||
|
iter.ReadVal(&elem)
|
||||||
|
obj[field] = elem
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return obj
|
return obj
|
||||||
|
@ -286,27 +286,6 @@ func Test_marshal_json_with_time(t *testing.T) {
|
|||||||
should.NotNil(obj.TF1.F2)
|
should.NotNil(obj.TF1.F2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
|
|
||||||
var obj interface{}
|
|
||||||
RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
switch iter.WhatIsNext() {
|
|
||||||
case NumberValue:
|
|
||||||
*(*interface{})(ptr) = iter.ReadInt64()
|
|
||||||
default:
|
|
||||||
*(*interface{})(ptr) = iter.Read()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
should := require.New(t)
|
|
||||||
Unmarshal([]byte("100"), &obj)
|
|
||||||
should.Equal(int64(100), obj)
|
|
||||||
Unmarshal([]byte(`"hello"`), &obj)
|
|
||||||
should.Equal("hello", obj)
|
|
||||||
var arr []interface{}
|
|
||||||
Unmarshal([]byte("[100]"), &arr)
|
|
||||||
should.Equal(int64(100), arr[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func Test_customize_tag_key(t *testing.T) {
|
func Test_customize_tag_key(t *testing.T) {
|
||||||
|
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
@ -319,3 +298,19 @@ func Test_customize_tag_key(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"field":"hello"}`, str)
|
should.Equal(`{"field":"hello"}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_recursive_empty_interface_customization(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
|
var obj interface{}
|
||||||
|
RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
switch iter.WhatIsNext() {
|
||||||
|
case NumberValue:
|
||||||
|
*(*interface{})(ptr) = iter.ReadInt64()
|
||||||
|
default:
|
||||||
|
*(*interface{})(ptr) = iter.Read()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
should := require.New(t)
|
||||||
|
Unmarshal([]byte("[100]"), &obj)
|
||||||
|
should.Equal([]interface{}{int64(100)}, obj)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user