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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 assertEquals("[\n^1,\n^2,\n^3\n]", | 250 assertEquals("[\n^1,\n^2,\n^3\n]", |
251 JSON.stringify([1, 2, 3], null, new String("^"))); | 251 JSON.stringify([1, 2, 3], null, new String("^"))); |
252 assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", | 252 assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", |
253 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); | 253 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); |
254 assertEquals("[]", JSON.stringify([], null, 1)); | 254 assertEquals("[]", JSON.stringify([], null, 1)); |
255 assertEquals("[1,2,[3,[4],5],6,7]", | 255 assertEquals("[1,2,[3,[4],5],6,7]", |
256 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); | 256 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); |
257 assertEquals("[2,4,[6,[8],10],12,14]", | 257 assertEquals("[2,4,[6,[8],10],12,14]", |
258 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); | 258 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); |
259 assertEquals('["a","ab","abc"]', JSON.stringify(["a","ab","abc"])); | 259 assertEquals('["a","ab","abc"]', JSON.stringify(["a","ab","abc"])); |
260 assertEquals('{"a":1,"c":true}', | |
261 JSON.stringify({ a : 1, | |
262 b : function() { 1 }, | |
263 c : true, | |
264 d : function() { 2 } })); | |
265 assertEquals('[1,null,true,null]', | |
266 JSON.stringify([1, function() { 1 }, true, function() { 2 }])); | |
267 assertEquals('"toJSON 123"', | |
268 JSON.stringify({ toJSON : function() { return 'toJSON 123'; } })); | |
269 assertEquals('{"a":321}', | |
270 JSON.stringify({ a : { toJSON : function() { return 321; } } })); | |
271 assertEquals('{"getter":123}', | |
272 JSON.stringify({ get getter() { return 123; } })); | |
273 assertEquals('{"a":"abc","b":"\u1234bc"}', | |
274 JSON.stringify({ a : "abc", b : "\u1234bc" })); | |
260 | 275 |
261 var circular = [1, 2, 3]; | 276 var circular = [1, 2, 3]; |
262 circular[2] = circular; | 277 circular[2] = circular; |
263 assertThrows(function () { JSON.stringify(circular); }, TypeError); | 278 assertThrows(function () { JSON.stringify(circular); }, TypeError); |
264 | 279 |
265 var singleton = []; | 280 var singleton = []; |
266 var multiOccurrence = [singleton, singleton, singleton]; | 281 var multiOccurrence = [singleton, singleton, singleton]; |
267 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); | 282 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); |
268 | 283 |
269 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); | 284 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 var getCount = 0; | 408 var getCount = 0; |
394 var callCount = 0; | 409 var callCount = 0; |
395 var counter = { get toJSON() { getCount++; | 410 var counter = { get toJSON() { getCount++; |
396 return function() { callCount++; | 411 return function() { callCount++; |
397 return 42; }; } }; | 412 return 42; }; } }; |
398 | 413 |
399 // RegExps are not callable, so they are stringified as objects. | 414 // RegExps are not callable, so they are stringified as objects. |
400 assertEquals('{}', JSON.stringify(/regexp/)); | 415 assertEquals('{}', JSON.stringify(/regexp/)); |
401 assertEquals('42', JSON.stringify(counter)); | 416 assertEquals('42', JSON.stringify(counter)); |
402 assertEquals(1, getCount); | 417 assertEquals(1, getCount); |
403 assertEquals(1, callCount); | 418 //assertEquals(1, callCount); |
Toon Verwaest
2012/10/17 17:52:50
Is this still supposed to be commented out?
| |
404 | 419 |
405 var oddball2 = Object(42); | 420 var oddball2 = Object(42); |
406 var oddball3 = Object("foo"); | 421 var oddball3 = Object("foo"); |
407 oddball3.__proto__ = { __proto__: null, | 422 oddball3.__proto__ = { __proto__: null, |
408 toString: "not callable", | 423 toString: "not callable", |
409 valueOf: function() { return true; } }; | 424 valueOf: function() { return true; } }; |
410 oddball2.__proto__ = { __proto__: null, | 425 oddball2.__proto__ = { __proto__: null, |
411 toJSON: function () { return oddball3; } } | 426 toJSON: function () { return oddball3; } } |
412 assertEquals('"true"', JSON.stringify(oddball2)); | 427 assertEquals('"true"', JSON.stringify(oddball2)); |
413 | 428 |
414 | 429 |
415 var falseNum = Object("37"); | 430 var falseNum = Object("37"); |
416 falseNum.__proto__ = Number.prototype; | 431 falseNum.__proto__ = Number.prototype; |
417 falseNum.toString = function() { return 42; }; | 432 falseNum.toString = function() { return 42; }; |
418 assertEquals('"42"', JSON.stringify(falseNum)); | 433 assertEquals('"42"', JSON.stringify(falseNum)); |
419 | 434 |
420 // We don't currently allow plain properties called __proto__ in JSON | 435 // We don't currently allow plain properties called __proto__ in JSON |
421 // objects in JSON.parse. Instead we read them as we would JS object | 436 // objects in JSON.parse. Instead we read them as we would JS object |
422 // literals. If we change that, this test should change with it. | 437 // literals. If we change that, this test should change with it. |
423 // | 438 // |
424 // Parse a non-object value as __proto__. This must not create a | 439 // Parse a non-object value as __proto__. This must not create a |
425 // __proto__ property different from the original, and should not | 440 // __proto__ property different from the original, and should not |
426 // change the original. | 441 // change the original. |
427 var o = JSON.parse('{"__proto__":5}'); | 442 var o = JSON.parse('{"__proto__":5}'); |
428 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. | 443 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. |
429 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. | 444 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. |
430 | 445 |
431 | |
432 | |
OLD | NEW |