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
|
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/
var gTestfile = 'parse-reviver-array-delete.js';
//-----------------------------------------------------------------------------
var BUGNUMBER = 999999;
var summary = "JSON.parse with a reviver which elides array elements";
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
/*
* The reviver deletes all properties from the to-be-returned array. Thus
* stringification reveals properties on the prototype chain -- but there are
* none, so this result is unsurprising.
*/
assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]',
function revive(k, v)
{
if (k === "")
return v;
return undefined;
}) + "",
",,,,,,,,,,,,,,,,,,,");
/*
* Now let's try a reviver that deletes every property but a mega-huge one.
*/
var str = "[";
var expected = "";
var expected2 = "";
for (var i = 0; i < 2048; i++)
{
str += "1,";
if (i === 2047)
{
expected += "1";
expected2 += "1";
}
if (i === 3)
expected2 += "17";
expected += ",";
expected2 += ",";
}
str += "1]";
assertEq(JSON.parse(str,
function reviver(k, v)
{
if (k === "" || k === "2047")
return v;
return undefined;
}) + "",
expected);
Array.prototype[3] = 17;
/* Now, with a property on the prototype chain, it'll show through. */
assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]',
function revive(k, v)
{
if (k === "")
return v;
return undefined;
}) + "",
",,,17,,,,,,,,,,,,,,,,");
/* And here too. */
assertEq(JSON.parse(str,
function reviver(k, v)
{
if (k === "" || k === "2047")
return v;
return undefined;
}) + "",
expected2);
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");
|