| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Properties are serialized once. | |
| 6 assertEquals('{"x":1}', JSON.stringify({ x : 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])); | |
| 9 assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, [1, "1", 1, "1"])); | |
| 10 | |
| 11 // Properties are visited at most once. | |
| 12 var fired = 0; | |
| 13 var getter_obj = { get x() { fired++; return 2; } }; | |
| 14 assertEquals('{"x":2}', JSON.stringify(getter_obj, ["x", "y", "x"])); | |
| 15 assertEquals(1, fired); | |
| 16 | |
| 17 // Order of the replacer array is followed. | |
| 18 assertEquals('{"y":4,"x":3}', JSON.stringify({ x : 3, y : 4}, ["y", "x"])); | |
| 19 assertEquals('{"y":4,"1":2,"x":3}', | |
| 20 JSON.stringify({ x : 3, y : 4, 1 : 2 }, ["y", 1, "x"])); | |
| 21 | |
| 22 // __proto__ is ignored and doesn't break anything. | |
| 23 var a = { x : 8 }; | |
| 24 a.__proto__ = { x : 7 }; | |
| 25 assertEquals('{"x":8}', JSON.stringify(a, ["__proto__", "x", "__proto__"])); | |
| 26 | |
| 27 // Arrays are not affected by the replacer array. | |
| 28 assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1])); | |
| 29 var mixed_arr = [11,12,13]; | |
| 30 mixed_arr.x = 10; | |
| 31 assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1])); | |
| 32 | |
| 33 // Array elements of objects are affected. | |
| 34 var mixed_obj = { x : 3 }; | |
| 35 mixed_obj[0] = 6; | |
| 36 mixed_obj[1] = 5; | |
| 37 assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1])); | |
| 38 | |
| 39 // Nested object. | |
| 40 assertEquals('{"z":{"x":3},"x":1}', | |
| 41 JSON.stringify({ x: 1, y:2, z: {x:3, b:4}}, ["z","x"])); | |
| 42 | |
| 43 // Objects in the replacer array are ignored. | |
| 44 assertEquals('{}', | |
| 45 JSON.stringify({ x : 1, "1": 1 }, [{}])); | |
| 46 assertEquals('{}', | |
| 47 JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null])); | |
| 48 assertEquals('{}', | |
| 49 JSON.stringify({ x : 1, "1": 1 }, | |
| 50 [{ toString: function() { return "x";} }])); | |
| 51 assertEquals('{}', | |
| 52 JSON.stringify({ x : 1, "1": 1 }, | |
| 53 [{ valueOf: function() { return 1;} }])); | |
| OLD | NEW |