1
0
mirror of https://github.com/json-iterator/go.git synced 2025-07-03 23:30:41 +02:00

test []interface{}

This commit is contained in:
Tao Wen
2017-08-21 22:43:51 +08:00
parent 9c358632dc
commit ac3b3cd160
2 changed files with 37 additions and 0 deletions

View File

@ -7,6 +7,8 @@ import (
"testing"
"time"
"unsafe"
"fmt"
"reflect"
)
func Test_customize_type_decoder(t *testing.T) {
@ -285,3 +287,23 @@ func Test_marshal_json_with_time(t *testing.T) {
should.Nil(Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
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])
}