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. 6assertEquals('{"x":1}', JSON.stringify({ x : 1 }, ["x", 1, "x", 1])); 7assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["x", 1, "x", 1])); 8assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["1", 1, "1", 1])); 9assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, [1, "1", 1, "1"])); 10 11// Properties are visited at most once. 12var fired = 0; 13var getter_obj = { get x() { fired++; return 2; } }; 14assertEquals('{"x":2}', JSON.stringify(getter_obj, ["x", "y", "x"])); 15assertEquals(1, fired); 16 17// Order of the replacer array is followed. 18assertEquals('{"y":4,"x":3}', JSON.stringify({ x : 3, y : 4}, ["y", "x"])); 19assertEquals('{"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. 24var a = { x : 8 }; 25assertEquals('{"__proto__":{"__proto__":null},"x":8}', 26 JSON.stringify(a, ["__proto__", "x", "__proto__"])); 27a.__proto__ = { x : 7 }; 28assertEquals('{"__proto__":{"__proto__":{"__proto__":null},"x":7},"x":8}', 29 JSON.stringify(a, ["__proto__", "x"])); 30var b = { __proto__: { x: 9 } }; 31assertEquals('{}', JSON.stringify(b)); 32assertEquals('{"x":9}', JSON.stringify(b, ["x"])); 33var c = {x: 10}; 34Object.defineProperty(c, 'x', { enumerable: false }); 35assertEquals('{}', JSON.stringify(c)); 36assertEquals('{"x":10}', JSON.stringify(c, ["x"])); 37 38// Arrays are not affected by the replacer array. 39assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1])); 40var mixed_arr = [11,12,13]; 41mixed_arr.x = 10; 42assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1])); 43 44// Array elements of objects are affected. 45var mixed_obj = { x : 3 }; 46mixed_obj[0] = 6; 47mixed_obj[1] = 5; 48assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1])); 49 50// Nested object. 51assertEquals('{"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. 55assertEquals('{}', 56 JSON.stringify({ x : 1, "1": 1 }, [{}])); 57assertEquals('{}', 58 JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null])); 59assertEquals('{}', 60 JSON.stringify({ x : 1, "1": 1 }, 61 [{ toString: function() { return "x";} }])); 62assertEquals('{}', 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. 68assertEquals('{"toString":42}', JSON.stringify({ toString: 42 }, ["toString"])); 69 70// Number wrappers and String wrappers should be unwrapped. 71assertEquals('{"1":1,"s":"s"}', 72 JSON.stringify({ 1: 1, s: "s" }, 73 [new Number(1), new String("s")])); 74