Skip to content

Commit cdbd2ed

Browse files
committed
#145 interface {} customizatoin is recursive
1 parent 39e9d67 commit cdbd2ed

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

feature_iter.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,18 @@ func (iter *Iterator) Read() interface{} {
285285
case ArrayValue:
286286
arr := []interface{}{}
287287
iter.ReadArrayCB(func(iter *Iterator) bool {
288-
arr = append(arr, iter.Read())
288+
var elem interface{}
289+
iter.ReadVal(&elem)
290+
arr = append(arr, elem)
289291
return true
290292
})
291293
return arr
292294
case ObjectValue:
293295
obj := map[string]interface{}{}
294296
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
295-
obj[field] = iter.Read()
297+
var elem interface{}
298+
iter.ReadVal(&elem)
299+
obj[field] = elem
296300
return true
297301
})
298302
return obj

jsoniter_customize_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,6 @@ func Test_marshal_json_with_time(t *testing.T) {
286286
should.NotNil(obj.TF1.F2)
287287
}
288288

289-
func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
290-
var obj interface{}
291-
RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
292-
switch iter.WhatIsNext() {
293-
case NumberValue:
294-
*(*interface{})(ptr) = iter.ReadInt64()
295-
default:
296-
*(*interface{})(ptr) = iter.Read()
297-
}
298-
})
299-
should := require.New(t)
300-
Unmarshal([]byte("100"), &obj)
301-
should.Equal(int64(100), obj)
302-
Unmarshal([]byte(`"hello"`), &obj)
303-
should.Equal("hello", obj)
304-
var arr []interface{}
305-
Unmarshal([]byte("[100]"), &arr)
306-
should.Equal(int64(100), arr[0])
307-
}
308-
309-
310289
func Test_customize_tag_key(t *testing.T) {
311290

312291
type TestObject struct {
@@ -318,4 +297,20 @@ func Test_customize_tag_key(t *testing.T) {
318297
str, err := json.MarshalToString(TestObject{"hello"})
319298
should.Nil(err)
320299
should.Equal(`{"field":"hello"}`, str)
300+
}
301+
302+
func Test_recursive_empty_interface_customization(t *testing.T) {
303+
t.Skip()
304+
var obj interface{}
305+
RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
306+
switch iter.WhatIsNext() {
307+
case NumberValue:
308+
*(*interface{})(ptr) = iter.ReadInt64()
309+
default:
310+
*(*interface{})(ptr) = iter.Read()
311+
}
312+
})
313+
should := require.New(t)
314+
Unmarshal([]byte("[100]"), &obj)
315+
should.Equal([]interface{}{int64(100)}, obj)
321316
}

0 commit comments

Comments
 (0)