| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.jsonTest'); |
| 16 |
goog.setTestOnly('goog.jsonTest'); |
| 17 |
|
| 18 |
goog.require('goog.functions'); |
| 19 |
goog.require('goog.json'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.userAgent'); |
| 22 |
|
| 23 |
function allChars(start, end) { |
| 24 |
var sb = []; |
| 25 |
for (var i = start; i < end; i++) { |
| 26 |
sb.push(String.fromCharCode(i)); |
| 27 |
} |
| 28 |
return sb.join(''); |
| 29 |
} |
| 30 |
|
| 31 |
// serialization |
| 32 |
|
| 33 |
function testStringSerialize() { |
| 34 |
assertSerialize('""', ''); |
| 35 |
|
| 36 |
assertSerialize('"true"', 'true'); |
| 37 |
assertSerialize('"false"', 'false'); |
| 38 |
assertSerialize('"null"', 'null'); |
| 39 |
assertSerialize('"0"', '0'); |
| 40 |
|
| 41 |
// Unicode and control characters |
| 42 |
assertSerialize('"\\n"', '\n'); |
| 43 |
assertSerialize('"\\u001f"', '\x1f'); |
| 44 |
assertSerialize('"\\u20ac"', '\u20AC'); |
| 45 |
assertSerialize('"\\ud83d\\ud83d"', '\ud83d\ud83d'); |
| 46 |
|
| 47 |
var str = allChars(0, 10000); |
| 48 |
assertEquals(str, eval(goog.json.serialize(str))); |
| 49 |
} |
| 50 |
|
| 51 |
function testNullSerialize() { |
| 52 |
assertSerialize('null', null); |
| 53 |
assertSerialize('null', undefined); |
| 54 |
assertSerialize('null', NaN); |
| 55 |
|
| 56 |
assertSerialize('0', 0); |
| 57 |
assertSerialize('""', ''); |
| 58 |
assertSerialize('false', false); |
| 59 |
} |
| 60 |
|
| 61 |
function testNullPropertySerialize() { |
| 62 |
assertSerialize('{"a":null}', {'a': null}); |
| 63 |
assertSerialize('{"a":null}', {'a': undefined}); |
| 64 |
} |
| 65 |
|
| 66 |
function testNumberSerialize() { |
| 67 |
assertSerialize('0', 0); |
| 68 |
assertSerialize('12345', 12345); |
| 69 |
assertSerialize('-12345', -12345); |
| 70 |
|
| 71 |
assertSerialize('0.1', 0.1); |
| 72 |
// the leading zero may not be omitted |
| 73 |
assertSerialize('0.1', .1); |
| 74 |
|
| 75 |
// no leading + |
| 76 |
assertSerialize('1', +1); |
| 77 |
|
| 78 |
// either format is OK |
| 79 |
var s = goog.json.serialize(1e50); |
| 80 |
assertTrue( |
| 81 |
'1e50', s == '1e50' || s == '1E50' || s == '1e+50' || s == '1E+50'); |
| 82 |
|
| 83 |
// either format is OK |
| 84 |
s = goog.json.serialize(1e-50); |
| 85 |
assertTrue('1e50', s == '1e-50' || s == '1E-50'); |
| 86 |
|
| 87 |
// These numbers cannot be represented in JSON |
| 88 |
assertSerialize('null', NaN); |
| 89 |
assertSerialize('null', Infinity); |
| 90 |
assertSerialize('null', -Infinity); |
| 91 |
} |
| 92 |
|
| 93 |
function testBooleanSerialize() { |
| 94 |
assertSerialize('true', true); |
| 95 |
assertSerialize('"true"', 'true'); |
| 96 |
|
| 97 |
assertSerialize('false', false); |
| 98 |
assertSerialize('"false"', 'false'); |
| 99 |
} |
| 100 |
|
| 101 |
function testArraySerialize() { |
| 102 |
assertSerialize('[]', []); |
| 103 |
assertSerialize('[1]', [1]); |
| 104 |
assertSerialize('[1,2]', [1, 2]); |
| 105 |
assertSerialize('[1,2,3]', [1, 2, 3]); |
| 106 |
assertSerialize('[[]]', [[]]); |
| 107 |
assertSerialize('[null,null]', [function() {}, function() {}]); |
| 108 |
|
| 109 |
assertNotEquals('{length:0}', goog.json.serialize({length: 0}), '[]'); |
| 110 |
} |
| 111 |
|
| 112 |
function testFunctionSerialize() { |
| 113 |
assertSerialize('null', function() {}); |
| 114 |
} |
| 115 |
|
| 116 |
function testObjectSerialize_emptyObject() { |
| 117 |
assertSerialize('{}', {}); |
| 118 |
} |
| 119 |
|
| 120 |
function testObjectSerialize_oneItem() { |
| 121 |
assertSerialize('{"a":"b"}', {a: 'b'}); |
| 122 |
} |
| 123 |
|
| 124 |
function testObjectSerialize_twoItems() { |
| 125 |
assertEquals( |
| 126 |
'{"a":"b","c":"d"}', goog.json.serialize({a: 'b', c: 'd'}), |
| 127 |
'{"a":"b","c":"d"}'); |
| 128 |
} |
| 129 |
|
| 130 |
function testObjectSerialize_whitespace() { |
| 131 |
assertSerialize('{" ":" "}', {' ': ' '}); |
| 132 |
} |
| 133 |
|
| 134 |
function testSerializeSkipFunction() { |
| 135 |
var object = |
| 136 |
{s: 'string value', b: true, i: 100, f: function() { var x = 'x'; }}; |
| 137 |
assertSerialize('null', object.f); |
| 138 |
assertSerialize('{"s":"string value","b":true,"i":100}', object); |
| 139 |
} |
| 140 |
|
| 141 |
function testObjectSerialize_array() { |
| 142 |
assertNotEquals('[0,1]', goog.json.serialize([0, 1]), '{"0":"0","1":"1"}'); |
| 143 |
} |
| 144 |
|
| 145 |
function testObjectSerialize_recursion() { |
| 146 |
if (goog.userAgent.WEBKIT) { |
| 147 |
return; // this makes safari 4 crash. |
| 148 |
} |
| 149 |
|
| 150 |
var anObject = {}; |
| 151 |
anObject.thisObject = anObject; |
| 152 |
assertThrows('expected recursion exception', function() { |
| 153 |
goog.json.serialize(anObject); |
| 154 |
}); |
| 155 |
} |
| 156 |
|
| 157 |
function testObjectSerializeWithHasOwnProperty() { |
| 158 |
var object = {'hasOwnProperty': null}; |
| 159 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) { |
| 160 |
assertEquals('{}', goog.json.serialize(object)); |
| 161 |
} else { |
| 162 |
assertEquals('{"hasOwnProperty":null}', goog.json.serialize(object)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
function testWrappedObjects() { |
| 167 |
assertSerialize('"foo"', new String('foo')); |
| 168 |
assertSerialize('42', new Number(42)); |
| 169 |
assertSerialize('null', new Number('a NaN')); |
| 170 |
assertSerialize('true', new Boolean(true)); |
| 171 |
} |
| 172 |
|
| 173 |
// parsing |
| 174 |
|
| 175 |
function testStringParse() { |
| 176 |
assertEquals('Empty string', goog.json.parse('""'), ''); |
| 177 |
assertEquals('whitespace string', goog.json.parse('" "'), ' '); |
| 178 |
|
| 179 |
// unicode without the control characters 0x00 - 0x1f, 0x7f - 0x9f |
| 180 |
var str = allChars(32, 1000); |
| 181 |
var jsonString = goog.json.serialize(str); |
| 182 |
var a = eval(jsonString); |
| 183 |
assertEquals('unicode string', goog.json.parse(jsonString), a); |
| 184 |
|
| 185 |
assertEquals('true as a string', goog.json.parse('"true"'), 'true'); |
| 186 |
assertEquals('false as a string', goog.json.parse('"false"'), 'false'); |
| 187 |
assertEquals('null as a string', goog.json.parse('"null"'), 'null'); |
| 188 |
assertEquals('number as a string', goog.json.parse('"0"'), '0'); |
| 189 |
} |
| 190 |
|
| 191 |
function testNullParse() { |
| 192 |
assertEquals('null', goog.json.parse(null), null); |
| 193 |
assertEquals('null', goog.json.parse('null'), null); |
| 194 |
|
| 195 |
assertNotEquals('0', goog.json.parse('0'), null); |
| 196 |
assertNotEquals('""', goog.json.parse('""'), null); |
| 197 |
assertNotEquals('false', goog.json.parse('false'), null); |
| 198 |
} |
| 199 |
|
| 200 |
function testNumberParse() { |
| 201 |
assertEquals('0', goog.json.parse('0'), 0); |
| 202 |
assertEquals('12345', goog.json.parse('12345'), 12345); |
| 203 |
assertEquals('-12345', goog.json.parse('-12345'), -12345); |
| 204 |
|
| 205 |
assertEquals('0.1', goog.json.parse('0.1'), 0.1); |
| 206 |
|
| 207 |
// either format is OK |
| 208 |
assertEquals(1e50, goog.json.parse('1e50')); |
| 209 |
assertEquals(1e50, goog.json.parse('1E50')); |
| 210 |
assertEquals(1e50, goog.json.parse('1e+50')); |
| 211 |
assertEquals(1e50, goog.json.parse('1E+50')); |
| 212 |
|
| 213 |
// either format is OK |
| 214 |
assertEquals(1e-50, goog.json.parse('1e-50')); |
| 215 |
assertEquals(1e-50, goog.json.parse('1E-50')); |
| 216 |
} |
| 217 |
|
| 218 |
function testBooleanParse() { |
| 219 |
assertEquals('true', goog.json.parse('true'), true); |
| 220 |
assertEquals('false', goog.json.parse('false'), false); |
| 221 |
|
| 222 |
assertNotEquals('0', goog.json.parse('0'), false); |
| 223 |
assertNotEquals('"false"', goog.json.parse('"false"'), false); |
| 224 |
assertNotEquals('null', goog.json.parse('null'), false); |
| 225 |
|
| 226 |
assertNotEquals('1', goog.json.parse('1'), true); |
| 227 |
assertNotEquals('"true"', goog.json.parse('"true"'), true); |
| 228 |
assertNotEquals('{}', goog.json.parse('{}'), true); |
| 229 |
assertNotEquals('[]', goog.json.parse('[]'), true); |
| 230 |
} |
| 231 |
|
| 232 |
function testArrayParse() { |
| 233 |
assertArrayEquals([], goog.json.parse('[]')); |
| 234 |
assertArrayEquals([1], goog.json.parse('[1]')); |
| 235 |
assertArrayEquals([1, 2], goog.json.parse('[1,2]')); |
| 236 |
assertArrayEquals([1, 2, 3], goog.json.parse('[1,2,3]')); |
| 237 |
assertArrayEquals([[]], goog.json.parse('[[]]')); |
| 238 |
|
| 239 |
// Note that array-holes are not valid json. However, goog.json.parse |
| 240 |
// supports them so that clients can reap the security benefits of |
| 241 |
// goog.json.parse even if they are using this non-standard format. |
| 242 |
assertArrayEquals([1, /* hole */, 3], goog.json.parse('[1,,3]')); |
| 243 |
|
| 244 |
// make sure we do not get an array for something that looks like an array |
| 245 |
assertFalse('{length:0}', 'push' in goog.json.parse('{"length":0}')); |
| 246 |
} |
| 247 |
|
| 248 |
function testObjectParse() { |
| 249 |
function objectEquals(a1, a2) { |
| 250 |
for (var key in a1) { |
| 251 |
if (a1[key] != a2[key]) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
} |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
assertTrue('{}', objectEquals(goog.json.parse('{}'), {})); |
| 259 |
assertTrue('{"a":"b"}', objectEquals(goog.json.parse('{"a":"b"}'), {a: 'b'})); |
| 260 |
assertTrue( |
| 261 |
'{"a":"b","c":"d"}', |
| 262 |
objectEquals(goog.json.parse('{"a":"b","c":"d"}'), {a: 'b', c: 'd'})); |
| 263 |
assertTrue( |
| 264 |
'{" ":" "}', objectEquals(goog.json.parse('{" ":" "}'), {' ': ' '})); |
| 265 |
|
| 266 |
// make sure we do not get an Object when it is really an array |
| 267 |
assertTrue('[0,1]', 'length' in goog.json.parse('[0,1]')); |
| 268 |
} |
| 269 |
|
| 270 |
function testForValidJson() { |
| 271 |
function error_(msg, s) { |
| 272 |
assertThrows( |
| 273 |
msg + ', Should have raised an exception: ' + s, |
| 274 |
goog.partial(goog.json.parse, s)); |
| 275 |
} |
| 276 |
|
| 277 |
error_('Non closed string', '"dasdas'); |
| 278 |
error_('undefined is not valid json', 'undefined'); |
| 279 |
|
| 280 |
// These numbers cannot be represented in JSON |
| 281 |
error_('NaN cannot be presented in JSON', 'NaN'); |
| 282 |
error_('Infinity cannot be presented in JSON', 'Infinity'); |
| 283 |
error_('-Infinity cannot be presented in JSON', '-Infinity'); |
| 284 |
} |
| 285 |
|
| 286 |
function testIsNotValid() { |
| 287 |
assertFalse(goog.json.isValid('t')); |
| 288 |
assertFalse(goog.json.isValid('r')); |
| 289 |
assertFalse(goog.json.isValid('u')); |
| 290 |
assertFalse(goog.json.isValid('e')); |
| 291 |
assertFalse(goog.json.isValid('f')); |
| 292 |
assertFalse(goog.json.isValid('a')); |
| 293 |
assertFalse(goog.json.isValid('l')); |
| 294 |
assertFalse(goog.json.isValid('s')); |
| 295 |
assertFalse(goog.json.isValid('n')); |
| 296 |
assertFalse(goog.json.isValid('E')); |
| 297 |
|
| 298 |
assertFalse(goog.json.isValid('+')); |
| 299 |
assertFalse(goog.json.isValid('-')); |
| 300 |
|
| 301 |
assertFalse(goog.json.isValid('t++')); |
| 302 |
assertFalse(goog.json.isValid('++t')); |
| 303 |
assertFalse(goog.json.isValid('t--')); |
| 304 |
assertFalse(goog.json.isValid('--t')); |
| 305 |
assertFalse(goog.json.isValid('-t')); |
| 306 |
assertFalse(goog.json.isValid('+t')); |
| 307 |
|
| 308 |
assertFalse(goog.json.isValid('"\\"')); // "\" |
| 309 |
assertFalse(goog.json.isValid('"\\')); // "\ |
| 310 |
|
| 311 |
// multiline string using \ at the end is not valid |
| 312 |
assertFalse(goog.json.isValid('"a\\\nb"')); |
| 313 |
|
| 314 |
|
| 315 |
assertFalse(goog.json.isValid('"\n"')); |
| 316 |
assertFalse(goog.json.isValid('"\r"')); |
| 317 |
assertFalse(goog.json.isValid('"\r\n"')); |
| 318 |
// Disallow the unicode newlines |
| 319 |
assertFalse(goog.json.isValid('"\u2028"')); |
| 320 |
assertFalse(goog.json.isValid('"\u2029"')); |
| 321 |
|
| 322 |
assertFalse(goog.json.isValid(' ')); |
| 323 |
assertFalse(goog.json.isValid('\n')); |
| 324 |
assertFalse(goog.json.isValid('\r')); |
| 325 |
assertFalse(goog.json.isValid('\r\n')); |
| 326 |
|
| 327 |
assertFalse(goog.json.isValid('t.r')); |
| 328 |
|
| 329 |
assertFalse(goog.json.isValid('1e')); |
| 330 |
assertFalse(goog.json.isValid('1e-')); |
| 331 |
assertFalse(goog.json.isValid('1e+')); |
| 332 |
|
| 333 |
assertFalse(goog.json.isValid('1e-')); |
| 334 |
|
| 335 |
assertFalse(goog.json.isValid('"\\\u200D\\"')); |
| 336 |
assertFalse(goog.json.isValid('"\\\0\\"')); |
| 337 |
assertFalse(goog.json.isValid('"\\\0"')); |
| 338 |
assertFalse(goog.json.isValid('"\\0"')); |
| 339 |
assertFalse(goog.json.isValid('"\x0c"')); |
| 340 |
|
| 341 |
assertFalse(goog.json.isValid('"\\\u200D\\", alert(\'foo\') //"\n')); |
| 342 |
|
| 343 |
// Disallow referencing variables with names built up from primitives |
| 344 |
assertFalse(goog.json.isValid('truefalse')); |
| 345 |
assertFalse(goog.json.isValid('null0')); |
| 346 |
assertFalse(goog.json.isValid('null0.null0')); |
| 347 |
assertFalse(goog.json.isValid('[truefalse]')); |
| 348 |
assertFalse(goog.json.isValid('{"a": null0}')); |
| 349 |
assertFalse(goog.json.isValid('{"a": null0, "b": 1}')); |
| 350 |
} |
| 351 |
|
| 352 |
function testIsValid() { |
| 353 |
assertTrue(goog.json.isValid('\n""\n')); |
| 354 |
assertTrue(goog.json.isValid('[1\n,2\r,3\u2028\n,4\u2029]')); |
| 355 |
assertTrue(goog.json.isValid('"\x7f"')); |
| 356 |
assertTrue(goog.json.isValid('"\x09"')); |
| 357 |
// Test tab characters in json. |
| 358 |
assertTrue(goog.json.isValid('{"\t":"\t"}')); |
| 359 |
} |
| 360 |
|
| 361 |
function testDoNotSerializeProto() { |
| 362 |
function F(){}; |
| 363 |
F.prototype = {c: 3}; |
| 364 |
|
| 365 |
var obj = new F; |
| 366 |
obj.a = 1; |
| 367 |
obj.b = 2; |
| 368 |
|
| 369 |
assertEquals( |
| 370 |
'Should not follow the prototype chain', '{"a":1,"b":2}', |
| 371 |
goog.json.serialize(obj)); |
| 372 |
} |
| 373 |
|
| 374 |
function testEscape() { |
| 375 |
var unescaped = '1a*/]'; |
| 376 |
assertEquals( |
| 377 |
'Should not escape', '"' + unescaped + '"', |
| 378 |
goog.json.serialize(unescaped)); |
| 379 |
|
| 380 |
var escaped = '\n\x7f\u1049'; |
| 381 |
assertEquals( |
| 382 |
'Should escape', '', |
| 383 |
findCommonChar(escaped, goog.json.serialize(escaped))); |
| 384 |
assertEquals( |
| 385 |
'Should eval to the same string after escaping', escaped, |
| 386 |
goog.json.parse(goog.json.serialize(escaped))); |
| 387 |
} |
| 388 |
|
| 389 |
function testReplacer() { |
| 390 |
assertSerialize('[null,null,0]', [, , 0]); |
| 391 |
|
| 392 |
assertSerialize('[0,0,{"x":0}]', [, , {x: 0}], function(k, v) { |
| 393 |
if (v === undefined && goog.isArray(this)) { |
| 394 |
return 0; |
| 395 |
} |
| 396 |
return v; |
| 397 |
}); |
| 398 |
|
| 399 |
assertSerialize('[0,1,2,3]', [0, 0, 0, 0], function(k, v) { |
| 400 |
var kNum = Number(k); |
| 401 |
if (k && !isNaN(kNum)) { |
| 402 |
return kNum; |
| 403 |
} |
| 404 |
return v; |
| 405 |
}); |
| 406 |
|
| 407 |
var f = function(k, v) { return typeof v == 'number' ? v + 1 : v; }; |
| 408 |
assertSerialize('{"a":1,"b":{"c":2}}', {'a': 0, 'b': {'c': 1}}, f); |
| 409 |
} |
| 410 |
|
| 411 |
function testDateSerialize() { |
| 412 |
assertSerialize('{}', new Date(0)); |
| 413 |
} |
| 414 |
|
| 415 |
function testToJSONSerialize() { |
| 416 |
assertSerialize('{}', {toJSON: goog.functions.constant('serialized')}); |
| 417 |
assertSerialize('{"toJSON":"normal"}', {toJSON: 'normal'}); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
function testTryNativeJson() { |
| 422 |
goog.json.TRY_NATIVE_JSON = true; |
| 423 |
var error; |
| 424 |
goog.json.setErrorLogger(function(message, ex) { |
| 425 |
error = message; |
| 426 |
}); |
| 427 |
|
| 428 |
error = undefined; |
| 429 |
goog.json.parse('{"a":[,1]}'); |
| 430 |
assertEquals('Invalid JSON: {"a":[,1]}', error); |
| 431 |
|
| 432 |
goog.json.TRY_NATIVE_JSON = false; |
| 433 |
goog.json.setErrorLogger(goog.nullFunction); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
/** |
| 438 |
* Asserts that the given object serializes to the given value. |
| 439 |
* If the current browser has an implementation of JSON.serialize, |
| 440 |
* we make sure our version matches that one. |
| 441 |
*/ |
| 442 |
function assertSerialize(expected, obj, opt_replacer) { |
| 443 |
assertEquals(expected, goog.json.serialize(obj, opt_replacer)); |
| 444 |
|
| 445 |
// goog.json.serialize escapes non-ASCI characters while JSON.stringify |
| 446 |
// doesn’t. This is expected so do not compare the results. |
| 447 |
if (typeof obj == 'string' && obj.charCodeAt(0) > 0x7f) return; |
| 448 |
|
| 449 |
// I'm pretty sure that the goog.json.serialize behavior is correct by the ES5 |
| 450 |
// spec, but JSON.stringify(undefined) is undefined on all browsers. |
| 451 |
if (obj === undefined) return; |
| 452 |
|
| 453 |
// Browsers don't serialize undefined properties, but goog.json.serialize does |
| 454 |
if (goog.isObject(obj) && ('a' in obj) && obj['a'] === undefined) return; |
| 455 |
|
| 456 |
// Replacers are broken on IE and older versions of firefox. |
| 457 |
if (opt_replacer && !goog.userAgent.WEBKIT) return; |
| 458 |
|
| 459 |
// goog.json.serialize does not stringify dates the same way. |
| 460 |
if (obj instanceof Date) return; |
| 461 |
|
| 462 |
// goog.json.serialize does not stringify functions the same way. |
| 463 |
if (obj instanceof Function) return; |
| 464 |
|
| 465 |
// goog.json.serialize doesn't use the toJSON method. |
| 466 |
if (goog.isObject(obj) && goog.isFunction(obj.toJSON)) return; |
| 467 |
|
| 468 |
if (typeof JSON != 'undefined') { |
| 469 |
assertEquals( |
| 470 |
'goog.json.serialize does not match JSON.stringify', expected, |
| 471 |
JSON.stringify(obj, opt_replacer)); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
/** |
| 477 |
* @param {string} a |
| 478 |
* @param {string} b |
| 479 |
* @return {string} any common character between two strings a and b. |
| 480 |
*/ |
| 481 |
function findCommonChar(a, b) { |
| 482 |
for (var i = 0; i < b.length; i++) { |
| 483 |
if (a.indexOf(b.charAt(i)) >= 0) { |
| 484 |
return b.charAt(i); |
| 485 |
} |
| 486 |
} |
| 487 |
return ''; |
| 488 |
} |