| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 TestInvalid('[] ""'); | 227 TestInvalid('[] ""'); |
| 228 TestInvalid('[true] ""'); | 228 TestInvalid('[true] ""'); |
| 229 TestInvalid('{} ""'); | 229 TestInvalid('{} ""'); |
| 230 TestInvalid('{"x":true} ""'); | 230 TestInvalid('{"x":true} ""'); |
| 231 TestInvalid('"Garbage""After string"'); | 231 TestInvalid('"Garbage""After string"'); |
| 232 | 232 |
| 233 // Stringify | 233 // Stringify |
| 234 | 234 |
| 235 function TestStringify(expected, input) { | 235 function TestStringify(expected, input) { |
| 236 assertEquals(expected, JSON.stringify(input)); | 236 assertEquals(expected, JSON.stringify(input)); |
| 237 assertEquals(expected, JSON.stringify(input, null, 0)); | 237 assertEquals(expected, JSON.stringify(input, (key, value) => value)); |
| 238 assertEquals(JSON.stringify(input, null, "="), |
| 239 JSON.stringify(input, (key, value) => value, "=")); |
| 238 } | 240 } |
| 239 | 241 |
| 240 TestStringify("true", true); | 242 TestStringify("true", true); |
| 241 TestStringify("false", false); | 243 TestStringify("false", false); |
| 242 TestStringify("null", null); | 244 TestStringify("null", null); |
| 243 TestStringify("false", {toJSON: function () { return false; }}); | 245 TestStringify("false", {toJSON: function () { return false; }}); |
| 244 TestStringify("4", 4); | 246 TestStringify("4", 4); |
| 245 TestStringify('"foo"', "foo"); | 247 TestStringify('"foo"', "foo"); |
| 246 TestStringify("null", Infinity); | 248 TestStringify("null", Infinity); |
| 247 TestStringify("null", -Infinity); | 249 TestStringify("null", -Infinity); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 446 |
| 445 var getCount = 0; | 447 var getCount = 0; |
| 446 var callCount = 0; | 448 var callCount = 0; |
| 447 var counter = { get toJSON() { getCount++; | 449 var counter = { get toJSON() { getCount++; |
| 448 return function() { callCount++; | 450 return function() { callCount++; |
| 449 return 42; }; } }; | 451 return 42; }; } }; |
| 450 | 452 |
| 451 // RegExps are not callable, so they are stringified as objects. | 453 // RegExps are not callable, so they are stringified as objects. |
| 452 TestStringify('{}', /regexp/); | 454 TestStringify('{}', /regexp/); |
| 453 TestStringify('42', counter); | 455 TestStringify('42', counter); |
| 454 assertEquals(2, getCount); | 456 assertEquals(4, getCount); |
| 455 assertEquals(2, callCount); | 457 assertEquals(4, callCount); |
| 456 | 458 |
| 457 var oddball2 = Object(42); | 459 var oddball2 = Object(42); |
| 458 var oddball3 = Object("foo"); | 460 var oddball3 = Object("foo"); |
| 459 oddball3.__proto__ = { __proto__: null, | 461 oddball3.__proto__ = { __proto__: null, |
| 460 toString: "not callable", | 462 toString: "not callable", |
| 461 valueOf: function() { return true; } }; | 463 valueOf: function() { return true; } }; |
| 462 oddball2.__proto__ = { __proto__: null, | 464 oddball2.__proto__ = { __proto__: null, |
| 463 toJSON: function () { return oddball3; } } | 465 toJSON: function () { return oddball3; } } |
| 464 TestStringify('"true"', oddball2); | 466 TestStringify('"true"', oddball2); |
| 465 | 467 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); | 513 assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); |
| 512 | 514 |
| 513 | 515 |
| 514 // Make sure a failed [[DefineProperty]] doesn't throw | 516 // Make sure a failed [[DefineProperty]] doesn't throw |
| 515 | 517 |
| 516 reviver = function(p, v) { | 518 reviver = function(p, v) { |
| 517 Object.freeze(this); | 519 Object.freeze(this); |
| 518 return p === "" ? v : 42; | 520 return p === "" ? v : 42; |
| 519 } | 521 } |
| 520 assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); | 522 assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver)); |
| OLD | NEW |