1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-30 08:36:43 +02:00
json-iterator/jsoniter_interface_test.go

155 lines
3.7 KiB
Go
Raw Normal View History

2017-01-09 15:00:01 +02:00
package jsoniter
import (
"github.com/json-iterator/go/require"
2017-06-06 17:27:00 +02:00
"testing"
2017-01-25 17:25:25 +02:00
"unsafe"
"encoding/json"
2017-01-09 15:00:01 +02:00
)
func Test_write_array_of_interface(t *testing.T) {
should := require.New(t)
array := []interface{}{"hello"}
str, err := MarshalToString(array)
should.Nil(err)
should.Equal(`["hello"]`, str)
2017-01-25 16:43:57 +02:00
}
func Test_write_map_of_interface(t *testing.T) {
should := require.New(t)
2017-06-06 17:27:00 +02:00
val := map[string]interface{}{"hello": "world"}
2017-01-25 16:43:57 +02:00
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"hello":"world"}`, str)
}
2017-02-03 12:44:54 +02:00
func Test_write_map_of_interface_in_struct(t *testing.T) {
type TestObject struct {
Field map[string]interface{}
}
should := require.New(t)
2017-06-06 17:27:00 +02:00
val := TestObject{map[string]interface{}{"hello": "world"}}
2017-02-03 12:44:54 +02:00
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"Field":{"hello":"world"}}`, str)
}
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
type TestObject struct {
2017-06-06 17:27:00 +02:00
Field map[string]interface{}
2017-02-03 12:44:54 +02:00
Field2 string
}
should := require.New(t)
2017-06-06 17:27:00 +02:00
val := TestObject{map[string]interface{}{"hello": "world"}, ""}
2017-02-03 12:44:54 +02:00
str, err := MarshalToString(val)
should.Nil(err)
2017-06-02 11:34:40 +02:00
should.Contains(str, `"Field":{"hello":"world"}`)
2017-02-03 12:44:54 +02:00
}
2017-01-25 16:43:57 +02:00
type MyInterface interface {
2017-01-25 17:25:25 +02:00
Hello() string
}
type MyString string
func (ms MyString) Hello() string {
return string(ms)
2017-01-25 16:43:57 +02:00
}
func Test_write_map_of_custom_interface(t *testing.T) {
should := require.New(t)
2017-01-25 17:25:25 +02:00
myStr := MyString("world")
should.Equal("world", myStr.Hello())
2017-06-06 17:27:00 +02:00
val := map[string]MyInterface{"hello": myStr}
2017-01-25 16:43:57 +02:00
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"hello":"world"}`, str)
}
func Test_write_interface(t *testing.T) {
should := require.New(t)
var val interface{}
val = "hello"
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`"hello"`, str)
2017-01-25 17:25:25 +02:00
}
func Test_read_interface(t *testing.T) {
should := require.New(t)
var val interface{}
err := UnmarshalFromString(`"hello"`, &val)
should.Nil(err)
should.Equal("hello", val)
}
func Test_read_custom_interface(t *testing.T) {
should := require.New(t)
var val MyInterface
RegisterTypeDecoder("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
})
err := UnmarshalFromString(`"hello"`, &val)
should.Nil(err)
should.Equal("hello", val.Hello())
2017-05-26 18:36:21 +02:00
}
func Test_decode_object_contain_empty_interface(t *testing.T) {
type TestObject struct {
Field interface{}
}
should := require.New(t)
obj := TestObject{}
obj.Field = 1024
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
should.Equal("hello", obj.Field)
}
func Test_decode_object_contain_non_empty_interface(t *testing.T) {
type TestObject struct {
Field MyInterface
}
should := require.New(t)
obj := TestObject{}
obj.Field = MyString("abc")
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
should.Equal(MyString("hello"), obj.Field)
}
func Test_encode_object_contain_empty_interface(t *testing.T) {
type TestObject struct {
Field interface{}
}
should := require.New(t)
obj := TestObject{}
obj.Field = 1024
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Field":1024}`, str)
}
func Test_encode_object_contain_non_empty_interface(t *testing.T) {
type TestObject struct {
Field MyInterface
}
should := require.New(t)
obj := TestObject{}
obj.Field = MyString("hello")
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Field":"hello"}`, str)
2017-06-06 17:27:00 +02:00
}
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))
}