1package misc_tests
2
3import (
4	"bytes"
5	"testing"
6
7	"github.com/json-iterator/go"
8	"github.com/stretchr/testify/require"
9	"strings"
10	"time"
11)
12
13func Test_empty_object(t *testing.T) {
14	should := require.New(t)
15	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
16	field := iter.ReadObject()
17	should.Equal("", field)
18	iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
19	iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
20		should.FailNow("should not call")
21		return true
22	})
23}
24
25func Test_one_field(t *testing.T) {
26	should := require.New(t)
27	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
28	field := iter.ReadObject()
29	should.Equal("a", field)
30	value := iter.ReadString()
31	should.Equal("stream", value)
32	field = iter.ReadObject()
33	should.Equal("", field)
34	iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
35	should.True(iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
36		should.Equal("a", field)
37		iter.Skip()
38		return true
39	}))
40
41}
42
43func Test_two_field(t *testing.T) {
44	should := require.New(t)
45	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{ "a": "stream" , "c": "d" }`)
46	field := iter.ReadObject()
47	should.Equal("a", field)
48	value := iter.ReadString()
49	should.Equal("stream", value)
50	field = iter.ReadObject()
51	should.Equal("c", field)
52	value = iter.ReadString()
53	should.Equal("d", value)
54	field = iter.ReadObject()
55	should.Equal("", field)
56	iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"field1": "1", "field2": 2}`)
57	for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
58		switch field {
59		case "field1":
60			iter.ReadString()
61		case "field2":
62			iter.ReadInt64()
63		default:
64			iter.ReportError("bind object", "unexpected field")
65		}
66	}
67}
68
69func Test_write_object(t *testing.T) {
70	should := require.New(t)
71	buf := &bytes.Buffer{}
72	stream := jsoniter.NewStream(jsoniter.Config{IndentionStep: 2}.Froze(), buf, 4096)
73	stream.WriteObjectStart()
74	stream.WriteObjectField("hello")
75	stream.WriteInt(1)
76	stream.WriteMore()
77	stream.WriteObjectField("world")
78	stream.WriteInt(2)
79	stream.WriteObjectEnd()
80	stream.Flush()
81	should.Nil(stream.Error)
82	should.Equal("{\n  \"hello\": 1,\n  \"world\": 2\n}", buf.String())
83}
84
85func Test_reader_and_load_more(t *testing.T) {
86	should := require.New(t)
87	type TestObject struct {
88		CreatedAt time.Time
89	}
90	reader := strings.NewReader(`
91{
92	"agency": null,
93	"candidateId": 0,
94	"candidate": "Blah Blah",
95	"bookingId": 0,
96	"shiftId": 1,
97	"shiftTypeId": 0,
98	"shift": "Standard",
99	"bonus": 0,
100	"bonusNI": 0,
101	"days": [],
102	"totalHours": 27,
103	"expenses": [],
104	"weekEndingDateSystem": "2016-10-09",
105	"weekEndingDateClient": "2016-10-09",
106	"submittedAt": null,
107	"submittedById": null,
108	"approvedAt": "2016-10-10T18:38:04Z",
109	"approvedById": 0,
110	"authorisedAt": "2016-10-10T18:38:04Z",
111	"authorisedById": 0,
112	"invoicedAt": "2016-10-10T20:00:00Z",
113	"revokedAt": null,
114	"revokedById": null,
115	"revokeReason": null,
116	"rejectedAt": null,
117	"rejectedById": null,
118	"rejectReasonCode": null,
119	"rejectReason": null,
120	"createdAt": "2016-10-03T00:00:00Z",
121	"updatedAt": "2016-11-09T10:26:13Z",
122	"updatedById": null,
123	"overrides": [],
124	"bookingApproverId": null,
125	"bookingApprover": null,
126	"status": "approved"
127}
128	`)
129	decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
130	obj := TestObject{}
131	should.Nil(decoder.Decode(&obj))
132}
133
134func Test_unmarshal_into_existing_value(t *testing.T) {
135	should := require.New(t)
136	type TestObject struct {
137		Field1 int
138		Field2 interface{}
139	}
140	var obj TestObject
141	m := map[string]interface{}{}
142	obj.Field2 = &m
143	cfg := jsoniter.Config{UseNumber: true}.Froze()
144	err := cfg.Unmarshal([]byte(`{"Field1":1,"Field2":{"k":"v"}}`), &obj)
145	should.NoError(err)
146	should.Equal(map[string]interface{}{
147		"k": "v",
148	}, m)
149}
150