OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Properties are serialized once. | 5 // Properties are serialized once. |
6 assertEquals('{"x":1}', JSON.stringify({ x : 1 }, ["x", 1, "x", 1])); | 6 assertEquals('{"x":1}', JSON.stringify({ x : 1 }, ["x", 1, "x", 1])); |
7 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["x", 1, "x", 1])); | 7 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["x", 1, "x", 1])); |
8 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["1", 1, "1", 1])); | 8 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["1", 1, "1", 1])); |
9 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, [1, "1", 1, "1"])); | 9 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, [1, "1", 1, "1"])); |
10 | 10 |
11 // Properties are visited at most once. | 11 // Properties are visited at most once. |
12 var fired = 0; | 12 var fired = 0; |
13 var getter_obj = { get x() { fired++; return 2; } }; | 13 var getter_obj = { get x() { fired++; return 2; } }; |
14 assertEquals('{"x":2}', JSON.stringify(getter_obj, ["x", "y", "x"])); | 14 assertEquals('{"x":2}', JSON.stringify(getter_obj, ["x", "y", "x"])); |
15 assertEquals(1, fired); | 15 assertEquals(1, fired); |
16 | 16 |
17 // Order of the replacer array is followed. | 17 // Order of the replacer array is followed. |
18 assertEquals('{"y":4,"x":3}', JSON.stringify({ x : 3, y : 4}, ["y", "x"])); | 18 assertEquals('{"y":4,"x":3}', JSON.stringify({ x : 3, y : 4}, ["y", "x"])); |
19 assertEquals('{"y":4,"1":2,"x":3}', | 19 assertEquals('{"y":4,"1":2,"x":3}', |
20 JSON.stringify({ x : 3, y : 4, 1 : 2 }, ["y", 1, "x"])); | 20 JSON.stringify({ x : 3, y : 4, 1 : 2 }, ["y", 1, "x"])); |
21 | 21 |
22 // __proto__ is ignored and doesn't break anything. | 22 // With a replacer array the value of the property is retrieved using [[Get]] |
23 // ignoring own and enumerability. | |
23 var a = { x : 8 }; | 24 var a = { x : 8 }; |
25 assertEquals('{"__proto__":{"__proto__":null},"x":8}', | |
26 JSON.stringify(a, ["__proto__", "x", "__proto__"])); | |
24 a.__proto__ = { x : 7 }; | 27 a.__proto__ = { x : 7 }; |
25 assertEquals('{"x":8}', JSON.stringify(a, ["__proto__", "x", "__proto__"])); | 28 assertEquals('{"__proto__":{"__proto__":{"__proto__":null},"x":7},"x":8}', |
arv (Not doing code reviews)
2014/03/04 21:43:42
This surprised me at first so I had to dig deep in
| |
29 JSON.stringify(a, ["__proto__", "x"])); | |
30 var b = { __proto__: { x: 9 } }; | |
31 assertEquals('{}', JSON.stringify(b)); | |
32 assertEquals('{"x":9}', JSON.stringify(b, ["x"])); | |
33 var c = {x: 10}; | |
34 Object.defineProperty(c, 'x', { enumerable: false }); | |
35 assertEquals('{}', JSON.stringify(c)); | |
36 assertEquals('{"x":10}', JSON.stringify(c, ["x"])); | |
26 | 37 |
27 // Arrays are not affected by the replacer array. | 38 // Arrays are not affected by the replacer array. |
28 assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1])); | 39 assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1])); |
29 var mixed_arr = [11,12,13]; | 40 var mixed_arr = [11,12,13]; |
30 mixed_arr.x = 10; | 41 mixed_arr.x = 10; |
31 assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1])); | 42 assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1])); |
32 | 43 |
33 // Array elements of objects are affected. | 44 // Array elements of objects are affected. |
34 var mixed_obj = { x : 3 }; | 45 var mixed_obj = { x : 3 }; |
35 mixed_obj[0] = 6; | 46 mixed_obj[0] = 6; |
36 mixed_obj[1] = 5; | 47 mixed_obj[1] = 5; |
37 assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1])); | 48 assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1])); |
38 | 49 |
39 // Nested object. | 50 // Nested object. |
40 assertEquals('{"z":{"x":3},"x":1}', | 51 assertEquals('{"z":{"x":3},"x":1}', |
41 JSON.stringify({ x: 1, y:2, z: {x:3, b:4}}, ["z","x"])); | 52 JSON.stringify({ x: 1, y:2, z: {x:3, b:4}}, ["z","x"])); |
42 | 53 |
43 // Objects in the replacer array are ignored. | 54 // Objects in the replacer array are ignored. |
44 assertEquals('{}', | 55 assertEquals('{}', |
45 JSON.stringify({ x : 1, "1": 1 }, [{}])); | 56 JSON.stringify({ x : 1, "1": 1 }, [{}])); |
46 assertEquals('{}', | 57 assertEquals('{}', |
47 JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null])); | 58 JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null])); |
48 assertEquals('{}', | 59 assertEquals('{}', |
49 JSON.stringify({ x : 1, "1": 1 }, | 60 JSON.stringify({ x : 1, "1": 1 }, |
50 [{ toString: function() { return "x";} }])); | 61 [{ toString: function() { return "x";} }])); |
51 assertEquals('{}', | 62 assertEquals('{}', |
52 JSON.stringify({ x : 1, "1": 1 }, | 63 JSON.stringify({ x : 1, "1": 1 }, |
53 [{ valueOf: function() { return 1;} }])); | 64 [{ valueOf: function() { return 1;} }])); |
65 | |
66 // Make sure that property names that clash with the names of Object.prototype | |
67 // still works. | |
68 assertEquals('{"toString":42}', JSON.stringify({ toString: 42 }, ["toString"])); | |
69 | |
70 // Number wrappers and String wrappers should be unwrapped. | |
71 assertEquals('{"1":1,"s":"s"}', | |
72 JSON.stringify({ 1: 1, s: "s" }, | |
73 [new Number(1), new String("s")])); | |
OLD | NEW |