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" })); |
| 275 |
| 276 var a = { a : 1, b : 2 }; |
| 277 delete a.a; |
| 278 assertEquals('{"b":2}', JSON.stringify(a)); |
| 279 |
| 280 var b = {}; |
| 281 b.__proto__ = { toJSON : function() { return 321;} }; |
| 282 assertEquals("321", JSON.stringify(b)); |
| 283 |
| 284 var array = [""]; |
| 285 var expected = '""'; |
| 286 for (var i = 0; i < 10000; i++) { |
| 287 array.push(""); |
| 288 expected = '"",' + expected; |
| 289 } |
| 290 expected = '[' + expected + ']'; |
| 291 assertEquals(expected, JSON.stringify(array)); |
| 292 |
260 | 293 |
261 var circular = [1, 2, 3]; | 294 var circular = [1, 2, 3]; |
262 circular[2] = circular; | 295 circular[2] = circular; |
263 assertThrows(function () { JSON.stringify(circular); }, TypeError); | 296 assertThrows(function () { JSON.stringify(circular); }, TypeError); |
264 | 297 |
265 var singleton = []; | 298 var singleton = []; |
266 var multiOccurrence = [singleton, singleton, singleton]; | 299 var multiOccurrence = [singleton, singleton, singleton]; |
267 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); | 300 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); |
268 | 301 |
269 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); | 302 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 // objects in JSON.parse. Instead we read them as we would JS object | 454 // 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. | 455 // literals. If we change that, this test should change with it. |
423 // | 456 // |
424 // Parse a non-object value as __proto__. This must not create a | 457 // Parse a non-object value as __proto__. This must not create a |
425 // __proto__ property different from the original, and should not | 458 // __proto__ property different from the original, and should not |
426 // change the original. | 459 // change the original. |
427 var o = JSON.parse('{"__proto__":5}'); | 460 var o = JSON.parse('{"__proto__":5}'); |
428 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. | 461 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. |
429 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. | 462 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. |
430 | 463 |
431 | |
432 | |
OLD | NEW |