2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 * Provides methods to parse JSON strings and convert objects to JSON strings.
12 * @namespace YAHOO.lang
18 isFunction = l.isFunction,
19 isObject = l.isObject,
21 _toStr = Object.prototype.toString,
22 // 'this' is the global object. window in browser env. Keep
23 // the code env agnostic. Caja requies window, unfortunately.
24 Native = (YAHOO.env.ua.caja ? window : this).JSON,
26 /* Variables used by parse */
29 * Replace certain Unicode characters that JavaScript may handle incorrectly
30 * during eval--either by deleting them or treating them as line
31 * endings--with escape sequences.
32 * IMPORTANT NOTE: This regex will be used to modify the input if a match is
35 * @property _UNICODE_EXCEPTIONS
39 _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
42 * First step in the safety evaluation. Regex used to replace all escape
43 * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character).
50 _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
53 * Second step in the safety evaluation. Regex used to replace all simple
54 * values with ']' characters.
61 _VALUES = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
64 * Third step in the safety evaluation. Regex used to remove all open
65 * square brackets following a colon, comma, or at the beginning of the
73 _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g,
76 * Final step in the safety evaluation. Regex used to test the string left
77 * after all previous replacements for invalid characters.
84 _UNSAFE = /[^\],:{}\s]/,
87 /* Variables used by stringify */
90 * Regex used to replace special characters in strings for JSON
93 * @property _SPECIAL_CHARS
98 _SPECIAL_CHARS = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
101 * Character substitution map for common escapes and special characters.
118 UNDEFINED = 'undefined',
126 'undefined' : UNDEFINED,
128 '[object String]' : STRING,
130 '[object Number]' : NUMBER,
132 '[object Boolean]' : BOOLEAN,
133 '[object Date]' : DATE,
134 '[object RegExp]' : OBJECT
148 // Only accept JSON objects that report a [[Class]] of JSON
149 Native = _toStr.call(Native) === '[object JSON]' && Native;
151 // Escapes a special character to a safe Unicode representation
154 _CHARS[c] = '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4);
160 /* functions used by parse */
163 * Traverses nested objects, applying a filter or reviver function to
164 * each value. The value returned from the function will replace the
165 * original value in the key:value pair. If the value returned is
166 * undefined, the key will be omitted from the returned object.
169 * @param data {MIXED} Any JavaScript data
170 * @param reviver {Function} filter or mutation function
171 * @return {MIXED} The results of the filtered/mutated data structure
174 function _revive(data, reviver) {
175 var walk = function (o,key) {
176 var k,v,value = o[key];
177 if (value && typeof value === 'object') {
179 if (l.hasOwnProperty(value,k)) {
181 if (v === undefined) {
189 return reviver.call(o,key,value);
192 return typeof reviver === 'function' ? walk({'':data},'') : data;
196 * Replace certain Unicode characters that may be handled incorrectly by
197 * some browser implementations.
200 * @param s {String} parse input
201 * @return {String} sanitized JSON string ready to be validated/parsed
204 function _prepare(s) {
205 return s.replace(_UNICODE_EXCEPTIONS, _char);
208 function _isSafe(str) {
209 return l.isString(str) &&
210 !_UNSAFE.test(str.replace(_ESCAPES,'@').
211 replace(_VALUES,']').
212 replace(_BRACKETS,''));
215 function _parse(s,reviver) {
221 // Eval the text into a JavaScript data structure, apply the
222 // reviver function if provided, and return
223 return _revive( eval('(' + s + ')'), reviver );
226 // The text is not valid JSON
227 throw new SyntaxError('JSON.parse');
232 /* functions used by stringify */
234 // Utility function used to determine how to serialize a variable.
237 return _allowable[t] || // number, string, boolean, undefined
238 _allowable[_toStr.call(o)] || // Number, String, Boolean, Date
240 (o ? OBJECT : NULL) : // object, array, null, misc natives
241 UNDEFINED); // function, unknown
244 // Enclose escaped strings in quotes
245 function _string(s) {
246 return QUOTE + s.replace(_SPECIAL_CHARS, _char) + QUOTE;
249 // Adds the provided space to the beginning of every line in the input string
250 function _indent(s,space) {
251 return s.replace(/^/gm, space);
254 // JavaScript implementation of stringify (see API declaration of stringify)
255 function _stringify(o,w,space) {
256 if (o === undefined) {
260 var replacer = isFunction(w) ? w : null,
261 format = _toStr.call(space).match(/String|Number/) || [],
262 _date = YAHOO.lang.JSON.dateToString,
266 if (replacer || !isArray(w)) {
270 // Ensure whitelist keys are unique (bug 2110391)
273 for (i = 0, len = w.length; i < len; ++i) {
279 // Per the spec, strings are truncated to 10 characters and numbers
280 // are converted to that number of spaces (max 10)
281 space = format[0] === 'Number' ?
282 new Array(Math.min(Math.max(0,space),10)+1).join(" ") :
283 (space || EMPTY).slice(0,10);
285 function _serialize(h,key) {
289 colon = space ? COLON_SP : COLON,
292 // Per the ECMA 5 spec, toJSON is applied before the replacer is
293 // called. Also per the spec, Date.prototype.toJSON has been added, so
294 // Date instances should be serialized prior to exposure to the
295 // replacer. I disagree with this decision, but the spec is the spec.
296 if (isObject(value) && isFunction(value.toJSON)) {
297 value = value.toJSON(key);
298 } else if (t === DATE) {
299 value = _date(value);
302 if (isFunction(replacer)) {
303 value = replacer.call(h,key,value);
306 if (value !== h[key]) {
311 case DATE : // intentional fallthrough. Pre-replacer Dates are
312 // serialized in the toJSON stage. Dates here would
313 // have been produced by the replacer.
315 case STRING : return _string(value);
316 case NUMBER : return isFinite(value) ? value+EMPTY : NULL;
317 case BOOLEAN : return value+EMPTY;
318 case NULL : return NULL;
319 default : return undefined;
322 // Check for cyclical references in nested objects
323 for (i = stack.length - 1; i >= 0; --i) {
324 if (stack[i] === value) {
325 throw new Error("JSON.stringify. Cyclical reference");
329 arr = isArray(value);
331 // Add the object to the processing stack
335 for (i = value.length - 1; i >= 0; --i) {
336 a[i] = _serialize(value, i) || NULL;
339 // If whitelist provided, take only those keys
344 if (l.hasOwnProperty(keys, k)) {
345 v = _serialize(value, k);
347 a[i++] = _string(k) + colon + v;
353 // remove the array from the stack
356 if (space && a.length) {
358 OPEN_A + CR + _indent(a.join(COMMA_CR), space) + CR + CLOSE_A :
359 OPEN_O + CR + _indent(a.join(COMMA_CR), space) + CR + CLOSE_O;
362 OPEN_A + a.join(COMMA) + CLOSE_A :
363 OPEN_O + a.join(COMMA) + CLOSE_O;
368 return _serialize({'':o},'');
375 * Leverage native JSON parse if the browser has a native implementation.
376 * In general, this is a good idea. See the Known Issues section in the
377 * JSON user guide for caveats. The default value is true for browsers with
378 * native JSON support.
380 * @property useNativeParse
385 useNativeParse : !!Native,
388 * Leverage native JSON stringify if the browser has a native
389 * implementation. In general, this is a good idea. See the Known Issues
390 * section in the JSON user guide for caveats. The default value is true
391 * for browsers with native JSON support.
393 * @property useNativeStringify
398 useNativeStringify : !!Native,
401 * Four step determination whether a string is safe to eval. In three steps,
402 * escape sequences, safe values, and properly placed open square brackets
403 * are replaced with placeholders or removed. Then in the final step, the
404 * result of all these replacements is checked for invalid characters.
407 * @param str {String} JSON string to be tested
408 * @return {boolean} is the string safe for eval?
411 isSafe : function (s) {
412 return _isSafe(_prepare(s));
416 * <p>Parse a JSON string, returning the native JavaScript
417 * representation.</p>
419 * <p>When lang.JSON.useNativeParse is true, this will defer to the native
420 * JSON.parse if the browser has a native implementation. Otherwise, a
421 * JavaScript implementation based on http://www.json.org/json2.js
425 * @param s {string} JSON string data
426 * @param reviver {function} (optional) function(k,v) passed each key:value
427 * pair of object literals, allowing pruning or altering values
428 * @return {MIXED} the native JavaScript representation of the JSON string
429 * @throws SyntaxError
432 parse : function (s,reviver) {
433 if (typeof s !== 'string') {
437 return Native && YAHOO.lang.JSON.useNativeParse ?
438 Native.parse(s,reviver) : _parse(s,reviver);
442 * <p>Converts an arbitrary value to a JSON string representation.</p>
444 * <p>Objects with cyclical references will trigger an exception.</p>
446 * <p>If a whitelist is provided, only matching object keys will be
447 * included. Alternately, a replacer function may be passed as the
448 * second parameter. This function is executed on every value in the
449 * input, and its return value will be used in place of the original value.
450 * This is useful to serialize specialized objects or class instances.</p>
452 * <p>If a positive integer or non-empty string is passed as the third
453 * parameter, the output will be formatted with carriage returns and
454 * indentation for readability. If a String is passed (such as "\t") it
455 * will be used once for each indentation level. If a number is passed,
456 * that number of spaces will be used.</p>
458 * <p>When lang.JSON.useNativeStringify is true, this will defer to the
459 * native JSON.stringify if the browser has a native implementation.
460 * Otherwise, a JavaScript implementation is used.</p>
463 * @param o {MIXED} any arbitrary object to convert to JSON string
464 * @param w {Array|Function} (optional) whitelist of acceptable object keys
465 * to include OR a function(value,key) to alter values
466 * before serialization
467 * @param space {Number|String} (optional) indentation character(s) or
468 * depthy of spaces to format the output
469 * @return {string} JSON string representation of the input
473 stringify : function (o,w,space) {
474 return Native && YAHOO.lang.JSON.useNativeStringify ?
475 Native.stringify(o,w,space) : _stringify(o,w,space);
479 * Serializes a Date instance as a UTC date string. Used internally by
480 * the JavaScript implementation of stringify. If you need a different
481 * Date serialization format, override this method. If you change this,
482 * you should also set useNativeStringify to false, since native JSON
483 * implementations serialize Dates per the ECMAScript 5 spec. You've been
486 * @method dateToString
487 * @param d {Date} The Date to serialize
488 * @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ
491 dateToString : function (d) {
492 function _zeroPad(v) {
493 return v < 10 ? '0' + v : v;
496 return d.getUTCFullYear() + '-' +
497 _zeroPad(d.getUTCMonth() + 1) + '-' +
498 _zeroPad(d.getUTCDate()) + 'T' +
499 _zeroPad(d.getUTCHours()) + COLON +
500 _zeroPad(d.getUTCMinutes()) + COLON +
501 _zeroPad(d.getUTCSeconds()) + 'Z';
505 * Reconstitute Date instances from the default JSON UTC serialization.
506 * Reference this from a reviver function to rebuild Dates during the
509 * @method stringToDate
510 * @param str {String} String serialization of a Date
513 stringToDate : function (str) {
514 var m = str.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?Z$/);
517 d.setUTCFullYear(m[1], m[2]-1, m[3]);
518 d.setUTCHours(m[4], m[5], m[6], (m[7] || 0));
526 * <p>Four step determination whether a string is safe to eval. In three steps,
527 * escape sequences, safe values, and properly placed open square brackets
528 * are replaced with placeholders or removed. Then in the final step, the
529 * result of all these replacements is checked for invalid characters.</p>
531 * <p>This is an alias for isSafe.</p>
534 * @param str {String} JSON string to be tested
535 * @return {boolean} is the string safe for eval?
537 * @deprecated use isSafe
539 YAHOO.lang.JSON.isValid = YAHOO.lang.JSON.isSafe;
542 YAHOO.register("json", YAHOO.lang.JSON, {version: "2.9.0", build: "2800"});