2 function createTests() {
4 result.push(function(jsonObject){
5 return jsonObject.parse();
7 result[result.length - 1].throws = true;
8 result.push(function(jsonObject){
9 return jsonObject.parse('');
11 result[result.length - 1].throws = true;
12 result.push(function(jsonObject){
13 return jsonObject.parse('1');
15 result.push(function(jsonObject){
16 return jsonObject.parse('-1');
18 result.push(function(jsonObject){
19 return jsonObject.parse('Infinity');
21 result[result.length - 1].throws = true;
22 result.push(function(jsonObject){
23 return jsonObject.parse('NaN');
25 result[result.length - 1].throws = true;
26 result.push(function(jsonObject){
27 return jsonObject.parse('null');
29 result.push(function(jsonObject){
30 return jsonObject.parse('undefined');
32 result[result.length - 1].throws = true;
33 result.push(function(jsonObject){
34 return jsonObject.parse('{}');
36 result.push(function(jsonObject){
37 return jsonObject.parse('({})');
39 result[result.length - 1].throws = true;
40 result.push(function(jsonObject){
41 return jsonObject.parse('{a}');
43 result[result.length - 1].throws = true;
44 result.push(function(jsonObject){
45 return jsonObject.parse('{a:}');
47 result[result.length - 1].throws = true;
48 result.push(function(jsonObject){
49 return jsonObject.parse('{a:5}');
51 result[result.length - 1].throws = true;
52 result.push(function(jsonObject){
53 return jsonObject.parse('{a:5,}');
55 result[result.length - 1].throws = true;
56 result.push(function(jsonObject){
57 return jsonObject.parse('{"a"}');
59 result[result.length - 1].throws = true;
60 result.push(function(jsonObject){
61 return jsonObject.parse('{"a":}');
63 result[result.length - 1].throws = true;
64 result.push(function(jsonObject){
65 return jsonObject.parse('{"a":5}');
67 result.push(function(jsonObject){
68 return jsonObject.parse('{"__proto__":5}');
70 result.push(function(jsonObject){
71 return jsonObject.parse('{"a":5,}');
73 result[result.length - 1].throws = true;
74 result.push(function(jsonObject){
75 return jsonObject.parse('{"a":5,,}');
77 result[result.length - 1].throws = true;
78 result.push(function(jsonObject){
79 return jsonObject.parse('{"a":5,"a",}');
81 result[result.length - 1].throws = true;
82 result.push(function(jsonObject){
83 return jsonObject.parse('{"a":(5,"a"),}');
85 result[result.length - 1].throws = true;
86 result.push(function(jsonObject){
87 return jsonObject.parse('[]');
89 result.push(function(jsonObject){
90 return jsonObject.parse('[1]');
92 result.push(function(jsonObject){
93 return jsonObject.parse('[1,]');
95 result[result.length - 1].throws = true;
96 result.push(function(jsonObject){
97 return jsonObject.parse('[1,2]');
99 result.push(function(jsonObject){
100 return jsonObject.parse('[1,2,,]');
102 result[result.length - 1].throws = true;
103 result.push(function(jsonObject){
104 return jsonObject.parse('[1,2,,4]');
106 result[result.length - 1].throws = true;
107 result.push(function(jsonObject){
108 return jsonObject.parse('""');
110 result.push(function(jsonObject){
111 return jsonObject.parse('"\'"');
113 result.push(function(jsonObject){
114 return jsonObject.parse('"a\"');
116 result.push(function(jsonObject){
117 return jsonObject.parse('"a\\"');
119 result[result.length - 1].throws = true;
120 result.push(function(jsonObject){
121 return jsonObject.parse('"a\\z"');
123 result[result.length - 1].throws = true;
124 result.push(function(jsonObject){
125 return jsonObject.parse('"a\\\z"');
127 result[result.length - 1].throws = true;
128 result.push(function(jsonObject){
129 return jsonObject.parse('"a\\\\z"');
131 result.push(function(jsonObject){
132 return jsonObject.parse('"a\tz"');
134 result[result.length - 1].throws = true; // rfc4627 does not allow literal tab characters in JSON source
135 result.push(function(jsonObject){
136 return jsonObject.parse('"a\\tz"');
138 result.push(function(jsonObject){
139 return jsonObject.parse('"a\nz"');
141 result[result.length - 1].throws = true;
142 result.push(function(jsonObject){
143 return jsonObject.parse('"a\\nz"');
145 result.push(function(jsonObject){
146 return jsonObject.parse('"a\rz"');
148 result[result.length - 1].throws = true;
149 result.push(function(jsonObject){
150 return jsonObject.parse('"a\\rz"');
152 result.push(function(jsonObject){
153 return jsonObject.parse('"a\/z"');
155 result.push(function(jsonObject){
156 return jsonObject.parse('"a\\/z"');
158 result.push(function(jsonObject){
159 return jsonObject.parse('"a\bz"');
161 result[result.length - 1].throws = true;
162 result.push(function(jsonObject){
163 return jsonObject.parse('"a\\bz"');
165 result.push(function(jsonObject){
166 return jsonObject.parse('"a\rz"');
168 result[result.length - 1].throws = true;
169 result.push(function(jsonObject){
170 return jsonObject.parse('"a\\rz"');
172 result.push(function(jsonObject){
173 return jsonObject.parse('"a\\uz" ');
175 result[result.length - 1].throws = true;
176 result.push(function(jsonObject){
177 return jsonObject.parse('"a\\u0z" ');
179 result[result.length - 1].throws = true;
180 result.push(function(jsonObject){
181 return jsonObject.parse('"a\\u00z" ');
183 result[result.length - 1].throws = true;
184 result.push(function(jsonObject){
185 return jsonObject.parse('"a\\u000z" ');
187 result[result.length - 1].throws = true;
188 result.push(function(jsonObject){
189 return jsonObject.parse('"a\\u0000z" ');
191 result.push(function(jsonObject){
192 return jsonObject.parse('"a\\u000Az" ');
194 result.push(function(jsonObject){
195 return jsonObject.parse('"a\\u000az" ');
197 result.push(function(jsonObject){
198 return jsonObject.parse('"a\\u000Gz" ');
200 result[result.length - 1].throws = true;
201 result.push(function(jsonObject){
202 return jsonObject.parse('"a\\u000gz" ');
204 result[result.length - 1].throws = true;
205 result.push(function(jsonObject){
206 return jsonObject.parse('"a\\u00A0z" ');
208 result.push(function(jsonObject){
209 return jsonObject.parse('"a\\u00a0z" ');
211 result.push(function(jsonObject){
212 return jsonObject.parse('"a\\u00G0z" ');
214 result[result.length - 1].throws = true;
215 result.push(function(jsonObject){
216 return jsonObject.parse('"a\\u00g0z" ');
218 result[result.length - 1].throws = true;
219 result.push(function(jsonObject){
220 return jsonObject.parse('"a\\u0A00z" ');
222 result.push(function(jsonObject){
223 return jsonObject.parse('"a\\u0a00z" ');
225 result.push(function(jsonObject){
226 return jsonObject.parse('"a\\u0G00z" ');
228 result[result.length - 1].throws = true;
229 result.push(function(jsonObject){
230 return jsonObject.parse('"a\\u0g00z" ');
232 result[result.length - 1].throws = true;
233 result.push(function(jsonObject){
234 return jsonObject.parse('"a\\uA000z" ');
236 result.push(function(jsonObject){
237 return jsonObject.parse('"a\\ua000z" ');
239 result.push(function(jsonObject){
240 return jsonObject.parse('"a\\uG000z" ');
242 result[result.length - 1].throws = true;
243 result.push(function(jsonObject){
244 return jsonObject.parse('"a\\ug000z" ');
246 result[result.length - 1].throws = true;
247 result.push(function(jsonObject){
248 return jsonObject.parse('00');
250 result[result.length - 1].throws = true;
251 result.push(function(jsonObject){
252 return jsonObject.parse('01');
254 result[result.length - 1].throws = true;
255 result.push(function(jsonObject){
256 return jsonObject.parse('0.a');
258 result[result.length - 1].throws = true;
259 result.push(function(jsonObject){
260 return jsonObject.parse('0x0');
262 result[result.length - 1].throws = true;
263 result.push(function(jsonObject){
264 return jsonObject.parse('2e1.3');
266 result[result.length - 1].throws = true;
267 result.push(function(jsonObject){
268 return jsonObject.parse('2e-+10');
270 result[result.length - 1].throws = true;
271 result.push(function(jsonObject){
272 return jsonObject.parse('2e+-10');
274 result[result.length - 1].throws = true;
275 result.push(function(jsonObject){
276 return jsonObject.parse('2e3e4');
278 result[result.length - 1].throws = true;
279 result.push(function(jsonObject){
280 return jsonObject.parse('-01.0');
282 result[result.length - 1].throws = true;
283 result.push(function(jsonObject){
284 return jsonObject.parse('-01');
286 result[result.length - 1].throws = true;
287 result.push(function(jsonObject){
288 return jsonObject.parse('-01.a');
290 result[result.length - 1].throws = true;
291 result.push(function(jsonObject){
292 return jsonObject.parse('1.e1');
294 result[result.length - 1].throws = true;
295 result.push(function(jsonObject){
296 return jsonObject.parse('{/* block comments are not allowed */}');
298 result[result.length - 1].throws = true;
299 result.push(function(jsonObject){
300 return jsonObject.parse('{// line comments are not allowed \n}');
302 result[result.length - 1].throws = true;
303 result.push(function(jsonObject){
304 return jsonObject.parse('true');
306 result.push(function(jsonObject){
307 return jsonObject.parse('false');
309 var simpleArray = ['a', 'b', 'c'];
310 var simpleObject = {a:"1", b:"2", c:"3"};
311 var complexArray = ['a', 'b', 'c',,,simpleObject, simpleArray, [simpleObject,simpleArray]];
312 var complexObject = {a:"1", b:"2", c:"3", d:4.5e10, g: 0.45e-5, h: 0.0, i: 0, j:.5, k:0., l:-0, m:-0.0, n:-0., o:-.5, p:-0.45e-10, q:-4.5e10, e:null, "":12, f: simpleArray, array: complexArray};
313 result.push(function(jsonObject){
314 return jsonObject.parse(JSON.stringify(simpleObject));
316 result[result.length - 1].expected = JSON.stringify(simpleObject);
317 result.push(function(jsonObject){
318 return jsonObject.parse(JSON.stringify(complexObject));
320 result.push(function(jsonObject){
321 return jsonObject.parse(JSON.stringify(complexObject));
323 result[result.length - 1].expected = JSON.stringify(complexObject);
324 result.push(function(jsonObject){
325 return jsonObject.parse(JSON.stringify(simpleObject,null,100));
327 result[result.length - 1].expected = JSON.stringify(simpleObject);
328 result.push(function(jsonObject){
329 return jsonObject.parse(JSON.stringify(complexObject,null,100));
331 result.push(function(jsonObject){
332 return jsonObject.parse(JSON.stringify(complexObject,null,100));
334 result[result.length - 1].expected = JSON.stringify(complexObject);
335 result.push(function(jsonObject){
336 return jsonObject.parse(JSON.stringify(simpleObject,null," "));
338 result[result.length - 1].expected = JSON.stringify(simpleObject);
339 result.push(function(jsonObject){
340 return jsonObject.parse(JSON.stringify(complexObject,null," "));
342 result.push(function(jsonObject){
343 return jsonObject.parse(JSON.stringify(complexObject,null," "));
345 result[result.length - 1].expected = JSON.stringify(complexObject);
347 result.push(function(jsonObject){
348 return jsonObject.parse(JSON.stringify(simpleObject,null,"\t"));
350 result[result.length - 1].expected = JSON.stringify(simpleObject);
351 result.push(function(jsonObject){
352 return jsonObject.parse(JSON.stringify(complexObject,null,"\t"));
354 result.push(function(jsonObject){
355 return jsonObject.parse(JSON.stringify(complexObject,null,"\t"));
357 result[result.length - 1].expected = JSON.stringify(complexObject);
359 result.push(function(jsonObject){
360 return jsonObject.parse(JSON.stringify(simpleObject,null,"\n"));
362 result[result.length - 1].expected = JSON.stringify(simpleObject);
363 result.push(function(jsonObject){
364 return jsonObject.parse(JSON.stringify(complexObject,null,"\n"));
366 result[result.length - 1].expected = JSON.stringify(complexObject);
367 function log(key, value) {
370 o.keyType = typeof key;
373 result.push(function(jsonObject){
374 return jsonObject.parse("true", log);
376 result.push(function(jsonObject){
377 return jsonObject.parse("false", log);
379 result.push(function(jsonObject){
380 return jsonObject.parse("null", log);
382 result.push(function(jsonObject){
383 return jsonObject.parse("1", log);
385 result.push(function(jsonObject){
386 return jsonObject.parse("1.5", log);
388 result.push(function(jsonObject){
389 return jsonObject.parse('"a string"', log);
391 result.push(function(jsonObject){
392 return jsonObject.parse(JSON.stringify(simpleArray), log);
394 result.push(function(jsonObject){
395 return jsonObject.parse(JSON.stringify(complexArray), log);
397 result.push(function(jsonObject){
398 return jsonObject.parse(JSON.stringify(simpleObject), log);
400 result.push(function(jsonObject){
401 return jsonObject.parse(JSON.stringify(complexObject), log);
403 result.push(function(jsonObject){
404 return jsonObject.parse('{"__proto__":{"a":5}}', log);
407 function logOrder(key, value) {
408 logOrderString += key +":"+JSON.stringify(value);
411 result.push(function(jsonObject){
413 return jsonObject.parse("true", logOrder);
415 result.push(function(jsonObject){
417 return jsonObject.parse("false", logOrder);
419 result.push(function(jsonObject){
421 return jsonObject.parse("null", logOrder);
423 result.push(function(jsonObject){
425 return jsonObject.parse("1", logOrder);
427 result.push(function(jsonObject){
429 return jsonObject.parse("1.5", logOrder);
431 result.push(function(jsonObject){
433 return jsonObject.parse('"a string"', logOrder);
435 result.push(function(jsonObject){
437 return jsonObject.parse(JSON.stringify(simpleArray), logOrder);
439 result.push(function(jsonObject){
441 return jsonObject.parse(JSON.stringify(complexArray), logOrder);
443 result.push(function(jsonObject){
445 return jsonObject.parse(JSON.stringify(simpleObject), logOrder);
447 result.push(function(jsonObject){
449 return jsonObject.parse(JSON.stringify(complexObject), logOrder);
451 result.push(function(jsonObject){
453 jsonObject.parse("true", logOrder);
454 return logOrderString;
456 result.push(function(jsonObject){
458 jsonObject.parse("false", logOrder);
459 return logOrderString;
461 result.push(function(jsonObject){
463 jsonObject.parse("null", logOrder);
464 return logOrderString;
466 result.push(function(jsonObject){
468 jsonObject.parse("1", logOrder);
469 return logOrderString;
471 result.push(function(jsonObject){
473 jsonObject.parse("1.5", logOrder);
474 return logOrderString;
476 result.push(function(jsonObject){
478 jsonObject.parse('"a string"', logOrder);
479 return logOrderString;
481 result.push(function(jsonObject){
483 jsonObject.parse(JSON.stringify(simpleArray), logOrder);
484 return logOrderString;
486 result.push(function(jsonObject){
488 jsonObject.parse(JSON.stringify(complexArray), logOrder);
489 return logOrderString;
491 result.push(function(jsonObject){
493 jsonObject.parse(JSON.stringify(simpleObject), logOrder);
494 return logOrderString;
496 result.push(function(jsonObject){
498 jsonObject.parse(JSON.stringify(complexObject), logOrder);
499 return logOrderString;
502 function throwAfterFifthCall(key, value) {
503 logOrder(key, value);
505 throw "from reviver";
508 result.push(function(jsonObject){
511 return jsonObject.parse(JSON.stringify(complexArray), throwAfterFifthCall);
513 result[result.length - 1].throws = true;
514 result.push(function(jsonObject){
517 return jsonObject.parse(JSON.stringify(simpleObject), throwAfterFifthCall);
519 result.push(function(jsonObject){
522 return jsonObject.parse(JSON.stringify(complexObject), throwAfterFifthCall);
524 result[result.length - 1].throws = true;
525 result.push(function(jsonObject){
528 try { jsonObject.parse(JSON.stringify(complexArray), throwAfterFifthCall); } catch (e) {}
529 return logOrderString;
531 result.push(function(jsonObject){
534 try { jsonObject.parse(JSON.stringify(simpleObject), throwAfterFifthCall); } catch (e) {}
535 return logOrderString;
537 result.push(function(jsonObject){
540 try { jsonObject.parse(JSON.stringify(complexObject), throwAfterFifthCall); } catch (e) {}
541 return logOrderString;
544 for (var i = 0; i < 1<<16; i++)
545 unicode += String.fromCharCode(i);
546 result.push(function(jsonObject){
547 return jsonObject.parse(JSON.stringify(unicode));
549 result[result.length - 1].unstringifiedExpected = unicode;
552 var tests = createTests();
553 for (var i = 0; i < tests.length; i++) {
556 if (tests[i].throws) {
557 shouldThrow('tests[i](nativeJSON)');
565 debug("json2.js did not throw for a test we expect to throw.");
566 } else if (tests[i].expected)
567 try { shouldBe('JSON.stringify(tests[i](nativeJSON))', "tests[i].expected") } catch(e) { debug("threw - " + e)}
568 else if (tests[i].unstringifiedExpected)
569 try { shouldBe('tests[i](nativeJSON)', "tests[i].unstringifiedExpected") } catch(e) { debug("threw - " + e)}
571 try { shouldBe('JSON.stringify(tests[i](nativeJSON))', 'JSON.stringify(tests[i](JSON))') } catch(e) { debug("threw - " + e) };