Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | // With a replacer array the value of the property is retrieved using [[Get]] |
| 23 | // ignoring own and enumerability. |
| 24 | var a = { x : 8 }; |
| 25 | assertEquals('{"__proto__":{"__proto__":null},"x":8}', |
| 26 | JSON.stringify(a, ["__proto__", "x", "__proto__"])); |
| 27 | a.__proto__ = { x : 7 }; |
| 28 | assertEquals('{"__proto__":{"__proto__":{"__proto__":null},"x":7},"x":8}', |
| 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"])); |
| 37 | |
| 38 | // Arrays are not affected by the replacer array. |
| 39 | assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1])); |
| 40 | var mixed_arr = [11,12,13]; |
| 41 | mixed_arr.x = 10; |
| 42 | assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1])); |
| 43 | |
| 44 | // Array elements of objects are affected. |
| 45 | var mixed_obj = { x : 3 }; |
| 46 | mixed_obj[0] = 6; |
| 47 | mixed_obj[1] = 5; |
| 48 | assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1])); |
| 49 | |
| 50 | // Nested object. |
| 51 | assertEquals('{"z":{"x":3},"x":1}', |
| 52 | JSON.stringify({ x: 1, y:2, z: {x:3, b:4}}, ["z","x"])); |
| 53 | |
| 54 | // Objects in the replacer array are ignored. |
| 55 | assertEquals('{}', |
| 56 | JSON.stringify({ x : 1, "1": 1 }, [{}])); |
| 57 | assertEquals('{}', |
| 58 | JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null])); |
| 59 | assertEquals('{}', |
| 60 | JSON.stringify({ x : 1, "1": 1 }, |
| 61 | [{ toString: function() { return "x";} }])); |
| 62 | assertEquals('{}', |
| 63 | JSON.stringify({ x : 1, "1": 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")])); |