forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoJSON.js
More file actions
190 lines (159 loc) · 4.77 KB
/
toJSON.js
File metadata and controls
190 lines (159 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
var TEST = function(a, b) {
if (a != b) {
throw new Error(a + " != " + b);
}
}
var fnc = function(n) { this.number = n };
fnc.prototype.toJSON = function() {
return this.number.toString();
}
// test getter - no-enum
{
var obj = {};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: false });
obj.a = 1;
obj.b = 2;
obj.d = 4;
TEST('{"a":1,"b":2,"d":4}', JSON.stringify(obj));
}
// test getter - no-enum - delete
{
var obj = {};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: false, configurable: true });
obj.a = 1;
obj.b = 2;
delete obj['c']
TEST('{"a":1,"b":2}', JSON.stringify(obj));;
Object.defineProperty(obj, "c", { get: () => 3, enumerable: false });
obj.d = 4;
TEST('{"a":1,"b":2,"d":4}', JSON.stringify(obj));
}
// test getter - enum - delete 'em all
{
var obj = {};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: true, configurable: true });
obj.a = 1;
obj.b = 2;
delete obj['c']
TEST('{"a":1,"b":2}', JSON.stringify(obj));;
Object.defineProperty(obj, "c", { get: () => 3, enumerable: true });
obj.d = 4;
TEST('{"c":3,"a":1,"b":2,"d":4}', JSON.stringify(obj));
obj[9]=19;
TEST('{"9":19,"c":3,"a":1,"b":2,"d":4}', JSON.stringify(obj));
delete obj[9]
TEST('{"c":3,"a":1,"b":2,"d":4}', JSON.stringify(obj));
delete obj['a']
TEST('{"c":3,"b":2,"d":4}', JSON.stringify(obj));
}
// test getter
{
var obj = {a:1, b:2};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: true });
obj.d = 4;
TEST('{"a":1,"b":2,"c":3,"d":4}', JSON.stringify(obj));;
}
// test getter - setter
{
var obj = {a:1, b:2};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: true }); // should print
Object.defineProperty(obj, "d", { set: () => 4, enumerable: true }); // skips undefined
obj.e = 5;
TEST('{"a":1,"b":2,"c":3,"e":5}', JSON.stringify(obj));
}
// test getter with revive
{
function rev(name, value) {
if (value == 3) {
return undefined;
}
else if (value == true) {
return 99;
}
return value;
}
var obj = {a:1, b:2};
Object.defineProperty(obj, "c", { get: () => 3, enumerable: true });
obj.d = 4;
TEST('{"a":99,"b":2,"d":4}', JSON.stringify( JSON.parse(JSON.stringify(obj), rev) ));
}
// test getter with revive && objectArray
{
function rev(name, value) {
if (value == 3) {
return undefined;
}
else if (value == true) {
return 99;
}
return value;
}
var obj = {a:1, b:2, q:3};
obj[9] = 9;
obj[44] = 44;
Object.defineProperty(obj, "c", { get: () => 7, enumerable: true });
obj.d = 4;
obj = JSON.parse(JSON.stringify(obj), rev);
TEST('{"9":9,"44":44,"a":99,"b":2,"c":7,"d":4}', JSON.stringify( obj ));
}
// test getter with revive && objectArray && no-enum
{
function rev(name, value) {
if (value == 9) {
return undefined;
}
else if (value == true) {
return 99;
}
return value;
}
var obj = {a:1, b:2};
obj[7] = 8;
obj[9] = 9;
obj[44] = 44;
Object.defineProperty(obj, "c", { get: () => 3, enumerable: false });
obj.d = 4;
obj = JSON.parse(JSON.stringify(obj), rev);
TEST('{"7":8,"44":44,"a":99,"b":2,"d":4}', JSON.stringify( obj ));
}
// test - hasObjectArray but no elements in it
{
var obj = {"a":1};
obj[0] = 2;
TEST(JSON.stringify(obj), '{"0":2,"a":1}');
delete obj[0];
TEST(JSON.stringify(obj), '{"a":1}');
}
// test internal properties
{
let obj = {foo: 1};
let wm = new WeakMap();
// Add internal WeakMapKeyMap property to obj
wm.set(obj,obj);
TEST('{"foo":1}', JSON.stringify(obj));
Object.defineProperty(obj, "getter", {get: function () { return 2}, enumerable: true, configurable: true});
TEST('{"foo":1,"getter":2}', JSON.stringify(obj));
const err = new Error("message");
TEST('{}', JSON.stringify(err));
}
// test - function prototype new instance
TEST("\"1\"", JSON.stringify(new fnc(1)))
// test - pre-post alter Date toJSON definition
var dateString = JSON.stringify(new Date(0));
TEST("1970", dateString.substr(dateString.indexOf("1970"), 4))
Date.prototype.toJSON = 1;
TEST("{}", JSON.stringify(new Date(0)))
delete Date.prototype.toJSON
TEST("{}", JSON.stringify(new Date(0)))
// test - use from Object prototype
Object.prototype.toJSON = function() { return 2; }
delete fnc.prototype.toJSON;
TEST(2, JSON.stringify(new fnc(1)))
// test - symbol
Object.prototype[Symbol("toJSON")] = function() { return 3; }
TEST(2, JSON.stringify(new fnc(1)))
console.log("PASS")