2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('json-parse-shim', function (Y, NAME) {
11 * <p>The JSON module adds support for serializing JavaScript objects into
12 * JSON strings and parsing JavaScript objects from strings in JSON format.</p>
14 * <p>The JSON namespace is added to your YUI instance including static methods
15 * Y.JSON.parse(..) and Y.JSON.stringify(..).</p>
17 * <p>The functionality and method signatures follow the ECMAScript 5
18 * specification. In browsers with native JSON support, the native
19 * implementation is used.</p>
21 * <p>The <code>json</code> module is a rollup of <code>json-parse</code> and
22 * <code>json-stringify</code>.</p>
24 * <p>As their names suggest, <code>json-parse</code> adds support for parsing
25 * JSON data (Y.JSON.parse) and <code>json-stringify</code> for serializing
26 * JavaScript data into JSON strings (Y.JSON.stringify). You may choose to
27 * include either of the submodules individually if you don't need the
28 * complementary functionality, or include the rollup for both.</p>
37 * Provides Y.JSON.parse method to accept JSON strings and return native
41 * @submodule json-parse
48 * Replace certain Unicode characters that JavaScript may handle incorrectly
49 * during eval--either by deleting them or treating them as line
50 * endings--with escape sequences.
51 * IMPORTANT NOTE: This regex will be used to modify the input if a match is
54 * @property _UNICODE_EXCEPTIONS
58 var _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
62 * First step in the safety evaluation. Regex used to replace all escape
63 * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character).
69 _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
72 * Second step in the safety evaluation. Regex used to replace all simple
73 * values with ']' characters.
79 _VALUES = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
82 * Third step in the safety evaluation. Regex used to remove all open
83 * square brackets following a colon, comma, or at the beginning of the
90 _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g,
93 * Final step in the safety evaluation. Regex used to test the string left
94 * after all previous replacements for invalid characters.
100 _UNSAFE = /[^\],:{}\s]/,
103 * Replaces specific unicode characters with their appropriate \unnnn
104 * format. Some browsers ignore certain characters during eval.
106 * @method escapeException
107 * @param c {String} Unicode character
108 * @return {String} the \unnnn escapement of the character
111 _escapeException = function (c) {
112 return '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4);
116 * Traverses nested objects, applying a reviver function to each (key,value)
117 * from the scope if the key:value's containing object. The value returned
118 * from the function will replace the original value in the key:value pair.
119 * If the value returned is undefined, the key will be omitted from the
123 * @param data {MIXED} Any JavaScript data
124 * @param reviver {Function} filter or mutation function
125 * @return {MIXED} The results of the filtered data
128 _revive = function (data, reviver) {
129 var walk = function (o,key) {
130 var k,v,value = o[key];
131 if (value && typeof value === 'object') {
133 if (value.hasOwnProperty(k)) {
135 if (v === undefined) {
143 return reviver.call(o,key,value);
146 return typeof reviver === 'function' ? walk({'':data},'') : data;
150 * Parse a JSON string, returning the native JavaScript representation.
152 * @param s {string} JSON string data
153 * @param reviver {function} (optional) function(k,v) passed each key value
154 * pair of object literals, allowing pruning or altering values
155 * @return {MIXED} the native JavaScript representation of the JSON string
156 * @throws SyntaxError
160 // JavaScript implementation in lieu of native browser support. Based on
161 // the json2.js library from http://json.org
162 Y.JSON.parse = function (s,reviver) {
163 if (typeof s !== 'string') {
167 // Replace certain Unicode characters that are otherwise handled
168 // incorrectly by some browser implementations.
169 // NOTE: This modifies the input if such characters are found!
170 s = s.replace(_UNICODE_EXCEPTIONS, _escapeException);
172 // Test for any remaining invalid characters
173 if (!_UNSAFE.test(s.replace(_ESCAPES,'@').
174 replace(_VALUES,']').
175 replace(_BRACKETS,''))) {
177 // Eval the text into a JavaScript data structure, apply any
178 // reviver function, and return
179 return _revive(eval('(' + s + ')'), reviver);
182 throw new SyntaxError('JSON.parse');
185 // Property available for testing if the implementation being used
186 // is native or a shim
187 Y.JSON.parse.isShim = true;
190 }, '3.13.0', {"requires": ["json-parse"]});