forked from cevaris/ordered_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_codec_test.go
More file actions
318 lines (309 loc) · 6.2 KB
/
json_codec_test.go
File metadata and controls
318 lines (309 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package orderedmap
import (
"testing"
"encoding/json"
"fmt"
"log"
)
func TestBlankMarshalJSON(t *testing.T) {
o := New()
// blank map
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling blank map to json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{}` {
t.Error("JSON Marshaling blank map value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json for blank map", err)
}
si := string(bi)
ei := `{}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent blank map value is incorrect", si)
}
}
func TestMarshalJSON(t *testing.T) {
o := New()
// number
o.Set("number", 3)
// string
o.Set("string", "x")
// new value keeps key in old position
o.Set("number", 4)
// keys not sorted alphabetically
o.Set("z", 1)
o.Set("a", 2)
o.Set("b", 3)
// slice
o.Set("slice", []interface{}{
"1",
1,
})
// orderedmap
v := New()
v.Set("e", 1)
v.Set("a", 2)
o.Set("orderedmap", v)
// double quote in key
o.Set(`test"ing`, 9)
// convert to json
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{"number":4,"string":"x","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\"ing":9}` {
t.Error("JSON Marshal value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json", err)
}
si := string(bi)
ei := `{
"number": 4,
"string": "x",
"z": 1,
"a": 2,
"b": 3,
"slice": [
"1",
1
],
"orderedmap": {
"e": 1,
"a": 2
},
"test\"ing": 9
}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent value is incorrect", si)
}
}
func TestUnmarshalJSON(t *testing.T) {
s := `{
"number": 4,
"string": "x",
"z": 1,
"a": "should not break with unclosed { character in value",
"b": 3,
"slice": [
"1",
1
],
"orderedmap": {
"e": 1,
"a { nested key with brace": "with a }}}} }} {{{ brace value",
"after": {
"link": "test {{{ with even deeper nested braces }"
}
},
"test\"ing": 9,
"after": 1,
"multitype_array": [
"test",
1,
{
"map": "obj",
"it": 5,
":colon in key": "colon: in value"
},
[
{
"inner": "map"
}
]
],
"should not break with { character in key": 1
}`
om := New()
err := json.Unmarshal([]byte(s), &om)
if err != nil {
t.Error("YAML Unmarshal error", err)
}
sout, err := json.MarshalIndent(&om, "", " ")
if err != nil {
t.Error("YAML Marshal error", err)
}
if string(sout) != s {
log.Println(string(sout))
t.Error("YAML Marshal/Unmarshal doesn't match")
}
// Check the root keys
expectedKeys := []string{
"number",
"string",
"z",
"a",
"b",
"slice",
"orderedmap",
"test\"ing",
"after",
"multitype_array",
"should not break with { character in key",
}
iter := om.IterFunc()
i := 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
// Check nested maps are converted to orderedmaps
// nested 1 level deep
expectedKeys = []string{
"e",
"a { nested key with brace",
"after",
}
vi, ok := om.Get("orderedmap")
if !ok {
t.Error("Missing key for nested map 1 deep")
}
v := vi.(OrderedMap)
iter = v.IterFunc()
i = 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
// nested 2 levels deep
expectedKeys = []string{
"link",
}
vi, ok = v.Get("after")
if !ok {
t.Error("Missing key for nested map 2 deep")
}
v = vi.(OrderedMap)
iter = v.IterFunc()
i = 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
// multitype array
expectedKeys = []string{
"map",
"it",
":colon in key",
}
vislice, ok := om.Get("multitype_array")
if !ok {
t.Error("Missing key for multitype array")
}
vslice := vislice.([]interface{})
vmap := vslice[2].(OrderedMap)
iter = vmap.IterFunc()
i = 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
expectedKeys = []string{"inner"}
vinnerslice := vslice[3].([]interface{})
vinnermap := vinnerslice[0].(OrderedMap)
iter = vinnermap.IterFunc()
i = 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
}
func TestUnmarshalJSONSpecialChars(t *testing.T) {
s := `{ " \\\\\\\\\\\\ " : { "\\\\\\" : "\\\\\"\\" }, "\\": " \\\\ test " }`
o := New()
err := json.Unmarshal([]byte(s), &o)
if err != nil {
t.Error("JSON Unmarshal error with special chars", err)
}
}
func TestUnmarshalJSONArrayOfMaps(t *testing.T) {
s := `
{
"name": "test",
"percent": 6,
"breakdown": [
{
"name": "a",
"percent": 0.9
},
{
"name": "b",
"percent": 0.9
},
{
"name": "d",
"percent": 0.4
},
{
"name": "e",
"percent": 2.7
}
]
}
`
om := New()
err := json.Unmarshal([]byte(s), &om)
if err != nil {
t.Error("JSON Unmarshal error", err)
}
// Check the root keys
expectedKeys := []string{
"name",
"percent",
"breakdown",
}
iter := om.IterFunc()
i := 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
// Check nested maps are converted to orderedmaps
// nested 1 level deep
expectedKeys = []string{
"name",
"percent",
}
vi, ok := om.Get("breakdown")
if !ok {
t.Error("Missing key for nested map 1 deep")
}
vs := vi.([]interface{})
for _, vInterface := range vs {
v := vInterface.(OrderedMap)
iter := v.IterFunc()
i := 0
for kv, ok := iter(); ok; kv, ok = iter() {
if kv.Key != expectedKeys[i] {
t.Error("Unmarshal root key order", i, kv.Key, "!=", expectedKeys[i])
}
i++
}
}
}