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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
" Test for JSON functions.
let s:json1 = '"str\"in\\g"'
let s:var1 = "str\"in\\g"
let s:json2 = '"\u0001\u0002\u0003\u0004\u0005\u0006\u0007"'
let s:var2 = "\x01\x02\x03\x04\x05\x06\x07"
let s:json3 = '"\b\t\n\u000b\f\r\u000e\u000f"'
let s:var3 = "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
let s:json4 = '"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"'
let s:var4 = "\x10\x11\x12\x13\x14\x15\x16\x17"
let s:json5 = '"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"'
let s:var5 = "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
" surrogate pair
let s:jsonsp1 = '"\ud83c\udf63"'
let s:varsp1 = "\xf0\x9f\x8d\xa3"
let s:jsonsp2 = '"\ud83c\u00a0"'
let s:varsp2 = "\ud83c\u00a0"
let s:jsonmb = '"s¢cĴgё"'
let s:varmb = "s¢cĴgё"
let s:jsonnr = '1234'
let s:varnr = 1234
if has('float')
let s:jsonfl = '12.34'
let s:varfl = 12.34
let s:jsonneginf = '-Infinity'
let s:jsonposinf = 'Infinity'
let s:varneginf = -1.0 / 0.0
let s:varposinf = 1.0 / 0.0
let s:jsonnan = 'NaN'
let s:varnan = 0.0 / 0.0
endif
let s:jsonl1 = '[1,"a",3]'
let s:varl1 = [1, "a", 3]
let s:jsonl2 = '[1,["a",[],"c"],3]'
let s:jsonl2s = " [\r1 , [ \"a\" , [ ] , \"c\" ] , 3\<Tab>]\r\n"
let s:varl2 = [1, 2, 3]
let l2 = ['a', s:varl2, 'c']
let s:varl2[1] = l2
let s:varl2x = [1, ["a", [], "c"], 3]
let s:jsonl3 = '[[1,2],[1,2]]'
let l3 = [1, 2]
let s:varl3 = [l3, l3]
let s:jsond1 = '{"a":1,"b":"bee","c":[1,2]}'
let s:jsd1 = '{a:1,b:"bee",c:[1,2]}'
let s:vard1 = {"a": 1, "b": "bee","c": [1,2]}
let s:jsond2 = '{"1":1,"2":{"a":"aa","b":{},"c":"cc"},"3":3}'
let s:jsd2 = '{"1":1,"2":{a:"aa",b:{},c:"cc"},"3":3}'
let s:jsond2s = " { \"1\" : 1 , \"2\" :\n{ \"a\"\r: \"aa\" , \"b\" : {\<Tab>} , \"c\" : \"cc\" } , \"3\" : 3 }\r\n"
let s:jsd2s = " { \"1\" : 1 , \"2\" :\n{ a\r: \"aa\" , b : {\<Tab>} , c : \"cc\" } , \"3\" : 3 }\r\n"
let s:vard2 = {"1": 1, "2": 2, "3": 3}
let d2 = {"a": "aa", "b": s:vard2, "c": "cc"}
let s:vard2["2"] = d2
let s:vard2x = {"1": 1, "2": {"a": "aa", "b": {}, "c": "cc"}, "3": 3}
let d3 = {"a": 1, "b": 2}
let s:vard3 = {"x": d3, "y": d3}
let s:jsond3 = '{"x":{"a":1,"b":2},"y":{"a":1,"b":2}}'
let s:jsd3 = '{x:{a:1,b:2},y:{a:1,b:2}}'
let s:vard4 = {"key": v:none}
let s:vard4x = {"key": v:null}
let s:jsond4 = '{"key":null}'
let s:jsd4 = '{key:null}' " js_encode puts no quotes around simple key.
let s:vard5 = {"key!": v:none}
let s:vard5x = {"key!": v:null}
let s:jsond5 = '{"key!":null}'
let s:jsd5 = '{"key!":null}' " js_encode puts quotes around non-simple key.
let s:jsonvals = '[true,false,null,null]'
let s:varvals = [v:true, v:false, v:null, v:null]
func Test_json_encode()
call assert_equal(s:json1, json_encode(s:var1))
call assert_equal(s:json2, json_encode(s:var2))
call assert_equal(s:json3, s:var3->json_encode())
call assert_equal(s:json4, json_encode(s:var4))
call assert_equal(s:json5, json_encode(s:var5))
call assert_equal(s:jsonmb, json_encode(s:varmb))
" no test for surrogate pair, json_encode() doesn't create them.
call assert_equal(s:jsonnr, json_encode(s:varnr))
if has('float')
call assert_equal(s:jsonfl, json_encode(s:varfl))
call assert_equal(s:jsonneginf, json_encode(s:varneginf))
call assert_equal(s:jsonposinf, json_encode(s:varposinf))
call assert_equal(s:jsonnan, json_encode(s:varnan))
endif
call assert_equal(s:jsonl1, json_encode(s:varl1))
call assert_equal(s:jsonl2, json_encode(s:varl2))
call assert_equal(s:jsonl3, json_encode(s:varl3))
call assert_equal(s:jsond1, json_encode(s:vard1))
call assert_equal(s:jsond2, json_encode(s:vard2))
call assert_equal(s:jsond3, json_encode(s:vard3))
call assert_equal(s:jsond4, json_encode(s:vard4))
call assert_equal(s:jsond5, json_encode(s:vard5))
call assert_equal(s:jsonvals, json_encode(s:varvals))
" JSON is always encoded in utf-8 regardless of 'encoding' value.
let save_encoding = &encoding
set encoding=latin1
call assert_equal('"café"', json_encode("caf\xe9"))
let &encoding = save_encoding
call assert_fails('echo json_encode(function("tr"))', 'E1161: Cannot json encode a func')
call assert_fails('echo json_encode([function("tr")])', 'E1161: Cannot json encode a func')
call assert_equal('{"a":""}', json_encode({'a': test_null_string()}))
call assert_equal('{"a":[]}', json_encode({"a": test_null_list()}))
call assert_equal('{"a":{}}', json_encode({"a": test_null_dict()}))
silent! let res = json_encode(function("tr"))
call assert_equal("", res)
endfunc
func Test_json_decode()
call assert_equal(s:var1, json_decode(s:json1))
call assert_equal(s:var2, json_decode(s:json2))
call assert_equal(s:var3, s:json3->json_decode())
call assert_equal(s:var4, json_decode(s:json4))
call assert_equal(s:var5, json_decode(s:json5))
call assert_equal(s:varmb, json_decode(s:jsonmb))
call assert_equal(s:varsp1, json_decode(s:jsonsp1))
call assert_equal(s:varsp2, json_decode(s:jsonsp2))
call assert_equal(s:varnr, json_decode(s:jsonnr))
if has('float')
call assert_equal(s:varfl, json_decode(s:jsonfl))
endif
call assert_equal(s:varl1, json_decode(s:jsonl1))
call assert_equal(s:varl2x, json_decode(s:jsonl2))
call assert_equal(s:varl2x, json_decode(s:jsonl2s))
call assert_equal(s:varl3, json_decode(s:jsonl3))
call assert_equal(s:vard1, json_decode(s:jsond1))
call assert_equal(s:vard2x, json_decode(s:jsond2))
call assert_equal(s:vard2x, json_decode(s:jsond2s))
call assert_equal(s:vard3, json_decode(s:jsond3))
call assert_equal(s:vard4x, json_decode(s:jsond4))
call assert_equal(s:vard5x, json_decode(s:jsond5))
call assert_equal(s:varvals, json_decode(s:jsonvals))
call assert_equal(v:true, json_decode('true'))
call assert_equal(type(v:true), type(json_decode('true')))
call assert_equal(v:none, json_decode(''))
call assert_equal(type(v:none), type(json_decode('')))
call assert_equal("", json_decode('""'))
" Character in string after \ is ignored if not special.
call assert_equal("x", json_decode('"\x"'))
" JSON is always encoded in utf-8 regardless of 'encoding' value.
let save_encoding = &encoding
set encoding=latin1
call assert_equal("caf\xe9", json_decode('"café"'))
let &encoding = save_encoding
" empty key is OK
call assert_equal({'': 'ok'}, json_decode('{"": "ok"}'))
" but not twice
call assert_fails("call json_decode('{\"\": \"ok\", \"\": \"bad\"}')", 'E938:')
call assert_equal({'n': 1}, json_decode('{"n":1,}'))
call assert_fails("call json_decode(\"{'n':'1',}\")", 'E491:')
call assert_fails("call json_decode(\"'n'\")", 'E491:')
call assert_fails('call json_decode("\"")', "E491:")
call assert_fails('call json_decode("blah")', "E491:")
call assert_fails('call json_decode("true blah")', "E488:")
call assert_fails('call json_decode("<foobar>")', "E491:")
call assert_fails('call json_decode("{\"a\":1,\"a\":2}")', "E938:")
call assert_fails('call json_decode("{")', "E491:")
call assert_fails('call json_decode("{foobar}")', "E491:")
call assert_fails('call json_decode("{\"n\",")', "E491:")
call assert_fails('call json_decode("{\"n\":")', "E491:")
call assert_fails('call json_decode("{\"n\":1")', "E491:")
call assert_fails('call json_decode("{\"n\":1,")', "E491:")
call assert_fails('call json_decode("{\"n\",1}")', "E491:")
call assert_fails('call json_decode("{-}")', "E491:")
if has('float')
call assert_fails('call json_decode("{3.14:1}")', "E806:")
endif
call assert_fails('call json_decode("[foobar]")', "E491:")
call assert_fails('call json_decode("[")', "E491:")
call assert_fails('call json_decode("[1")', "E491:")
call assert_fails('call json_decode("[1,")', "E491:")
call assert_fails('call json_decode("[1 2]")', "E491:")
call assert_fails('call json_decode("[1,,2]")', "E491:")
call assert_fails('call json_decode("{{}:42}")', "E491:")
call assert_fails('call json_decode("{[]:42}")', "E491:")
call assert_fails('call json_decode("{1:1{")', "E491:")
call assert_fails('call json_decode("-")', "E491:")
call assert_fails('call json_decode("-1x")', "E491:")
call assert_fails('call json_decode("infinit")', "E491:")
call assert_fails('call json_decode("\"\\u111Z\"")', 'E491:')
call assert_equal('[😂]', json_decode('"[\uD83D\uDE02]"'))
call assert_equal('a😂b', json_decode('"a\uD83D\uDE02b"'))
call assert_fails('call json_decode("{\"\":{\"\":{")', 'E491:')
endfunc
let s:jsl5 = '[7,,,]'
let s:varl5 = [7, v:none, v:none]
func Test_js_encode()
call assert_equal(s:json1, js_encode(s:var1))
call assert_equal(s:json2, js_encode(s:var2))
call assert_equal(s:json3, s:var3->js_encode())
call assert_equal(s:json4, js_encode(s:var4))
call assert_equal(s:json5, js_encode(s:var5))
call assert_equal(s:jsonmb, js_encode(s:varmb))
" no test for surrogate pair, js_encode() doesn't create them.
call assert_equal(s:jsonnr, js_encode(s:varnr))
if has('float')
call assert_equal(s:jsonfl, js_encode(s:varfl))
call assert_equal(s:jsonneginf, js_encode(s:varneginf))
call assert_equal(s:jsonposinf, js_encode(s:varposinf))
call assert_equal(s:jsonnan, js_encode(s:varnan))
endif
call assert_equal(s:jsonl1, js_encode(s:varl1))
call assert_equal(s:jsonl2, js_encode(s:varl2))
call assert_equal(s:jsonl3, js_encode(s:varl3))
call assert_equal(s:jsd1, js_encode(s:vard1))
call assert_equal(s:jsd2, js_encode(s:vard2))
call assert_equal(s:jsd3, js_encode(s:vard3))
call assert_equal(s:jsd4, js_encode(s:vard4))
call assert_equal(s:jsd5, js_encode(s:vard5))
call assert_equal(s:jsonvals, js_encode(s:varvals))
call assert_fails('echo js_encode(function("tr"))', 'E1161: Cannot json encode a func')
call assert_fails('echo js_encode([function("tr")])', 'E1161: Cannot json encode a func')
silent! let res = js_encode(function("tr"))
call assert_equal("", res)
call assert_equal(s:jsl5, js_encode(s:varl5))
endfunc
func Test_js_decode()
call assert_equal(s:var1, js_decode(s:json1))
call assert_equal(s:var2, js_decode(s:json2))
call assert_equal(s:var3, s:json3->js_decode())
call assert_equal(s:var4, js_decode(s:json4))
call assert_equal(s:var5, js_decode(s:json5))
call assert_equal(s:varmb, js_decode(s:jsonmb))
call assert_equal(s:varsp1, js_decode(s:jsonsp1))
call assert_equal(s:varsp2, js_decode(s:jsonsp2))
call assert_equal(s:varnr, js_decode(s:jsonnr))
if has('float')
call assert_equal(s:varfl, js_decode(s:jsonfl))
call assert_equal(s:varneginf, js_decode(s:jsonneginf))
call assert_equal(s:varposinf, js_decode(s:jsonposinf))
call assert_true(isnan(js_decode(s:jsonnan)))
endif
call assert_equal(s:varl1, js_decode(s:jsonl1))
call assert_equal(s:varl2x, js_decode(s:jsonl2))
call assert_equal(s:varl2x, js_decode(s:jsonl2s))
call assert_equal(s:varl3, js_decode(s:jsonl3))
call assert_equal(s:vard1, js_decode(s:jsond1))
call assert_equal(s:vard1, js_decode(s:jsd1))
call assert_equal(s:vard2x, js_decode(s:jsond2))
call assert_equal(s:vard2x, js_decode(s:jsd2))
call assert_equal(s:vard2x, js_decode(s:jsond2s))
call assert_equal(s:vard2x, js_decode(s:jsd2s))
call assert_equal(s:vard3, js_decode(s:jsond3))
call assert_equal(s:vard3, js_decode(s:jsd3))
call assert_equal(s:vard4x, js_decode(s:jsond4))
call assert_equal(s:vard4x, js_decode(s:jsd4))
call assert_equal(s:vard5x, js_decode(s:jsond5))
call assert_equal(s:vard5x, js_decode(s:jsd5))
call assert_equal(s:varvals, js_decode(s:jsonvals))
call assert_equal(v:true, js_decode('true'))
call assert_equal(type(v:true), type(js_decode('true')))
call assert_equal(v:none, js_decode(''))
call assert_equal(type(v:none), type(js_decode('')))
call assert_equal("", js_decode('""'))
call assert_equal("", js_decode("''"))
call assert_equal('n', js_decode("'n'"))
call assert_equal({'n': 1}, js_decode('{"n":1,}'))
call assert_equal({'n': '1'}, js_decode("{'n':'1',}"))
call assert_fails('call js_decode("\"")', "E491:")
call assert_fails('call js_decode("blah")', "E491:")
call assert_fails('call js_decode("true blah")', "E488:")
call assert_fails('call js_decode("<foobar>")', "E491:")
call assert_fails('call js_decode("{")', "E491:")
call assert_fails('call js_decode("{foobar}")', "E491:")
call assert_fails('call js_decode("{\"n\",")', "E491:")
call assert_fails('call js_decode("{\"n\":")', "E491:")
call assert_fails('call js_decode("{\"n\":1")', "E491:")
call assert_fails('call js_decode("{\"n\":1,")', "E491:")
call assert_fails('call js_decode("{\"n\",1}")', "E491:")
call assert_fails('call js_decode("{-}")', "E491:")
call assert_fails('call js_decode("[foobar]")', "E491:")
call assert_fails('call js_decode("[")', "E491:")
call assert_fails('call js_decode("[1")', "E491:")
call assert_fails('call js_decode("[1,")', "E491:")
call assert_fails('call js_decode("[1 2]")', "E491:")
call assert_equal(s:varl5, js_decode(s:jsl5))
endfunc
func Test_json_encode_long()
" The growarray uses a grow size of 4000, check that a result that is exactly
" 4000 bytes long is not missing the final NUL.
let json = json_encode([repeat('a', 3996)])
call assert_equal(4000, len(json))
endfunc
" vim: shiftwidth=2 sts=2 expandtab
|