blob: bf44f7814ee640225b0f05f966b1de74ecda9cf2 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28function GenericToJSONChecks(Constructor, value, alternative) {
29 var n1 = new Constructor(value);
30 n1.valueOf = function () { return alternative; };
31 assertEquals(alternative, n1.toJSON());
32 var n2 = new Constructor(value);
33 n2.valueOf = null;
34 assertThrows(function () { n2.toJSON(); }, TypeError);
35 var n3 = new Constructor(value);
36 n3.valueOf = function () { return {}; };
37 assertThrows(function () { n3.toJSON(); }, TypeError, 'result_not_primitive');
38 var n4 = new Constructor(value);
39 n4.valueOf = function () {
40 assertEquals(0, arguments.length);
41 assertEquals(this, n4);
42 return null;
43 };
44 assertEquals(null, n4.toJSON());
45}
46
47// Number toJSON
48assertEquals(3, (3).toJSON());
49assertEquals(3, (3).toJSON(true));
50assertEquals(4, (new Number(4)).toJSON());
51GenericToJSONChecks(Number, 5, 6);
52
53// Boolean toJSON
54assertEquals(true, (true).toJSON());
55assertEquals(true, (true).toJSON(false));
56assertEquals(false, (false).toJSON());
57assertEquals(true, (new Boolean(true)).toJSON());
58GenericToJSONChecks(Boolean, true, false);
59GenericToJSONChecks(Boolean, false, true);
60
61// String toJSON
62assertEquals("flot", "flot".toJSON());
63assertEquals("flot", "flot".toJSON(3));
64assertEquals("tolf", (new String("tolf")).toJSON());
65GenericToJSONChecks(String, "x", "y");
66
67// Date toJSON
68assertEquals("1970-01-01T00:00:00Z", new Date(0).toJSON());
69assertEquals("1979-01-11T08:00:00Z", new Date("1979-01-11 08:00 GMT").toJSON());
70assertEquals("2005-05-05T05:05:05Z", new Date("2005-05-05 05:05:05 GMT").toJSON());
71var n1 = new Date(10000);
72n1.toISOString = function () { return "foo"; };
73assertEquals("foo", n1.toJSON());
74var n2 = new Date(10001);
75n2.toISOString = null;
76assertThrows(function () { n2.toJSON(); }, TypeError);
77var n3 = new Date(10002);
78n3.toISOString = function () { return {}; };
79assertThrows(function () { n3.toJSON(); }, TypeError, "result_not_primitive");
80var n4 = new Date(10003);
81n4.toISOString = function () {
82 assertEquals(0, arguments.length);
83 assertEquals(this, n4);
84 return null;
85};
86assertEquals(null, n4.toJSON());
87
88assertEquals(Object.prototype, JSON.__proto__);
89assertEquals("[object JSON]", Object.prototype.toString.call(JSON));
90
91// DontEnum
92for (var p in this)
93 assertFalse(p == "JSON");
94
95// Parse
96
97assertEquals({}, JSON.parse("{}"));
98assertEquals(null, JSON.parse("null"));
99assertEquals(true, JSON.parse("true"));
100assertEquals(false, JSON.parse("false"));
101assertEquals("foo", JSON.parse('"foo"'));
102assertEquals("f\no", JSON.parse('"f\\no"'));
103assertEquals(1.1, JSON.parse("1.1"));
104assertEquals(1, JSON.parse("1.0"));
105assertEquals(0.0000000003, JSON.parse("3e-10"));
106assertEquals([], JSON.parse("[]"));
107assertEquals([1], JSON.parse("[1]"));
108assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]'));
109
110function GetFilter(name) {
111 function Filter(key, value) {
112 return (key == name) ? undefined : value;
113 }
114 return Filter;
115}
116
117var pointJson = '{"x": 1, "y": 2}';
118assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson));
119assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y')));
120assertEquals({'y': 2}, JSON.parse(pointJson, GetFilter('x')));
121assertEquals([1, 2, 3], JSON.parse("[1, 2, 3]"));
122assertEquals([1, undefined, 3], JSON.parse("[1, 2, 3]", GetFilter(1)));
123assertEquals([1, 2, undefined], JSON.parse("[1, 2, 3]", GetFilter(2)));
124
125function DoubleNumbers(key, value) {
126 return (typeof value == 'number') ? 2 * value : value;
127}
128
129var deepObject = '{"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}';
130assertEquals({"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}},
131 JSON.parse(deepObject));
132assertEquals({"a": {"b": 2, "c": 4}, "d": {"e": {"f": 6}}},
133 JSON.parse(deepObject, DoubleNumbers));
134
135function TestInvalid(str) {
136 assertThrows(function () { JSON.parse(str); }, SyntaxError);
137}
138
139TestInvalid('abcdef');
140TestInvalid('isNaN()');
141TestInvalid('{"x": [1, 2, deepObject]}');
142TestInvalid('[1, [2, [deepObject], 3], 4]');
143TestInvalid('function () { return 0; }');
144
145TestInvalid("[1, 2");
146TestInvalid('{"x": 3');
147
148// Stringify
149
150assertEquals("true", JSON.stringify(true));
151assertEquals("false", JSON.stringify(false));
152assertEquals("null", JSON.stringify(null));
153assertEquals("false", JSON.stringify({toJSON: function () { return false; }}));
154assertEquals("4", JSON.stringify(4));
155assertEquals('"foo"', JSON.stringify("foo"));
156assertEquals("null", JSON.stringify(Infinity));
157assertEquals("null", JSON.stringify(-Infinity));
158assertEquals("null", JSON.stringify(NaN));
159assertEquals("4", JSON.stringify(new Number(4)));
160assertEquals('"bar"', JSON.stringify(new String("bar")));
161
162assertEquals('"foo\\u0000bar"', JSON.stringify("foo\0bar"));
163assertEquals('"f\\"o\'o\\\\b\\ba\\fr\\nb\\ra\\tz"',
164 JSON.stringify("f\"o\'o\\b\ba\fr\nb\ra\tz"));
165
166assertEquals("[1,2,3]", JSON.stringify([1, 2, 3]));
167assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 1));
168assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 2));
169assertEquals("[\n 1,\n 2,\n 3\n]",
170 JSON.stringify([1, 2, 3], null, new Number(2)));
171assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^"));
172assertEquals("[\n^1,\n^2,\n^3\n]",
173 JSON.stringify([1, 2, 3], null, new String("^")));
174assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]",
175 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1));
176assertEquals("[]", JSON.stringify([], null, 1));
177assertEquals("[1,2,[3,[4],5],6,7]",
178 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null));
179assertEquals("[2,4,[6,[8],10],12,14]",
180 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers));
181
182var circular = [1, 2, 3];
183circular[2] = circular;
184assertThrows(function () { JSON.stringify(circular); }, TypeError);
185
186var singleton = [];
187var multiOccurrence = [singleton, singleton, singleton];
188assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence));
189
190assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6}));
191assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x']));
192assertEquals('{\n "a": "b",\n "c": "d"\n}',
193 JSON.stringify({a:"b",c:"d"}, null, 1));
194assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));
195
196assertEquals(undefined, JSON.stringify(undefined));
197assertEquals(undefined, JSON.stringify(function () { }));
198
199function checkIllegal(str) {
200 assertThrows(function () { JSON.parse(str); }, SyntaxError);
201}
202
203checkIllegal('1); throw "foo"; (1');
204
205var x = 0;
206eval("(1); x++; (1)");
207checkIllegal('1); x++; (1');