mirror of
https://github.com/json-iterator/go.git
synced 2025-01-23 18:54:21 +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:
|
||||
arr := []interface{}{}
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
arr = append(arr, iter.Read())
|
||||
var elem interface{}
|
||||
iter.ReadVal(&elem)
|
||||
arr = append(arr, elem)
|
||||
return true
|
||||
})
|
||||
return arr
|
||||
case ObjectValue:
|
||||
obj := map[string]interface{}{}
|
||||
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
|
||||
obj[field] = iter.Read()
|
||||
var elem interface{}
|
||||
iter.ReadVal(&elem)
|
||||
obj[field] = elem
|
||||
return true
|
||||
})
|
||||
return obj
|
||||
|
@ -286,27 +286,6 @@ func Test_marshal_json_with_time(t *testing.T) {
|
||||
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) {
|
||||
|
||||
type TestObject struct {
|
||||
@ -318,4 +297,20 @@ func Test_customize_tag_key(t *testing.T) {
|
||||
str, err := json.MarshalToString(TestObject{"hello"})
|
||||
should.Nil(err)
|
||||
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