Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 28 | // Date toJSON |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 29 | assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); |
| 30 | assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON()); |
| 31 | assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJSON()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | var n1 = new Date(10000); |
| 33 | n1.toISOString = function () { return "foo"; }; |
| 34 | assertEquals("foo", n1.toJSON()); |
| 35 | var n2 = new Date(10001); |
| 36 | n2.toISOString = null; |
| 37 | assertThrows(function () { n2.toJSON(); }, TypeError); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | var n4 = new Date(10003); |
| 39 | n4.toISOString = function () { |
| 40 | assertEquals(0, arguments.length); |
| 41 | assertEquals(this, n4); |
| 42 | return null; |
| 43 | }; |
| 44 | assertEquals(null, n4.toJSON()); |
| 45 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 46 | assertTrue(Object.prototype === JSON.__proto__); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | assertEquals("[object JSON]", Object.prototype.toString.call(JSON)); |
| 48 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 49 | //Test Date.prototype.toJSON as generic function. |
| 50 | var d1 = {toJSON: Date.prototype.toJSON, |
| 51 | toISOString: function() { return 42; }}; |
| 52 | assertEquals(42, d1.toJSON()); |
| 53 | |
| 54 | var d2 = {toJSON: Date.prototype.toJSON, |
| 55 | valueOf: function() { return Infinity; }, |
| 56 | toISOString: function() { return 42; }}; |
| 57 | assertEquals(null, d2.toJSON()); |
| 58 | |
| 59 | var d3 = {toJSON: Date.prototype.toJSON, |
| 60 | valueOf: "not callable", |
| 61 | toString: function() { return Infinity; }, |
| 62 | toISOString: function() { return 42; }}; |
| 63 | |
| 64 | assertEquals(null, d3.toJSON()); |
| 65 | |
| 66 | var d4 = {toJSON: Date.prototype.toJSON, |
| 67 | valueOf: "not callable", |
| 68 | toString: "not callable either", |
| 69 | toISOString: function() { return 42; }}; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 70 | assertThrows("d4.toJSON()", TypeError); // ToPrimitive throws. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 71 | |
| 72 | var d5 = {toJSON: Date.prototype.toJSON, |
| 73 | valueOf: "not callable", |
| 74 | toString: function() { return "Infinity"; }, |
| 75 | toISOString: function() { return 42; }}; |
| 76 | assertEquals(42, d5.toJSON()); |
| 77 | |
| 78 | var d6 = {toJSON: Date.prototype.toJSON, |
| 79 | toISOString: function() { return ["not primitive"]; }}; |
| 80 | assertEquals(["not primitive"], d6.toJSON()); |
| 81 | |
| 82 | var d7 = {toJSON: Date.prototype.toJSON, |
| 83 | ISOString: "not callable"}; |
| 84 | assertThrows("d7.toJSON()", TypeError); |
| 85 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | // DontEnum |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 87 | for (var p in this) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | assertFalse(p == "JSON"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 89 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | |
| 91 | // Parse |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | assertEquals({}, JSON.parse("{}")); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 93 | assertEquals({42:37}, JSON.parse('{"42":37}')); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | assertEquals(null, JSON.parse("null")); |
| 95 | assertEquals(true, JSON.parse("true")); |
| 96 | assertEquals(false, JSON.parse("false")); |
| 97 | assertEquals("foo", JSON.parse('"foo"')); |
| 98 | assertEquals("f\no", JSON.parse('"f\\no"')); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 99 | assertEquals("\b\f\n\r\t\"\u2028\/\\", |
| 100 | JSON.parse('"\\b\\f\\n\\r\\t\\"\\u2028\\/\\\\"')); |
| 101 | assertEquals([1.1], JSON.parse("[1.1]")); |
| 102 | assertEquals([1], JSON.parse("[1.0]")); |
| 103 | |
| 104 | assertEquals(0, JSON.parse("0")); |
| 105 | assertEquals(1, JSON.parse("1")); |
| 106 | assertEquals(0.1, JSON.parse("0.1")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | assertEquals(1.1, JSON.parse("1.1")); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 108 | assertEquals(1.1, JSON.parse("1.100000")); |
| 109 | assertEquals(1.111111, JSON.parse("1.111111")); |
| 110 | assertEquals(-0, JSON.parse("-0")); |
| 111 | assertEquals(-1, JSON.parse("-1")); |
| 112 | assertEquals(-0.1, JSON.parse("-0.1")); |
| 113 | assertEquals(-1.1, JSON.parse("-1.1")); |
| 114 | assertEquals(-1.1, JSON.parse("-1.100000")); |
| 115 | assertEquals(-1.111111, JSON.parse("-1.111111")); |
| 116 | assertEquals(11, JSON.parse("1.1e1")); |
| 117 | assertEquals(11, JSON.parse("1.1e+1")); |
| 118 | assertEquals(0.11, JSON.parse("1.1e-1")); |
| 119 | assertEquals(11, JSON.parse("1.1E1")); |
| 120 | assertEquals(11, JSON.parse("1.1E+1")); |
| 121 | assertEquals(0.11, JSON.parse("1.1E-1")); |
| 122 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | assertEquals([], JSON.parse("[]")); |
| 124 | assertEquals([1], JSON.parse("[1]")); |
| 125 | assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]')); |
| 126 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 127 | assertEquals("", JSON.parse('""')); |
| 128 | assertEquals(["", "", -0, ""], JSON.parse('[ "" , "" , -0, ""]')); |
| 129 | assertEquals("", JSON.parse('""')); |
| 130 | |
| 131 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | function GetFilter(name) { |
| 133 | function Filter(key, value) { |
| 134 | return (key == name) ? undefined : value; |
| 135 | } |
| 136 | return Filter; |
| 137 | } |
| 138 | |
| 139 | var pointJson = '{"x": 1, "y": 2}'; |
| 140 | assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson)); |
| 141 | assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y'))); |
| 142 | assertEquals({'y': 2}, JSON.parse(pointJson, GetFilter('x'))); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 143 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | assertEquals([1, 2, 3], JSON.parse("[1, 2, 3]")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | |
| 146 | var array1 = JSON.parse("[1, 2, 3]", GetFilter(1)); |
| 147 | assertEquals([1, , 3], array1); |
| 148 | assertFalse(array1.hasOwnProperty(1)); // assertEquals above is not enough |
| 149 | |
| 150 | var array2 = JSON.parse("[1, 2, 3]", GetFilter(2)); |
| 151 | assertEquals([1, 2, ,], array2); |
| 152 | assertFalse(array2.hasOwnProperty(2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
| 154 | function DoubleNumbers(key, value) { |
| 155 | return (typeof value == 'number') ? 2 * value : value; |
| 156 | } |
| 157 | |
| 158 | var deepObject = '{"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}'; |
| 159 | assertEquals({"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}, |
| 160 | JSON.parse(deepObject)); |
| 161 | assertEquals({"a": {"b": 2, "c": 4}, "d": {"e": {"f": 6}}}, |
| 162 | JSON.parse(deepObject, DoubleNumbers)); |
| 163 | |
| 164 | function TestInvalid(str) { |
| 165 | assertThrows(function () { JSON.parse(str); }, SyntaxError); |
| 166 | } |
| 167 | |
| 168 | TestInvalid('abcdef'); |
| 169 | TestInvalid('isNaN()'); |
| 170 | TestInvalid('{"x": [1, 2, deepObject]}'); |
| 171 | TestInvalid('[1, [2, [deepObject], 3], 4]'); |
| 172 | TestInvalid('function () { return 0; }'); |
| 173 | |
| 174 | TestInvalid("[1, 2"); |
| 175 | TestInvalid('{"x": 3'); |
| 176 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 177 | // JavaScript number literals not valid in JSON. |
| 178 | TestInvalid('[01]'); |
| 179 | TestInvalid('[.1]'); |
| 180 | TestInvalid('[1.]'); |
| 181 | TestInvalid('[1.e1]'); |
| 182 | TestInvalid('[-.1]'); |
| 183 | TestInvalid('[-1.]'); |
| 184 | |
| 185 | // Plain invalid number literals. |
| 186 | TestInvalid('-'); |
| 187 | TestInvalid('--1'); |
| 188 | TestInvalid('-1e'); |
| 189 | TestInvalid('1e--1]'); |
| 190 | TestInvalid('1e+-1'); |
| 191 | TestInvalid('1e-+1'); |
| 192 | TestInvalid('1e++1'); |
| 193 | |
| 194 | // JavaScript string literals not valid in JSON. |
| 195 | TestInvalid("'single quote'"); // Valid JavaScript |
| 196 | TestInvalid('"\\a invalid escape"'); |
| 197 | TestInvalid('"\\v invalid escape"'); // Valid JavaScript |
| 198 | TestInvalid('"\\\' invalid escape"'); // Valid JavaScript |
| 199 | TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript |
| 200 | TestInvalid('"\\u202 invalid escape"'); |
| 201 | TestInvalid('"\\012 invalid escape"'); |
| 202 | TestInvalid('"Unterminated string'); |
| 203 | TestInvalid('"Unterminated string\\"'); |
| 204 | TestInvalid('"Unterminated string\\\\\\"'); |
| 205 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 206 | // Test bad JSON that would be good JavaScript (ES5). |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 207 | TestInvalid("{true:42}"); |
| 208 | TestInvalid("{false:42}"); |
| 209 | TestInvalid("{null:42}"); |
| 210 | TestInvalid("{'foo':42}"); |
| 211 | TestInvalid("{42:42}"); |
| 212 | TestInvalid("{0:42}"); |
| 213 | TestInvalid("{-1:42}"); |
| 214 | |
| 215 | // Test for trailing garbage detection. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 216 | TestInvalid('42 px'); |
| 217 | TestInvalid('42 .2'); |
| 218 | TestInvalid('42 2'); |
| 219 | TestInvalid('42 e1'); |
| 220 | TestInvalid('"42" ""'); |
| 221 | TestInvalid('"42" ""'); |
| 222 | TestInvalid('"" ""'); |
| 223 | TestInvalid('true ""'); |
| 224 | TestInvalid('false ""'); |
| 225 | TestInvalid('null ""'); |
| 226 | TestInvalid('null ""'); |
| 227 | TestInvalid('[] ""'); |
| 228 | TestInvalid('[true] ""'); |
| 229 | TestInvalid('{} ""'); |
| 230 | TestInvalid('{"x":true} ""'); |
| 231 | TestInvalid('"Garbage""After string"'); |
| 232 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | // Stringify |
| 234 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | function TestStringify(expected, input) { |
| 236 | assertEquals(expected, JSON.stringify(input)); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 237 | assertEquals(expected, JSON.stringify(input, (key, value) => value)); |
| 238 | assertEquals(JSON.stringify(input, null, "="), |
| 239 | JSON.stringify(input, (key, value) => value, "=")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 242 | TestStringify("true", true); |
| 243 | TestStringify("false", false); |
| 244 | TestStringify("null", null); |
| 245 | TestStringify("false", {toJSON: function () { return false; }}); |
| 246 | TestStringify("4", 4); |
| 247 | TestStringify('"foo"', "foo"); |
| 248 | TestStringify("null", Infinity); |
| 249 | TestStringify("null", -Infinity); |
| 250 | TestStringify("null", NaN); |
| 251 | TestStringify("4", new Number(4)); |
| 252 | TestStringify('"bar"', new String("bar")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 253 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | TestStringify('"foo\\u0000bar"', "foo\0bar"); |
| 255 | TestStringify('"f\\"o\'o\\\\b\\ba\\fr\\nb\\ra\\tz"', |
| 256 | "f\"o\'o\\b\ba\fr\nb\ra\tz"); |
| 257 | |
| 258 | TestStringify("[1,2,3]", [1, 2, 3]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 1)); |
| 260 | assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 2)); |
| 261 | assertEquals("[\n 1,\n 2,\n 3\n]", |
| 262 | JSON.stringify([1, 2, 3], null, new Number(2))); |
| 263 | assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^")); |
| 264 | assertEquals("[\n^1,\n^2,\n^3\n]", |
| 265 | JSON.stringify([1, 2, 3], null, new String("^"))); |
| 266 | assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", |
| 267 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); |
| 268 | assertEquals("[]", JSON.stringify([], null, 1)); |
| 269 | assertEquals("[1,2,[3,[4],5],6,7]", |
| 270 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); |
| 271 | assertEquals("[2,4,[6,[8],10],12,14]", |
| 272 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | TestStringify('["a","ab","abc"]', ["a","ab","abc"]); |
| 274 | TestStringify('{"a":1,"c":true}', { a : 1, |
| 275 | b : function() { 1 }, |
| 276 | c : true, |
| 277 | d : function() { 2 } }); |
| 278 | TestStringify('[1,null,true,null]', |
| 279 | [1, function() { 1 }, true, function() { 2 }]); |
| 280 | TestStringify('"toJSON 123"', |
| 281 | { toJSON : function() { return 'toJSON 123'; } }); |
| 282 | TestStringify('{"a":321}', |
| 283 | { a : { toJSON : function() { return 321; } } }); |
| 284 | var counter = 0; |
| 285 | assertEquals('{"getter":123}', |
| 286 | JSON.stringify({ get getter() { counter++; return 123; } })); |
| 287 | assertEquals(1, counter); |
| 288 | assertEquals('{"getter":123}', |
| 289 | JSON.stringify({ get getter() { counter++; return 123; } }, |
| 290 | null, |
| 291 | 0)); |
| 292 | assertEquals(2, counter); |
| 293 | |
| 294 | TestStringify('{"a":"abc","b":"\u1234bc"}', |
| 295 | { a : "abc", b : "\u1234bc" }); |
| 296 | |
| 297 | |
| 298 | var a = { a : 1, b : 2 }; |
| 299 | delete a.a; |
| 300 | TestStringify('{"b":2}', a); |
| 301 | |
| 302 | var b = {}; |
| 303 | b.__proto__ = { toJSON : function() { return 321;} }; |
| 304 | TestStringify("321", b); |
| 305 | |
| 306 | var array = [""]; |
| 307 | var expected = '""'; |
| 308 | for (var i = 0; i < 10000; i++) { |
| 309 | array.push(""); |
| 310 | expected = '"",' + expected; |
| 311 | } |
| 312 | expected = '[' + expected + ']'; |
| 313 | TestStringify(expected, array); |
| 314 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | |
| 316 | var circular = [1, 2, 3]; |
| 317 | circular[2] = circular; |
| 318 | assertThrows(function () { JSON.stringify(circular); }, TypeError); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 319 | assertThrows(function () { JSON.stringify(circular, null, 0); }, TypeError); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | |
| 321 | var singleton = []; |
| 322 | var multiOccurrence = [singleton, singleton, singleton]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | TestStringify("[[],[],[]]", multiOccurrence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 325 | TestStringify('{"x":5,"y":6}', {x:5,y:6}); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x'])); |
| 327 | assertEquals('{\n "a": "b",\n "c": "d"\n}', |
| 328 | JSON.stringify({a:"b",c:"d"}, null, 1)); |
| 329 | assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x'])); |
| 330 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 331 | // toJSON get string keys. |
| 332 | var checker = {}; |
| 333 | var array = [checker]; |
| 334 | checker.toJSON = function(key) { return 1 + key; }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 335 | TestStringify('["10"]', array); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 336 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 337 | // The gap is capped at ten characters if specified as string. |
| 338 | assertEquals('{\n "a": "b",\n "c": "d"\n}', |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 339 | JSON.stringify({a:"b",c:"d"}, null, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 340 | " /*characters after 10th*/")); |
| 341 | |
| 342 | //The gap is capped at ten characters if specified as number. |
| 343 | assertEquals('{\n "a": "b",\n "c": "d"\n}', |
| 344 | JSON.stringify({a:"b",c:"d"}, null, 15)); |
| 345 | |
| 346 | // Replaced wrapped primitives are unwrapped. |
| 347 | function newx(k, v) { return (k == "x") ? new v(42) : v; } |
| 348 | assertEquals('{"x":"42"}', JSON.stringify({x: String}, newx)); |
| 349 | assertEquals('{"x":42}', JSON.stringify({x: Number}, newx)); |
| 350 | assertEquals('{"x":true}', JSON.stringify({x: Boolean}, newx)); |
| 351 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 352 | TestStringify(undefined, undefined); |
| 353 | TestStringify(undefined, function () { }); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 354 | // Arrays with missing, undefined or function elements have those elements |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 355 | // replaced by null. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 356 | TestStringify("[null,null,null]", [undefined,,function(){}]); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 357 | |
| 358 | // Objects with undefined or function properties (including replaced properties) |
| 359 | // have those properties ignored. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 360 | assertEquals('{}', |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 361 | JSON.stringify({a: undefined, b: function(){}, c: 42, d: 42}, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 362 | function(k, v) { if (k == "c") return undefined; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 363 | if (k == "d") return function(){}; |
| 364 | return v; })); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 366 | TestInvalid('1); throw "foo"; (1'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | |
| 368 | var x = 0; |
| 369 | eval("(1); x++; (1)"); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 370 | TestInvalid('1); x++; (1'); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 371 | |
| 372 | // Test string conversion of argument. |
| 373 | var o = { toString: function() { return "42"; } }; |
| 374 | assertEquals(42, JSON.parse(o)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 375 | |
| 376 | |
| 377 | for (var i = 0; i < 65536; i++) { |
| 378 | var string = String.fromCharCode(i); |
| 379 | var encoded = JSON.stringify(string); |
| 380 | var expected = "uninitialized"; |
| 381 | // Following the ES5 specification of the abstraction function Quote. |
| 382 | if (string == '"' || string == '\\') { |
| 383 | // Step 2.a |
| 384 | expected = '\\' + string; |
| 385 | } else if ("\b\t\n\r\f".indexOf(string) >= 0) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 386 | // Step 2.b |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 387 | if (string == '\b') expected = '\\b'; |
| 388 | else if (string == '\t') expected = '\\t'; |
| 389 | else if (string == '\n') expected = '\\n'; |
| 390 | else if (string == '\f') expected = '\\f'; |
| 391 | else if (string == '\r') expected = '\\r'; |
| 392 | } else if (i < 32) { |
| 393 | // Step 2.c |
| 394 | if (i < 16) { |
| 395 | expected = "\\u000" + i.toString(16); |
| 396 | } else { |
| 397 | expected = "\\u00" + i.toString(16); |
| 398 | } |
| 399 | } else { |
| 400 | expected = string; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 401 | } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 402 | assertEquals('"' + expected + '"', encoded, "Codepoint " + i); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
| 406 | // Ensure that wrappers and callables are handled correctly. |
| 407 | var num37 = new Number(42); |
| 408 | num37.valueOf = function() { return 37; }; |
| 409 | |
| 410 | var numFoo = new Number(42); |
| 411 | numFoo.valueOf = "not callable"; |
| 412 | numFoo.toString = function() { return "foo"; }; |
| 413 | |
| 414 | var numTrue = new Number(42); |
| 415 | numTrue.valueOf = function() { return true; } |
| 416 | |
| 417 | var strFoo = new String("bar"); |
| 418 | strFoo.toString = function() { return "foo"; }; |
| 419 | |
| 420 | var str37 = new String("bar"); |
| 421 | str37.toString = "not callable"; |
| 422 | str37.valueOf = function() { return 37; }; |
| 423 | |
| 424 | var strTrue = new String("bar"); |
| 425 | strTrue.toString = function() { return true; } |
| 426 | |
| 427 | var func = function() { /* Is callable */ }; |
| 428 | |
| 429 | var funcJSON = function() { /* Is callable */ }; |
| 430 | funcJSON.toJSON = function() { return "has toJSON"; }; |
| 431 | |
| 432 | var re = /Is callable/; |
| 433 | |
| 434 | var reJSON = /Is callable/; |
| 435 | reJSON.toJSON = function() { return "has toJSON"; }; |
| 436 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 437 | TestStringify('[37,null,1,"foo","37","true",null,"has toJSON",{},"has toJSON"]', |
| 438 | [num37, numFoo, numTrue, |
| 439 | strFoo, str37, strTrue, |
| 440 | func, funcJSON, re, reJSON]); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 441 | |
| 442 | |
| 443 | var oddball = Object(42); |
| 444 | oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 445 | TestStringify('1', oddball); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 446 | |
| 447 | var getCount = 0; |
| 448 | var callCount = 0; |
| 449 | var counter = { get toJSON() { getCount++; |
| 450 | return function() { callCount++; |
| 451 | return 42; }; } }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 452 | |
| 453 | // RegExps are not callable, so they are stringified as objects. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 454 | TestStringify('{}', /regexp/); |
| 455 | TestStringify('42', counter); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 456 | assertEquals(4, getCount); |
| 457 | assertEquals(4, callCount); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 458 | |
| 459 | var oddball2 = Object(42); |
| 460 | var oddball3 = Object("foo"); |
| 461 | oddball3.__proto__ = { __proto__: null, |
| 462 | toString: "not callable", |
| 463 | valueOf: function() { return true; } }; |
| 464 | oddball2.__proto__ = { __proto__: null, |
| 465 | toJSON: function () { return oddball3; } } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 466 | TestStringify('"true"', oddball2); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 467 | |
| 468 | |
| 469 | var falseNum = Object("37"); |
| 470 | falseNum.__proto__ = Number.prototype; |
| 471 | falseNum.toString = function() { return 42; }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 472 | TestStringify('"42"', falseNum); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 473 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 474 | // Parse an object value as __proto__. |
| 475 | var o1 = JSON.parse('{"__proto__":[]}'); |
| 476 | assertEquals([], o1.__proto__); |
| 477 | assertEquals(["__proto__"], Object.keys(o1)); |
| 478 | assertEquals([], Object.getOwnPropertyDescriptor(o1, "__proto__").value); |
| 479 | assertEquals(["__proto__"], Object.getOwnPropertyNames(o1)); |
| 480 | assertTrue(o1.hasOwnProperty("__proto__")); |
| 481 | assertTrue(Object.prototype.isPrototypeOf(o1)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 482 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 483 | // Parse a non-object value as __proto__. |
| 484 | var o2 = JSON.parse('{"__proto__":5}'); |
| 485 | assertEquals(5, o2.__proto__); |
| 486 | assertEquals(["__proto__"], Object.keys(o2)); |
| 487 | assertEquals(5, Object.getOwnPropertyDescriptor(o2, "__proto__").value); |
| 488 | assertEquals(["__proto__"], Object.getOwnPropertyNames(o2)); |
| 489 | assertTrue(o2.hasOwnProperty("__proto__")); |
| 490 | assertTrue(Object.prototype.isPrototypeOf(o2)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 491 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 492 | var json = '{"stuff before slash\\\\stuff after slash":"whatever"}'; |
| 493 | TestStringify(json, JSON.parse(json)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | // https://bugs.chromium.org/p/v8/issues/detail?id=3139 |
| 497 | |
| 498 | reviver = function(p, v) { |
| 499 | if (p == "a") { |
| 500 | this.b = { get x() {return null}, set x(_){throw 666} } |
| 501 | } |
| 502 | return v; |
| 503 | } |
| 504 | assertEquals({a: 0, b: {x: null}}, JSON.parse('{"a":0,"b":1}', reviver)); |
| 505 | |
| 506 | |
| 507 | // Make sure a failed [[Delete]] doesn't throw |
| 508 | |
| 509 | reviver = function(p, v) { |
| 510 | Object.freeze(this); |
| 511 | return p === "" ? v : undefined; |
| 512 | } |
| 513 | assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); |
| 514 | |
| 515 | |
| 516 | // Make sure a failed [[DefineProperty]] doesn't throw |
| 517 | |
| 518 | reviver = function(p, v) { |
| 519 | Object.freeze(this); |
| 520 | return p === "" ? v : 42; |
| 521 | } |
| 522 | assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 523 | |
| 524 | reviver = (k, v) => (v === Infinity) ? "inf" : v; |
| 525 | assertEquals('{"":"inf"}', JSON.stringify({"":Infinity}, reviver)); |