Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: test/mjsunit/json.js

Issue 549207: Added validating JSON parser mode to parser. (Closed)
Patch Set: Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 assertEquals(null, n4.toJSON()); 86 assertEquals(null, n4.toJSON());
87 87
88 assertEquals(Object.prototype, JSON.__proto__); 88 assertEquals(Object.prototype, JSON.__proto__);
89 assertEquals("[object JSON]", Object.prototype.toString.call(JSON)); 89 assertEquals("[object JSON]", Object.prototype.toString.call(JSON));
90 90
91 // DontEnum 91 // DontEnum
92 for (var p in this) 92 for (var p in this)
93 assertFalse(p == "JSON"); 93 assertFalse(p == "JSON");
94 94
95 // Parse 95 // Parse
96
97 assertEquals({}, JSON.parse("{}")); 96 assertEquals({}, JSON.parse("{}"));
97 assertEquals({42:37}, JSON.parse('{"42":37}'));
98 assertEquals(null, JSON.parse("null")); 98 assertEquals(null, JSON.parse("null"));
99 assertEquals(true, JSON.parse("true")); 99 assertEquals(true, JSON.parse("true"));
100 assertEquals(false, JSON.parse("false")); 100 assertEquals(false, JSON.parse("false"));
101 assertEquals("foo", JSON.parse('"foo"')); 101 assertEquals("foo", JSON.parse('"foo"'));
102 assertEquals("f\no", JSON.parse('"f\\no"')); 102 assertEquals("f\no", JSON.parse('"f\\no"'));
103 assertEquals("\b\f\n\r\t\"\u2028\/\\",
104 JSON.parse('"\\b\\f\\n\\r\\t\\"\\u2028\\/\\\\"'));
105 assertEquals([1.1], JSON.parse("[1.1]"));
106 assertEquals([1], JSON.parse("[1.0]"));
107
108 assertEquals(0, JSON.parse("0"));
109 assertEquals(1, JSON.parse("1"));
110 assertEquals(0.1, JSON.parse("0.1"));
103 assertEquals(1.1, JSON.parse("1.1")); 111 assertEquals(1.1, JSON.parse("1.1"));
104 assertEquals(1, JSON.parse("1.0")); 112 assertEquals(1.1, JSON.parse("1.100000"));
105 assertEquals(0.0000000003, JSON.parse("3e-10")); 113 assertEquals(1.111111, JSON.parse("1.111111"));
114 assertEquals(-0, JSON.parse("-0"));
115 assertEquals(-1, JSON.parse("-1"));
116 assertEquals(-0.1, JSON.parse("-0.1"));
117 assertEquals(-1.1, JSON.parse("-1.1"));
118 assertEquals(-1.1, JSON.parse("-1.100000"));
119 assertEquals(-1.111111, JSON.parse("-1.111111"));
120 assertEquals(11, JSON.parse("1.1e1"));
121 assertEquals(11, JSON.parse("1.1e+1"));
122 assertEquals(0.11, JSON.parse("1.1e-1"));
123 assertEquals(11, JSON.parse("1.1E1"));
124 assertEquals(11, JSON.parse("1.1E+1"));
125 assertEquals(0.11, JSON.parse("1.1E-1"));
126
106 assertEquals([], JSON.parse("[]")); 127 assertEquals([], JSON.parse("[]"));
107 assertEquals([1], JSON.parse("[1]")); 128 assertEquals([1], JSON.parse("[1]"));
108 assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]')); 129 assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]'));
109 130
131 assertEquals("", JSON.parse('""'));
132 assertEquals(["", "", -0, ""], JSON.parse('[ "" , "" , -0, ""]'));
133 assertEquals("", JSON.parse('""'));
134
135
110 function GetFilter(name) { 136 function GetFilter(name) {
111 function Filter(key, value) { 137 function Filter(key, value) {
112 return (key == name) ? undefined : value; 138 return (key == name) ? undefined : value;
113 } 139 }
114 return Filter; 140 return Filter;
115 } 141 }
116 142
117 var pointJson = '{"x": 1, "y": 2}'; 143 var pointJson = '{"x": 1, "y": 2}';
118 assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson)); 144 assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson));
119 assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y'))); 145 assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y')));
(...skipping 18 matching lines...) Expand all
138 164
139 TestInvalid('abcdef'); 165 TestInvalid('abcdef');
140 TestInvalid('isNaN()'); 166 TestInvalid('isNaN()');
141 TestInvalid('{"x": [1, 2, deepObject]}'); 167 TestInvalid('{"x": [1, 2, deepObject]}');
142 TestInvalid('[1, [2, [deepObject], 3], 4]'); 168 TestInvalid('[1, [2, [deepObject], 3], 4]');
143 TestInvalid('function () { return 0; }'); 169 TestInvalid('function () { return 0; }');
144 170
145 TestInvalid("[1, 2"); 171 TestInvalid("[1, 2");
146 TestInvalid('{"x": 3'); 172 TestInvalid('{"x": 3');
147 173
174 // JavaScript number literals not valid in JSON.
175 TestInvalid('[01]');
176 TestInvalid('[.1]');
177 TestInvalid('[1.]');
178 TestInvalid('[1.e1]');
179 TestInvalid('[-.1]');
180 TestInvalid('[-1.]');
181
182 // Plain invalid number literals.
183 TestInvalid('-');
184 TestInvalid('--1');
185 TestInvalid('-1e');
186 TestInvalid('1e--1]');
187 TestInvalid('1e+-1');
188 TestInvalid('1e-+1');
189 TestInvalid('1e++1');
190
191 // JavaScript string literals not valid in JSON.
192 TestInvalid("'single quote'");
193 TestInvalid('"\\a invalid escape"');
194 TestInvalid('"\\x42 invalid escape"');
195 TestInvalid('"\\u202 invalid escape"');
196 TestInvalid('"\\012 invalid escape"');
197
198 // Test bad JSON that would be good JavaScript (ES5).
199
200 TestInvalid("{true:42}");
201 TestInvalid("{false:42}");
202 TestInvalid("{null:42}");
203 TestInvalid("{'foo':42}");
204 TestInvalid("{42:42}");
205 TestInvalid("{0:42}");
206 TestInvalid("{-1:42}");
207
208 // Test for trailing garbage detection.
209
210 TestInvalid('42 px');
211 TestInvalid('42 .2');
212 TestInvalid('"42" ""');
213 TestInvalid('"42" ""');
214 TestInvalid('"" ""');
215 TestInvalid('true ""');
216 TestInvalid('false ""');
217 TestInvalid('null ""');
218 TestInvalid('null ""');
219 TestInvalid('[] ""');
220 TestInvalid('[true] ""');
221 TestInvalid('{} ""');
222 TestInvalid('{"x":true} ""');
223
148 // Stringify 224 // Stringify
149 225
150 assertEquals("true", JSON.stringify(true)); 226 assertEquals("true", JSON.stringify(true));
151 assertEquals("false", JSON.stringify(false)); 227 assertEquals("false", JSON.stringify(false));
152 assertEquals("null", JSON.stringify(null)); 228 assertEquals("null", JSON.stringify(null));
153 assertEquals("false", JSON.stringify({toJSON: function () { return false; }})); 229 assertEquals("false", JSON.stringify({toJSON: function () { return false; }}));
154 assertEquals("4", JSON.stringify(4)); 230 assertEquals("4", JSON.stringify(4));
155 assertEquals('"foo"', JSON.stringify("foo")); 231 assertEquals('"foo"', JSON.stringify("foo"));
156 assertEquals("null", JSON.stringify(Infinity)); 232 assertEquals("null", JSON.stringify(Infinity));
157 assertEquals("null", JSON.stringify(-Infinity)); 233 assertEquals("null", JSON.stringify(-Infinity));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 274
199 function checkIllegal(str) { 275 function checkIllegal(str) {
200 assertThrows(function () { JSON.parse(str); }, SyntaxError); 276 assertThrows(function () { JSON.parse(str); }, SyntaxError);
201 } 277 }
202 278
203 checkIllegal('1); throw "foo"; (1'); 279 checkIllegal('1); throw "foo"; (1');
204 280
205 var x = 0; 281 var x = 0;
206 eval("(1); x++; (1)"); 282 eval("(1); x++; (1)");
207 checkIllegal('1); x++; (1'); 283 checkIllegal('1); x++; (1');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698