blob: 84f2056856a6526f245f22b8d05c8b2ef6c3733e [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Steve Blocka7e24c12009-10-30 11:49:00 +000028// Date toJSON
Leon Clarkee46be812010-01-19 14:06:41 +000029assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON());
30assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON());
31assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJSON());
Steve Blocka7e24c12009-10-30 11:49:00 +000032var n1 = new Date(10000);
33n1.toISOString = function () { return "foo"; };
34assertEquals("foo", n1.toJSON());
35var n2 = new Date(10001);
36n2.toISOString = null;
37assertThrows(function () { n2.toJSON(); }, TypeError);
Steve Blocka7e24c12009-10-30 11:49:00 +000038var n4 = new Date(10003);
39n4.toISOString = function () {
40 assertEquals(0, arguments.length);
41 assertEquals(this, n4);
42 return null;
43};
44assertEquals(null, n4.toJSON());
45
Ben Murdoch3bec4d22010-07-22 14:51:16 +010046assertTrue(Object.prototype === JSON.__proto__);
Steve Blocka7e24c12009-10-30 11:49:00 +000047assertEquals("[object JSON]", Object.prototype.toString.call(JSON));
48
Ben Murdochb0fe1622011-05-05 13:52:32 +010049//Test Date.prototype.toJSON as generic function.
50var d1 = {toJSON: Date.prototype.toJSON,
51 toISOString: function() { return 42; }};
52assertEquals(42, d1.toJSON());
53
54var d2 = {toJSON: Date.prototype.toJSON,
55 valueOf: function() { return Infinity; },
56 toISOString: function() { return 42; }};
57assertEquals(null, d2.toJSON());
58
59var d3 = {toJSON: Date.prototype.toJSON,
60 valueOf: "not callable",
61 toString: function() { return Infinity; },
62 toISOString: function() { return 42; }};
63
64assertEquals(null, d3.toJSON());
65
66var d4 = {toJSON: Date.prototype.toJSON,
67 valueOf: "not callable",
68 toString: "not callable either",
69 toISOString: function() { return 42; }};
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000070assertThrows("d4.toJSON()", TypeError); // ToPrimitive throws.
Ben Murdochb0fe1622011-05-05 13:52:32 +010071
72var d5 = {toJSON: Date.prototype.toJSON,
73 valueOf: "not callable",
74 toString: function() { return "Infinity"; },
75 toISOString: function() { return 42; }};
76assertEquals(42, d5.toJSON());
77
78var d6 = {toJSON: Date.prototype.toJSON,
79 toISOString: function() { return ["not primitive"]; }};
80assertEquals(["not primitive"], d6.toJSON());
81
82var d7 = {toJSON: Date.prototype.toJSON,
83 ISOString: "not callable"};
84assertThrows("d7.toJSON()", TypeError);
85
Steve Blocka7e24c12009-10-30 11:49:00 +000086// DontEnum
Ben Murdochb0fe1622011-05-05 13:52:32 +010087for (var p in this) {
Steve Blocka7e24c12009-10-30 11:49:00 +000088 assertFalse(p == "JSON");
Ben Murdochb0fe1622011-05-05 13:52:32 +010089}
Steve Blocka7e24c12009-10-30 11:49:00 +000090
91// Parse
Steve Blocka7e24c12009-10-30 11:49:00 +000092assertEquals({}, JSON.parse("{}"));
Leon Clarke4515c472010-02-03 11:58:03 +000093assertEquals({42:37}, JSON.parse('{"42":37}'));
Steve Blocka7e24c12009-10-30 11:49:00 +000094assertEquals(null, JSON.parse("null"));
95assertEquals(true, JSON.parse("true"));
96assertEquals(false, JSON.parse("false"));
97assertEquals("foo", JSON.parse('"foo"'));
98assertEquals("f\no", JSON.parse('"f\\no"'));
Leon Clarke4515c472010-02-03 11:58:03 +000099assertEquals("\b\f\n\r\t\"\u2028\/\\",
100 JSON.parse('"\\b\\f\\n\\r\\t\\"\\u2028\\/\\\\"'));
101assertEquals([1.1], JSON.parse("[1.1]"));
102assertEquals([1], JSON.parse("[1.0]"));
103
104assertEquals(0, JSON.parse("0"));
105assertEquals(1, JSON.parse("1"));
106assertEquals(0.1, JSON.parse("0.1"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000107assertEquals(1.1, JSON.parse("1.1"));
Leon Clarke4515c472010-02-03 11:58:03 +0000108assertEquals(1.1, JSON.parse("1.100000"));
109assertEquals(1.111111, JSON.parse("1.111111"));
110assertEquals(-0, JSON.parse("-0"));
111assertEquals(-1, JSON.parse("-1"));
112assertEquals(-0.1, JSON.parse("-0.1"));
113assertEquals(-1.1, JSON.parse("-1.1"));
114assertEquals(-1.1, JSON.parse("-1.100000"));
115assertEquals(-1.111111, JSON.parse("-1.111111"));
116assertEquals(11, JSON.parse("1.1e1"));
117assertEquals(11, JSON.parse("1.1e+1"));
118assertEquals(0.11, JSON.parse("1.1e-1"));
119assertEquals(11, JSON.parse("1.1E1"));
120assertEquals(11, JSON.parse("1.1E+1"));
121assertEquals(0.11, JSON.parse("1.1E-1"));
122
Steve Blocka7e24c12009-10-30 11:49:00 +0000123assertEquals([], JSON.parse("[]"));
124assertEquals([1], JSON.parse("[1]"));
125assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]'));
126
Leon Clarke4515c472010-02-03 11:58:03 +0000127assertEquals("", JSON.parse('""'));
128assertEquals(["", "", -0, ""], JSON.parse('[ "" , "" , -0, ""]'));
129assertEquals("", JSON.parse('""'));
130
131
Steve Blocka7e24c12009-10-30 11:49:00 +0000132function GetFilter(name) {
133 function Filter(key, value) {
134 return (key == name) ? undefined : value;
135 }
136 return Filter;
137}
138
139var pointJson = '{"x": 1, "y": 2}';
140assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson));
141assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y')));
142assertEquals({'y': 2}, JSON.parse(pointJson, GetFilter('x')));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143
Steve Blocka7e24c12009-10-30 11:49:00 +0000144assertEquals([1, 2, 3], JSON.parse("[1, 2, 3]"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145
146var array1 = JSON.parse("[1, 2, 3]", GetFilter(1));
147assertEquals([1, , 3], array1);
148assertFalse(array1.hasOwnProperty(1)); // assertEquals above is not enough
149
150var array2 = JSON.parse("[1, 2, 3]", GetFilter(2));
151assertEquals([1, 2, ,], array2);
152assertFalse(array2.hasOwnProperty(2));
Steve Blocka7e24c12009-10-30 11:49:00 +0000153
154function DoubleNumbers(key, value) {
155 return (typeof value == 'number') ? 2 * value : value;
156}
157
158var deepObject = '{"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}';
159assertEquals({"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}},
160 JSON.parse(deepObject));
161assertEquals({"a": {"b": 2, "c": 4}, "d": {"e": {"f": 6}}},
162 JSON.parse(deepObject, DoubleNumbers));
163
164function TestInvalid(str) {
165 assertThrows(function () { JSON.parse(str); }, SyntaxError);
166}
167
168TestInvalid('abcdef');
169TestInvalid('isNaN()');
170TestInvalid('{"x": [1, 2, deepObject]}');
171TestInvalid('[1, [2, [deepObject], 3], 4]');
172TestInvalid('function () { return 0; }');
173
174TestInvalid("[1, 2");
175TestInvalid('{"x": 3');
176
Leon Clarke4515c472010-02-03 11:58:03 +0000177// JavaScript number literals not valid in JSON.
178TestInvalid('[01]');
179TestInvalid('[.1]');
180TestInvalid('[1.]');
181TestInvalid('[1.e1]');
182TestInvalid('[-.1]');
183TestInvalid('[-1.]');
184
185// Plain invalid number literals.
186TestInvalid('-');
187TestInvalid('--1');
188TestInvalid('-1e');
189TestInvalid('1e--1]');
190TestInvalid('1e+-1');
191TestInvalid('1e-+1');
192TestInvalid('1e++1');
193
194// JavaScript string literals not valid in JSON.
195TestInvalid("'single quote'"); // Valid JavaScript
196TestInvalid('"\\a invalid escape"');
197TestInvalid('"\\v invalid escape"'); // Valid JavaScript
198TestInvalid('"\\\' invalid escape"'); // Valid JavaScript
199TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript
200TestInvalid('"\\u202 invalid escape"');
201TestInvalid('"\\012 invalid escape"');
202TestInvalid('"Unterminated string');
203TestInvalid('"Unterminated string\\"');
204TestInvalid('"Unterminated string\\\\\\"');
205
Andrei Popescu31002712010-02-23 13:46:05 +0000206// Test bad JSON that would be good JavaScript (ES5).
Leon Clarke4515c472010-02-03 11:58:03 +0000207TestInvalid("{true:42}");
208TestInvalid("{false:42}");
209TestInvalid("{null:42}");
210TestInvalid("{'foo':42}");
211TestInvalid("{42:42}");
212TestInvalid("{0:42}");
213TestInvalid("{-1:42}");
214
215// Test for trailing garbage detection.
Leon Clarke4515c472010-02-03 11:58:03 +0000216TestInvalid('42 px');
217TestInvalid('42 .2');
218TestInvalid('42 2');
219TestInvalid('42 e1');
220TestInvalid('"42" ""');
221TestInvalid('"42" ""');
222TestInvalid('"" ""');
223TestInvalid('true ""');
224TestInvalid('false ""');
225TestInvalid('null ""');
226TestInvalid('null ""');
227TestInvalid('[] ""');
228TestInvalid('[true] ""');
229TestInvalid('{} ""');
230TestInvalid('{"x":true} ""');
231TestInvalid('"Garbage""After string"');
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233// Stringify
234
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235function TestStringify(expected, input) {
236 assertEquals(expected, JSON.stringify(input));
237 assertEquals(expected, JSON.stringify(input, null, 0));
238}
Steve Blocka7e24c12009-10-30 11:49:00 +0000239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240TestStringify("true", true);
241TestStringify("false", false);
242TestStringify("null", null);
243TestStringify("false", {toJSON: function () { return false; }});
244TestStringify("4", 4);
245TestStringify('"foo"', "foo");
246TestStringify("null", Infinity);
247TestStringify("null", -Infinity);
248TestStringify("null", NaN);
249TestStringify("4", new Number(4));
250TestStringify('"bar"', new String("bar"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252TestStringify('"foo\\u0000bar"', "foo\0bar");
253TestStringify('"f\\"o\'o\\\\b\\ba\\fr\\nb\\ra\\tz"',
254 "f\"o\'o\\b\ba\fr\nb\ra\tz");
255
256TestStringify("[1,2,3]", [1, 2, 3]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000257assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 1));
258assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 2));
259assertEquals("[\n 1,\n 2,\n 3\n]",
260 JSON.stringify([1, 2, 3], null, new Number(2)));
261assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^"));
262assertEquals("[\n^1,\n^2,\n^3\n]",
263 JSON.stringify([1, 2, 3], null, new String("^")));
264assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]",
265 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1));
266assertEquals("[]", JSON.stringify([], null, 1));
267assertEquals("[1,2,[3,[4],5],6,7]",
268 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null));
269assertEquals("[2,4,[6,[8],10],12,14]",
270 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271TestStringify('["a","ab","abc"]', ["a","ab","abc"]);
272TestStringify('{"a":1,"c":true}', { a : 1,
273 b : function() { 1 },
274 c : true,
275 d : function() { 2 } });
276TestStringify('[1,null,true,null]',
277 [1, function() { 1 }, true, function() { 2 }]);
278TestStringify('"toJSON 123"',
279 { toJSON : function() { return 'toJSON 123'; } });
280TestStringify('{"a":321}',
281 { a : { toJSON : function() { return 321; } } });
282var counter = 0;
283assertEquals('{"getter":123}',
284 JSON.stringify({ get getter() { counter++; return 123; } }));
285assertEquals(1, counter);
286assertEquals('{"getter":123}',
287 JSON.stringify({ get getter() { counter++; return 123; } },
288 null,
289 0));
290assertEquals(2, counter);
291
292TestStringify('{"a":"abc","b":"\u1234bc"}',
293 { a : "abc", b : "\u1234bc" });
294
295
296var a = { a : 1, b : 2 };
297delete a.a;
298TestStringify('{"b":2}', a);
299
300var b = {};
301b.__proto__ = { toJSON : function() { return 321;} };
302TestStringify("321", b);
303
304var array = [""];
305var expected = '""';
306for (var i = 0; i < 10000; i++) {
307 array.push("");
308 expected = '"",' + expected;
309}
310expected = '[' + expected + ']';
311TestStringify(expected, array);
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313
314var circular = [1, 2, 3];
315circular[2] = circular;
316assertThrows(function () { JSON.stringify(circular); }, TypeError);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317assertThrows(function () { JSON.stringify(circular, null, 0); }, TypeError);
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
319var singleton = [];
320var multiOccurrence = [singleton, singleton, singleton];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321TestStringify("[[],[],[]]", multiOccurrence);
Steve Blocka7e24c12009-10-30 11:49:00 +0000322
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323TestStringify('{"x":5,"y":6}', {x:5,y:6});
Steve Blocka7e24c12009-10-30 11:49:00 +0000324assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x']));
325assertEquals('{\n "a": "b",\n "c": "d"\n}',
326 JSON.stringify({a:"b",c:"d"}, null, 1));
327assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));
328
Ben Murdochb0fe1622011-05-05 13:52:32 +0100329// toJSON get string keys.
330var checker = {};
331var array = [checker];
332checker.toJSON = function(key) { return 1 + key; };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333TestStringify('["10"]', array);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100334
Andrei Popescu31002712010-02-23 13:46:05 +0000335// The gap is capped at ten characters if specified as string.
336assertEquals('{\n "a": "b",\n "c": "d"\n}',
Ben Murdochb0fe1622011-05-05 13:52:32 +0100337 JSON.stringify({a:"b",c:"d"}, null,
Andrei Popescu31002712010-02-23 13:46:05 +0000338 " /*characters after 10th*/"));
339
340//The gap is capped at ten characters if specified as number.
341assertEquals('{\n "a": "b",\n "c": "d"\n}',
342 JSON.stringify({a:"b",c:"d"}, null, 15));
343
344// Replaced wrapped primitives are unwrapped.
345function newx(k, v) { return (k == "x") ? new v(42) : v; }
346assertEquals('{"x":"42"}', JSON.stringify({x: String}, newx));
347assertEquals('{"x":42}', JSON.stringify({x: Number}, newx));
348assertEquals('{"x":true}', JSON.stringify({x: Boolean}, newx));
349
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350TestStringify(undefined, undefined);
351TestStringify(undefined, function () { });
Ben Murdochb0fe1622011-05-05 13:52:32 +0100352// Arrays with missing, undefined or function elements have those elements
Andrei Popescu31002712010-02-23 13:46:05 +0000353// replaced by null.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354TestStringify("[null,null,null]", [undefined,,function(){}]);
Andrei Popescu31002712010-02-23 13:46:05 +0000355
356// Objects with undefined or function properties (including replaced properties)
357// have those properties ignored.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100358assertEquals('{}',
Andrei Popescu31002712010-02-23 13:46:05 +0000359 JSON.stringify({a: undefined, b: function(){}, c: 42, d: 42},
Ben Murdochb0fe1622011-05-05 13:52:32 +0100360 function(k, v) { if (k == "c") return undefined;
Andrei Popescu31002712010-02-23 13:46:05 +0000361 if (k == "d") return function(){};
362 return v; }));
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
Leon Clarke4515c472010-02-03 11:58:03 +0000364TestInvalid('1); throw "foo"; (1');
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
366var x = 0;
367eval("(1); x++; (1)");
Leon Clarke4515c472010-02-03 11:58:03 +0000368TestInvalid('1); x++; (1');
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100369
370// Test string conversion of argument.
371var o = { toString: function() { return "42"; } };
372assertEquals(42, JSON.parse(o));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100373
374
375for (var i = 0; i < 65536; i++) {
376 var string = String.fromCharCode(i);
377 var encoded = JSON.stringify(string);
378 var expected = "uninitialized";
379 // Following the ES5 specification of the abstraction function Quote.
380 if (string == '"' || string == '\\') {
381 // Step 2.a
382 expected = '\\' + string;
383 } else if ("\b\t\n\r\f".indexOf(string) >= 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100384 // Step 2.b
Iain Merrick9ac36c92010-09-13 15:29:50 +0100385 if (string == '\b') expected = '\\b';
386 else if (string == '\t') expected = '\\t';
387 else if (string == '\n') expected = '\\n';
388 else if (string == '\f') expected = '\\f';
389 else if (string == '\r') expected = '\\r';
390 } else if (i < 32) {
391 // Step 2.c
392 if (i < 16) {
393 expected = "\\u000" + i.toString(16);
394 } else {
395 expected = "\\u00" + i.toString(16);
396 }
397 } else {
398 expected = string;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100399 }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100400 assertEquals('"' + expected + '"', encoded, "Codepoint " + i);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100401}
402
403
404// Ensure that wrappers and callables are handled correctly.
405var num37 = new Number(42);
406num37.valueOf = function() { return 37; };
407
408var numFoo = new Number(42);
409numFoo.valueOf = "not callable";
410numFoo.toString = function() { return "foo"; };
411
412var numTrue = new Number(42);
413numTrue.valueOf = function() { return true; }
414
415var strFoo = new String("bar");
416strFoo.toString = function() { return "foo"; };
417
418var str37 = new String("bar");
419str37.toString = "not callable";
420str37.valueOf = function() { return 37; };
421
422var strTrue = new String("bar");
423strTrue.toString = function() { return true; }
424
425var func = function() { /* Is callable */ };
426
427var funcJSON = function() { /* Is callable */ };
428funcJSON.toJSON = function() { return "has toJSON"; };
429
430var re = /Is callable/;
431
432var reJSON = /Is callable/;
433reJSON.toJSON = function() { return "has toJSON"; };
434
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435TestStringify('[37,null,1,"foo","37","true",null,"has toJSON",{},"has toJSON"]',
436 [num37, numFoo, numTrue,
437 strFoo, str37, strTrue,
438 func, funcJSON, re, reJSON]);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100439
440
441var oddball = Object(42);
442oddball.__proto__ = { __proto__: null, toString: function() { return true; } };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443TestStringify('1', oddball);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100444
445var getCount = 0;
446var callCount = 0;
447var counter = { get toJSON() { getCount++;
448 return function() { callCount++;
449 return 42; }; } };
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000450
451// RegExps are not callable, so they are stringified as objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452TestStringify('{}', /regexp/);
453TestStringify('42', counter);
454assertEquals(2, getCount);
455assertEquals(2, callCount);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100456
457var oddball2 = Object(42);
458var oddball3 = Object("foo");
459oddball3.__proto__ = { __proto__: null,
460 toString: "not callable",
461 valueOf: function() { return true; } };
462oddball2.__proto__ = { __proto__: null,
463 toJSON: function () { return oddball3; } }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464TestStringify('"true"', oddball2);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100465
466
467var falseNum = Object("37");
468falseNum.__proto__ = Number.prototype;
469falseNum.toString = function() { return 42; };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470TestStringify('"42"', falseNum);
Steve Block1e0659c2011-05-24 12:43:12 +0100471
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472// Parse an object value as __proto__.
473var o1 = JSON.parse('{"__proto__":[]}');
474assertEquals([], o1.__proto__);
475assertEquals(["__proto__"], Object.keys(o1));
476assertEquals([], Object.getOwnPropertyDescriptor(o1, "__proto__").value);
477assertEquals(["__proto__"], Object.getOwnPropertyNames(o1));
478assertTrue(o1.hasOwnProperty("__proto__"));
479assertTrue(Object.prototype.isPrototypeOf(o1));
Steve Block1e0659c2011-05-24 12:43:12 +0100480
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481// Parse a non-object value as __proto__.
482var o2 = JSON.parse('{"__proto__":5}');
483assertEquals(5, o2.__proto__);
484assertEquals(["__proto__"], Object.keys(o2));
485assertEquals(5, Object.getOwnPropertyDescriptor(o2, "__proto__").value);
486assertEquals(["__proto__"], Object.getOwnPropertyNames(o2));
487assertTrue(o2.hasOwnProperty("__proto__"));
488assertTrue(Object.prototype.isPrototypeOf(o2));
Steve Block1e0659c2011-05-24 12:43:12 +0100489
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490var json = '{"stuff before slash\\\\stuff after slash":"whatever"}';
491TestStringify(json, JSON.parse(json));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492
493
494// https://bugs.chromium.org/p/v8/issues/detail?id=3139
495
496reviver = function(p, v) {
497 if (p == "a") {
498 this.b = { get x() {return null}, set x(_){throw 666} }
499 }
500 return v;
501}
502assertEquals({a: 0, b: {x: null}}, JSON.parse('{"a":0,"b":1}', reviver));
503
504
505// Make sure a failed [[Delete]] doesn't throw
506
507reviver = function(p, v) {
508 Object.freeze(this);
509 return p === "" ? v : undefined;
510}
511assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver));
512
513
514// Make sure a failed [[DefineProperty]] doesn't throw
515
516reviver = function(p, v) {
517 Object.freeze(this);
518 return p === "" ? v : 42;
519}
520assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver));