|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('json-parse-shim', function (Y, NAME) { |
|
9 |
|
10 /** |
|
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> |
|
13 * |
|
14 * <p>The JSON namespace is added to your YUI instance including static methods |
|
15 * Y.JSON.parse(..) and Y.JSON.stringify(..).</p> |
|
16 * |
|
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> |
|
20 * |
|
21 * <p>The <code>json</code> module is a rollup of <code>json-parse</code> and |
|
22 * <code>json-stringify</code>.</p> |
|
23 * |
|
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> |
|
29 * |
|
30 * @module json |
|
31 * @main json |
|
32 * @class JSON |
|
33 * @static |
|
34 */ |
|
35 |
|
36 /** |
|
37 * Provides Y.JSON.parse method to accept JSON strings and return native |
|
38 * JavaScript objects. |
|
39 * |
|
40 * @module json |
|
41 * @submodule json-parse |
|
42 * @for JSON |
|
43 * @static |
|
44 */ |
|
45 |
|
46 |
|
47 /** |
|
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 |
|
52 * found. |
|
53 * |
|
54 * @property _UNICODE_EXCEPTIONS |
|
55 * @type {RegExp} |
|
56 * @private |
|
57 */ |
|
58 var _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
|
59 |
|
60 |
|
61 /** |
|
62 * First step in the safety evaluation. Regex used to replace all escape |
|
63 * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character). |
|
64 * |
|
65 * @property _ESCAPES |
|
66 * @type {RegExp} |
|
67 * @private |
|
68 */ |
|
69 _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, |
|
70 |
|
71 /** |
|
72 * Second step in the safety evaluation. Regex used to replace all simple |
|
73 * values with ']' characters. |
|
74 * |
|
75 * @property _VALUES |
|
76 * @type {RegExp} |
|
77 * @private |
|
78 */ |
|
79 _VALUES = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, |
|
80 |
|
81 /** |
|
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 |
|
84 * string. |
|
85 * |
|
86 * @property _BRACKETS |
|
87 * @type {RegExp} |
|
88 * @private |
|
89 */ |
|
90 _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g, |
|
91 |
|
92 /** |
|
93 * Final step in the safety evaluation. Regex used to test the string left |
|
94 * after all previous replacements for invalid characters. |
|
95 * |
|
96 * @property _UNSAFE |
|
97 * @type {RegExp} |
|
98 * @private |
|
99 */ |
|
100 _UNSAFE = /[^\],:{}\s]/, |
|
101 |
|
102 /** |
|
103 * Replaces specific unicode characters with their appropriate \unnnn |
|
104 * format. Some browsers ignore certain characters during eval. |
|
105 * |
|
106 * @method escapeException |
|
107 * @param c {String} Unicode character |
|
108 * @return {String} the \unnnn escapement of the character |
|
109 * @private |
|
110 */ |
|
111 _escapeException = function (c) { |
|
112 return '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4); |
|
113 }, |
|
114 |
|
115 /** |
|
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 |
|
120 * returned object. |
|
121 * |
|
122 * @method _revive |
|
123 * @param data {MIXED} Any JavaScript data |
|
124 * @param reviver {Function} filter or mutation function |
|
125 * @return {MIXED} The results of the filtered data |
|
126 * @private |
|
127 */ |
|
128 _revive = function (data, reviver) { |
|
129 var walk = function (o,key) { |
|
130 var k,v,value = o[key]; |
|
131 if (value && typeof value === 'object') { |
|
132 for (k in value) { |
|
133 if (value.hasOwnProperty(k)) { |
|
134 v = walk(value, k); |
|
135 if (v === undefined) { |
|
136 delete value[k]; |
|
137 } else { |
|
138 value[k] = v; |
|
139 } |
|
140 } |
|
141 } |
|
142 } |
|
143 return reviver.call(o,key,value); |
|
144 }; |
|
145 |
|
146 return typeof reviver === 'function' ? walk({'':data},'') : data; |
|
147 }; |
|
148 |
|
149 /** |
|
150 * Parse a JSON string, returning the native JavaScript representation. |
|
151 * |
|
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 |
|
157 * @method parse |
|
158 * @static |
|
159 */ |
|
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') { |
|
164 s += ''; |
|
165 } |
|
166 |
|
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); |
|
171 |
|
172 // Test for any remaining invalid characters |
|
173 if (!_UNSAFE.test(s.replace(_ESCAPES,'@'). |
|
174 replace(_VALUES,']'). |
|
175 replace(_BRACKETS,''))) { |
|
176 |
|
177 // Eval the text into a JavaScript data structure, apply any |
|
178 // reviver function, and return |
|
179 return _revive(eval('(' + s + ')'), reviver); |
|
180 } |
|
181 |
|
182 throw new SyntaxError('JSON.parse'); |
|
183 }; |
|
184 |
|
185 // Property available for testing if the implementation being used |
|
186 // is native or a shim |
|
187 Y.JSON.parse.isShim = true; |
|
188 |
|
189 |
|
190 }, '3.10.3', {"requires": ["json-parse"]}); |