1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-23 21:09:11 +02:00
json-iterator/jsoniter_reflect_struct_test.go

242 lines
6.0 KiB
Go
Raw Normal View History

2017-01-07 07:49:50 +08:00
package jsoniter
import (
2017-03-07 18:36:58 -08:00
"bytes"
2017-06-06 23:27:00 +08:00
"github.com/json-iterator/go/require"
"testing"
2017-01-07 07:49:50 +08:00
)
func Test_decode_one_field_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
2017-01-07 07:49:50 +08:00
}
obj := TestObject{}
2017-01-07 12:28:16 +08:00
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("hello", obj.Field1)
2017-01-07 07:49:50 +08:00
}
func Test_decode_two_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
Field2 string
2017-01-07 07:49:50 +08:00
}
obj := TestObject{}
2017-01-07 12:28:16 +08:00
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
2017-01-07 07:49:50 +08:00
}
func Test_decode_three_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
Field2 string
Field3 string
2017-01-07 07:49:50 +08:00
}
obj := TestObject{}
2017-01-07 12:28:16 +08:00
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
should.Equal("c", obj.Field3)
2017-01-07 07:49:50 +08:00
}
func Test_decode_four_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
Field2 string
Field3 string
Field4 string
2017-01-07 07:49:50 +08:00
}
obj := TestObject{}
2017-01-07 12:28:16 +08:00
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
should.Equal("c", obj.Field3)
should.Equal("d", obj.Field4)
2017-01-07 07:49:50 +08:00
}
func Test_decode_five_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
Field2 string
Field3 string
Field4 string
Field5 string
2017-01-07 07:49:50 +08:00
}
obj := TestObject{}
2017-01-07 12:28:16 +08:00
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
should.Equal("c", obj.Field3)
should.Equal("d", obj.Field4)
should.Equal("e", obj.Field5)
2017-02-08 13:46:28 +08:00
}
func Test_decode_ten_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-05-24 09:39:11 +08:00
Field1 string
Field2 string
Field3 string
Field4 string
Field5 string
Field6 string
Field7 string
Field8 string
Field9 string
Field10 string
2017-02-08 13:46:28 +08:00
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
2017-05-24 09:39:11 +08:00
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
should.Equal("c", obj.Field3)
should.Equal("d", obj.Field4)
should.Equal("e", obj.Field5)
2017-01-07 07:49:50 +08:00
}
func Test_decode_struct_field_with_tag(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string `json:"field-1"`
Field2 string `json:"-"`
Field3 int `json:",string"`
}
obj := TestObject{Field2: "world"}
2017-01-07 12:28:16 +08:00
UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
2017-01-07 07:49:50 +08:00
should.Equal("hello", obj.Field1)
should.Equal("world", obj.Field2)
should.Equal(100, obj.Field3)
2017-01-09 19:19:48 +08:00
}
func Test_write_val_zero_field_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
}
obj := TestObject{}
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{}`, str)
}
func Test_write_val_one_field_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string `json:"field-1"`
}
obj := TestObject{"hello"}
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"field-1":"hello"}`, str)
}
2017-03-07 18:36:58 -08:00
func Test_mixed(t *testing.T) {
should := require.New(t)
type AA struct {
2017-06-06 23:27:00 +08:00
ID int `json:"id"`
2017-03-07 18:36:58 -08:00
Payload map[string]interface{} `json:"payload"`
2017-06-06 23:27:00 +08:00
buf *bytes.Buffer `json:"-"`
2017-03-07 18:36:58 -08:00
}
aa := AA{}
err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
should.Nil(err)
should.Equal(1, aa.ID)
should.Equal("123", aa.Payload["account"])
2017-03-08 07:38:25 -08:00
}
func Test_omit_empty(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string `json:"field-1,omitempty"`
Field2 string `json:"field-2,omitempty"`
2017-03-11 18:19:39 +08:00
Field3 string `json:"field-3,omitempty"`
2017-03-08 07:38:25 -08:00
}
obj := TestObject{}
obj.Field2 = "hello"
str, err := MarshalToString(&obj)
should.Nil(err)
should.Equal(`{"field-2":"hello"}`, str)
2017-04-28 09:09:24 +08:00
}
func Test_any_within_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 Any
Field2 Any
}
obj := TestObject{}
err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
should.Nil(err)
should.Equal("hello", obj.Field1.ToString())
should.Equal("[1,2,3]", obj.Field2.ToString())
2017-04-28 09:10:06 +08:00
}
2017-05-05 16:51:05 +08:00
func Test_recursive_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
Me *TestObject
2017-05-05 16:51:05 +08:00
}
obj := TestObject{}
str, err := MarshalToString(obj)
should.Nil(err)
2017-06-02 17:34:40 +08:00
should.Contains(str, `"Field1":""`)
should.Contains(str, `"Me":null`)
2017-05-05 16:51:05 +08:00
err = UnmarshalFromString(str, &obj)
should.Nil(err)
}
2017-05-05 17:27:41 +08:00
func Test_one_field_struct(t *testing.T) {
should := require.New(t)
type YetYetAnotherObject struct {
Field string
}
type YetAnotherObject struct {
Field *YetYetAnotherObject
}
2017-05-05 17:27:41 +08:00
type AnotherObject struct {
Field *YetAnotherObject
2017-05-05 17:27:41 +08:00
}
type TestObject struct {
Me *AnotherObject
}
obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
2017-05-05 17:27:41 +08:00
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
str, err = MarshalToString(&obj)
2017-05-05 17:27:41 +08:00
should.Nil(err)
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
}
2017-06-02 16:52:20 +08:00
func Test_anonymous_struct_marshal(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field string
}
2017-06-06 23:27:00 +08:00
str, err := MarshalToString(struct {
2017-06-02 16:52:20 +08:00
TestObject
Field int
}{
Field: 100,
})
should.Nil(err)
should.Equal(`{"Field":100}`, str)
2017-06-06 23:27:00 +08:00
}