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
|
/**
* These return* functions are used by the
* replacer tests taken from bug 512447
*/
function returnObjectFor1(k, v)
{
if (k == "1")
return {};
return v;
}
function returnArrayFor1(k, v)
{
if (k == "1")
return [];
return v;
}
function returnNullFor1(k, v)
{
if (k == "1")
return null;
return v;
}
function returnStringForUndefined(k, v)
{
if (v === undefined)
return "undefined value";
return v;
}
var cycleObject = {}; cycleObject.cycle = cycleObject;
function returnCycleObjectFor1(k, v)
{
if (k == "1")
return cycleObject;
return v;
}
var array = [0, 1, 2]; array[3] = array;
function returnCycleArrayFor1(k, v)
{
if (k == "1")
return array;
return v;
}
// BEGIN TEST
var x;
x = JSON.stringify({ key: 2 },
function(k,v) { return k ? undefined : v; });
assertEq(x, "{}");
x = JSON.stringify(["hmm", "hmm"],
function(k,v) { return k !== "" ? undefined : v; });
assertEq(x, "[null,null]");
var foo = ["hmm"];
function censor(k, v)
{
if (v !== foo)
return "XXX";
return v;
}
x = JSON.stringify(foo, censor);
assertEq(x, '["XXX"]');
foo = ["bar", ["baz"], "qux"];
x = JSON.stringify(foo, censor);
assertEq(x, '["XXX","XXX","XXX"]');
function censor2(k, v)
{
if (typeof(v) == "string")
return "XXX";
return v;
}
foo = ["bar", ["baz"], "qux"];
x = JSON.stringify(foo, censor2);
assertEq(x, '["XXX",["XXX"],"XXX"]');
foo = { bar: 42, qux: 42, quux: 42 };
x = JSON.stringify(foo, ["bar"]);
assertEq(x, '{"bar":42}');
foo = {bar: {bar: 42, schmoo:[]}, qux: 42, quux: 42};
x = JSON.stringify(foo, ["bar", "schmoo"]);
assertEq(x, '{"bar":{"bar":42,"schmoo":[]}}');
x = JSON.stringify(foo, null, "");
assertEq(x, '{"bar":{"bar":42,"schmoo":[]},"qux":42,"quux":42}');
x = JSON.stringify(foo, null, " ");
assertEq(x, '{\n "bar": {\n "bar": 42,\n "schmoo": []\n },\n "qux": 42,\n "quux": 42\n}');
foo = {bar:{bar:{}}}
x = JSON.stringify(foo, null, " ");
assertEq(x, '{\n "bar": {\n "bar": {}\n }\n}');
x = JSON.stringify({ x: 1, arr: [1] },
function (k,v) { return typeof v === 'number' ? 3 : v; });
assertEq(x, '{"x":3,"arr":[3]}');
foo = ['e'];
x = JSON.stringify(foo, null, '\t');
assertEq(x, '[\n\t"e"\n]');
foo = {0:0, 1:1, 2:2, 3:undefined};
x = JSON.stringify(foo, returnObjectFor1);
assertEq(x, '{"0":0,"1":{},"2":2}');
x = JSON.stringify(foo, returnArrayFor1);
assertEq(x, '{"0":0,"1":[],"2":2}');
x = JSON.stringify(foo, returnNullFor1);
assertEq(x, '{"0":0,"1":null,"2":2}');
x = JSON.stringify(foo, returnStringForUndefined);
assertEq(x, '{"0":0,"1":1,"2":2,"3":"undefined value"}');
try
{
JSON.stringify(foo, returnCycleObjectFor1);
throw new Error("no error thrown");
}
catch (e)
{
assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
}
try
{
JSON.stringify(foo, returnCycleArrayFor1);
throw new Error("no error thrown");
}
catch (e)
{
assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
}
foo = [0, 1, 2, undefined];
try
{
JSON.stringify(foo, returnCycleObjectFor1);
throw new Error("no error thrown");
}
catch (e)
{
assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
}
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");
|