|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('json-parse', function(Y) { |
|
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 * @class JSON |
|
32 * @static |
|
33 */ |
|
34 |
|
35 /** |
|
36 * Provides Y.JSON.parse method to accept JSON strings and return native |
|
37 * JavaScript objects. |
|
38 * |
|
39 * @module json |
|
40 * @submodule json-parse |
|
41 * @for JSON |
|
42 * @static |
|
43 */ |
|
44 |
|
45 |
|
46 // All internals kept private for security reasons |
|
47 |
|
48 |
|
49 /** |
|
50 * Alias to native browser implementation of the JSON object if available. |
|
51 * |
|
52 * @property Native |
|
53 * @type {Object} |
|
54 * @private |
|
55 */ |
|
56 var Native = Y.config.win.JSON, |
|
57 |
|
58 /** |
|
59 * Replace certain Unicode characters that JavaScript may handle incorrectly |
|
60 * during eval--either by deleting them or treating them as line |
|
61 * endings--with escape sequences. |
|
62 * IMPORTANT NOTE: This regex will be used to modify the input if a match is |
|
63 * found. |
|
64 * |
|
65 * @property _UNICODE_EXCEPTIONS |
|
66 * @type {RegExp} |
|
67 * @private |
|
68 */ |
|
69 _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
|
70 |
|
71 |
|
72 /** |
|
73 * First step in the safety evaluation. Regex used to replace all escape |
|
74 * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character). |
|
75 * |
|
76 * @property _ESCAPES |
|
77 * @type {RegExp} |
|
78 * @private |
|
79 */ |
|
80 _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, |
|
81 |
|
82 /** |
|
83 * Second step in the safety evaluation. Regex used to replace all simple |
|
84 * values with ']' characters. |
|
85 * |
|
86 * @property _VALUES |
|
87 * @type {RegExp} |
|
88 * @private |
|
89 */ |
|
90 _VALUES = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, |
|
91 |
|
92 /** |
|
93 * Third step in the safety evaluation. Regex used to remove all open |
|
94 * square brackets following a colon, comma, or at the beginning of the |
|
95 * string. |
|
96 * |
|
97 * @property _BRACKETS |
|
98 * @type {RegExp} |
|
99 * @private |
|
100 */ |
|
101 _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g, |
|
102 |
|
103 /** |
|
104 * Final step in the safety evaluation. Regex used to test the string left |
|
105 * after all previous replacements for invalid characters. |
|
106 * |
|
107 * @property _INVALID |
|
108 * @type {RegExp} |
|
109 * @private |
|
110 */ |
|
111 _INVALID = /[^\],:{}\s]/, |
|
112 |
|
113 /** |
|
114 * Test for JSON string of simple data string, number, boolean, or null. |
|
115 * E.g. '"some string"', "true", "false", "null", or numbers "-123e+7" |
|
116 * This was needed for some WIP native implementations (FF3.1b2) but may be |
|
117 * unnecessary now. |
|
118 * |
|
119 * @property _SIMPLE |
|
120 * @type {RegExp} |
|
121 * @private |
|
122 */ |
|
123 _SIMPLE = /^\s*(?:"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)\s*$/, |
|
124 |
|
125 /** |
|
126 * Replaces specific unicode characters with their appropriate \unnnn |
|
127 * format. Some browsers ignore certain characters during eval. |
|
128 * |
|
129 * @method escapeException |
|
130 * @param c {String} Unicode character |
|
131 * @return {String} the \unnnn escapement of the character |
|
132 * @private |
|
133 */ |
|
134 _escapeException = function (c) { |
|
135 return '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4); |
|
136 }, |
|
137 |
|
138 /** |
|
139 * Traverses nested objects, applying a reviver function to each (key,value) |
|
140 * from the scope if the key:value's containing object. The value returned |
|
141 * from the function will replace the original value in the key:value pair. |
|
142 * If the value returned is undefined, the key will be omitted from the |
|
143 * returned object. |
|
144 * |
|
145 * @method _revive |
|
146 * @param data {MIXED} Any JavaScript data |
|
147 * @param reviver {Function} filter or mutation function |
|
148 * @return {MIXED} The results of the filtered data |
|
149 * @private |
|
150 */ |
|
151 _revive = function (data, reviver) { |
|
152 var walk = function (o,key) { |
|
153 var k,v,value = o[key]; |
|
154 if (value && typeof value === 'object') { |
|
155 for (k in value) { |
|
156 if (value.hasOwnProperty(k)) { |
|
157 v = walk(value, k); |
|
158 if (v === undefined) { |
|
159 delete value[k]; |
|
160 } else { |
|
161 value[k] = v; |
|
162 } |
|
163 } |
|
164 } |
|
165 } |
|
166 return reviver.call(o,key,value); |
|
167 }; |
|
168 |
|
169 return typeof reviver === 'function' ? walk({'':data},'') : data; |
|
170 }, |
|
171 |
|
172 /** |
|
173 * Parse a JSON string, returning the native JavaScript representation. |
|
174 * |
|
175 * @param s {string} JSON string data |
|
176 * @param reviver {function} (optional) function(k,v) passed each key value |
|
177 * pair of object literals, allowing pruning or altering values |
|
178 * @return {MIXED} the native JavaScript representation of the JSON string |
|
179 * @throws SyntaxError |
|
180 * @method parse |
|
181 * @static |
|
182 */ |
|
183 // JavaScript implementation in lieu of native browser support. Based on |
|
184 // the json2.js library from http://json.org |
|
185 _parse = function (s,reviver) { |
|
186 if (typeof s === 'string') { |
|
187 // Replace certain Unicode characters that are otherwise handled |
|
188 // incorrectly by some browser implementations. |
|
189 // NOTE: This modifies the input if such characters are found! |
|
190 s = s.replace(_UNICODE_EXCEPTIONS, _escapeException); |
|
191 |
|
192 // Test for any remaining invalid characters |
|
193 if (!_INVALID.test(s.replace(_ESCAPES,'@'). |
|
194 replace(_VALUES,']'). |
|
195 replace(_BRACKETS,''))) { |
|
196 |
|
197 // Eval the text into a JavaScript data structure, apply any |
|
198 // reviver function, and return |
|
199 return _revive( eval('(' + s + ')'), reviver ); |
|
200 } |
|
201 } |
|
202 |
|
203 throw new SyntaxError('JSON.parse'); |
|
204 }, |
|
205 |
|
206 test; |
|
207 |
|
208 |
|
209 // Test the level of native browser support |
|
210 if (Native && Object.prototype.toString.call(Native) === '[object JSON]') { |
|
211 try { |
|
212 test = Native.parse('{"x":1}', function (k,v) {return k=='x' ? 2 : v;}); |
|
213 switch (test.x) { |
|
214 case 1 : // Reviver not supported |
|
215 _parse = function (s,reviver) { |
|
216 return _SIMPLE.test(s) ? |
|
217 eval('(' + s + ')') : |
|
218 _revive(Native.parse(s), reviver); |
|
219 }; |
|
220 break; |
|
221 |
|
222 case 2 : // Full support |
|
223 _parse = function (s, reviver) { |
|
224 return Native.parse(s, reviver); |
|
225 }; |
|
226 break; |
|
227 |
|
228 // default is JS implementation |
|
229 } |
|
230 } |
|
231 catch (e) {} // defer to JS implementation |
|
232 } |
|
233 |
|
234 Y.mix(Y.namespace('JSON'),{ parse : _parse }); |
|
235 |
|
236 |
|
237 }, '3.0.0b1' ); |