1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-12 09:03:48 +02:00
json-iterator/jsoniter_object_test.go

351 lines
8.6 KiB
Go
Raw Normal View History

2016-12-01 10:11:07 +02:00
package jsoniter
import (
2017-06-06 17:27:00 +02:00
"bytes"
2017-06-19 17:43:53 +02:00
"fmt"
2017-01-07 16:08:45 +02:00
"github.com/json-iterator/go/require"
2017-06-06 17:27:00 +02:00
"testing"
2016-12-01 10:11:07 +02:00
)
func Test_empty_object(t *testing.T) {
2017-01-20 06:56:49 +02:00
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `{}`)
2016-12-01 10:11:07 +02:00
field := iter.ReadObject()
2017-01-20 06:56:49 +02:00
should.Equal("", field)
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `{}`)
2017-01-20 06:56:49 +02:00
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.FailNow("should not call")
return true
})
2016-12-01 10:11:07 +02:00
}
func Test_one_field(t *testing.T) {
2017-01-20 06:56:49 +02:00
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `{"a": "b"}`)
2016-12-01 10:11:07 +02:00
field := iter.ReadObject()
2017-01-20 06:56:49 +02:00
should.Equal("a", field)
2016-12-01 10:11:07 +02:00
value := iter.ReadString()
2017-01-20 06:56:49 +02:00
should.Equal("b", value)
2016-12-01 10:11:07 +02:00
field = iter.ReadObject()
2017-01-20 06:56:49 +02:00
should.Equal("", field)
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `{"a": "b"}`)
2017-01-20 06:56:49 +02:00
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.Equal("a", field)
return true
}))
2016-12-01 10:11:07 +02:00
}
func Test_two_field(t *testing.T) {
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `{ "a": "b" , "c": "d" }`)
2016-12-01 10:11:07 +02:00
field := iter.ReadObject()
should.Equal("a", field)
2016-12-01 10:11:07 +02:00
value := iter.ReadString()
should.Equal("b", value)
2016-12-01 10:11:07 +02:00
field = iter.ReadObject()
should.Equal("c", field)
2016-12-01 10:11:07 +02:00
value = iter.ReadString()
should.Equal("d", value)
2016-12-01 10:11:07 +02:00
field = iter.ReadObject()
should.Equal("", field)
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `{"field1": "1", "field2": 2}`)
2016-12-01 10:11:07 +02:00
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
iter.ReadString()
case "field2":
iter.ReadInt64()
default:
iter.reportError("bind object", "unexpected field")
2016-12-01 10:11:07 +02:00
}
}
}
2017-06-17 15:32:48 +02:00
func Test_object_wrapper_any_get_all(t *testing.T) {
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
Field1 []int
Field2 []int
}
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
}
2017-06-17 15:32:48 +02:00
func Test_write_object(t *testing.T) {
should := require.New(t)
2017-06-17 15:32:48 +02:00
buf := &bytes.Buffer{}
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
stream.WriteObjectStart()
stream.WriteObjectField("hello")
stream.WriteInt(1)
stream.WriteMore()
stream.WriteObjectField("world")
stream.WriteInt(2)
stream.WriteObjectEnd()
stream.Flush()
should.Nil(stream.Error)
should.Equal("{\n \"hello\":1,\n \"world\":2\n}", buf.String())
}
2017-06-17 15:32:48 +02:00
func Test_decode_one_field_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
should.Equal("hello", obj.Field1)
}
2017-06-17 15:32:48 +02:00
func Test_decode_two_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
Field2 string
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
should.Equal("a", obj.Field1)
should.Equal("b", obj.Field2)
}
2017-06-17 15:32:48 +02:00
func Test_decode_three_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
Field2 string
Field3 string
}
2017-06-17 15:32:48 +02:00
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
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-06-17 15:32:48 +02:00
func Test_decode_four_fields_struct(t *testing.T) {
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
Field1 string
Field2 string
Field3 string
Field4 string
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
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-06-17 15:32:48 +02:00
func Test_decode_five_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
Field2 string
Field3 string
Field4 string
Field5 string
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
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-06-17 15:32:48 +02:00
func Test_decode_ten_fields_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field1 string
Field2 string
Field3 string
Field4 string
Field5 string
Field6 string
Field7 string
Field8 string
Field9 string
Field10 string
}
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
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-06-17 15:32:48 +02: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"}
UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
should.Equal("hello", obj.Field1)
should.Equal("world", obj.Field2)
should.Equal(100, obj.Field3)
}
2017-06-17 15:32:48 +02:00
func Test_write_val_zero_field_struct(t *testing.T) {
2017-01-24 17:13:58 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
}
obj := TestObject{}
str, err := MarshalToString(obj)
2017-01-24 17:13:58 +02:00
should.Nil(err)
2017-06-17 15:32:48 +02:00
should.Equal(`{}`, str)
2017-01-24 17:13:58 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_write_val_one_field_struct(t *testing.T) {
2017-01-31 14:26:35 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
Field1 string `json:"field-1"`
}
obj := TestObject{"hello"}
str, err := MarshalToString(obj)
2017-01-31 14:26:35 +02:00
should.Nil(err)
2017-06-17 15:32:48 +02:00
should.Equal(`{"field-1":"hello"}`, str)
2017-01-31 14:26:35 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_mixed(t *testing.T) {
2017-01-26 10:24:01 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type AA struct {
ID int `json:"id"`
Payload map[string]interface{} `json:"payload"`
buf *bytes.Buffer `json:"-"`
}
aa := AA{}
err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
2017-01-26 10:24:01 +02:00
should.Nil(err)
2017-06-17 15:32:48 +02:00
should.Equal(1, aa.ID)
should.Equal("123", aa.Payload["account"])
2017-01-26 10:24:01 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_omit_empty(t *testing.T) {
2017-01-25 16:43:57 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
Field1 string `json:"field-1,omitempty"`
Field2 string `json:"field-2,omitempty"`
Field3 string `json:"field-3,omitempty"`
}
obj := TestObject{}
obj.Field2 = "hello"
str, err := MarshalToString(&obj)
2017-01-25 18:25:17 +02:00
should.Nil(err)
2017-06-17 15:32:48 +02:00
should.Equal(`{"field-2":"hello"}`, str)
2017-01-25 16:43:57 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_recursive_struct(t *testing.T) {
2017-01-28 16:45:03 +02:00
should := require.New(t)
type TestObject struct {
Field1 string
2017-06-17 15:32:48 +02:00
Me *TestObject
2017-01-28 16:45:03 +02:00
}
2017-06-17 15:32:48 +02:00
obj := TestObject{}
str, err := MarshalToString(obj)
should.Nil(err)
should.Contains(str, `"Field1":""`)
should.Contains(str, `"Me":null`)
err = UnmarshalFromString(str, &obj)
should.Nil(err)
2017-01-28 16:45:03 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_one_field_struct(t *testing.T) {
2017-01-31 17:16:40 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type YetYetAnotherObject struct {
Field string
}
type YetAnotherObject struct {
Field *YetYetAnotherObject
}
type AnotherObject struct {
Field *YetAnotherObject
}
2017-01-31 17:16:40 +02:00
type TestObject struct {
2017-06-17 15:32:48 +02:00
Me *AnotherObject
2017-01-31 17:16:40 +02:00
}
2017-06-17 15:32:48 +02:00
obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
str, err = MarshalToString(&obj)
should.Nil(err)
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
2017-01-31 17:16:40 +02:00
}
2017-06-19 17:02:57 +02:00
func Test_encode_anonymous_struct(t *testing.T) {
2017-01-07 16:08:45 +02:00
should := require.New(t)
2017-06-17 15:32:48 +02:00
type TestObject struct {
Field string
}
str, err := MarshalToString(struct {
TestObject
Field int
}{
Field: 100,
})
should.Nil(err)
should.Equal(`{"Field":100}`, str)
2017-01-07 16:08:45 +02:00
}
2017-06-19 17:02:57 +02:00
func Test_decode_anonymous_struct(t *testing.T) {
should := require.New(t)
type Inner struct {
Key string `json:"key"`
}
type Outer struct {
Inner
}
var outer Outer
j := []byte("{\"key\":\"value\"}")
should.Nil(Unmarshal(j, &outer))
should.Equal("value", outer.Key)
}
2017-06-17 15:32:48 +02:00
func Test_decode_nested(t *testing.T) {
type StructOfString struct {
2017-02-12 16:49:45 +02:00
Field1 string
2017-06-17 15:32:48 +02:00
Field2 string
2017-02-12 16:49:45 +02:00
}
2017-06-17 15:32:48 +02:00
iter := ParseString(ConfigDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
slice := []*StructOfString{}
iter.ReadVal(&slice)
if len(slice) != 3 {
fmt.Println(iter.Error)
t.Fatal(len(slice))
2016-12-04 05:06:38 +02:00
}
2017-06-17 15:32:48 +02:00
if slice[0].Field1 != "hello" {
fmt.Println(iter.Error)
t.Fatal(slice[0])
}
if slice[1] != nil {
fmt.Println(iter.Error)
t.Fatal(slice[1])
2017-02-12 16:49:45 +02:00
}
2017-06-17 15:32:48 +02:00
if slice[2].Field2 != "world" {
fmt.Println(iter.Error)
t.Fatal(slice[2])
2016-12-01 10:11:07 +02:00
}
}