|
0
|
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' ); |
|
|
238 |
YUI.add('json-stringify', function(Y) { |
|
|
239 |
|
|
|
240 |
/** |
|
|
241 |
* Provides Y.JSON.stringify method for converting objects to JSON strings. |
|
|
242 |
* |
|
|
243 |
* @module json |
|
|
244 |
* @submodule json-stringify |
|
|
245 |
* @for JSON |
|
|
246 |
* @static |
|
|
247 |
*/ |
|
|
248 |
var _toString = Object.prototype.toString, |
|
|
249 |
Lang = Y.Lang, |
|
|
250 |
isFunction= Lang.isFunction, |
|
|
251 |
isArray = Lang.isArray, |
|
|
252 |
type = Lang.type, |
|
|
253 |
STRING = 'string', |
|
|
254 |
NUMBER = 'number', |
|
|
255 |
BOOLEAN = 'boolean', |
|
|
256 |
OBJECT = 'object', |
|
|
257 |
ARRAY = 'array', |
|
|
258 |
REGEXP = 'regexp', |
|
|
259 |
ERROR = 'error', |
|
|
260 |
NULL = 'null', |
|
|
261 |
DATE = 'date', |
|
|
262 |
EMPTY = '', |
|
|
263 |
OPEN_O = '{', |
|
|
264 |
CLOSE_O = '}', |
|
|
265 |
OPEN_A = '[', |
|
|
266 |
CLOSE_A = ']', |
|
|
267 |
COMMA = ',', |
|
|
268 |
COMMA_CR = ",\n", |
|
|
269 |
CR = "\n", |
|
|
270 |
COLON = ':', |
|
|
271 |
COLON_SP = ': ', |
|
|
272 |
QUOTE = '"'; |
|
|
273 |
|
|
|
274 |
Y.mix(Y.namespace('JSON'),{ |
|
|
275 |
/** |
|
|
276 |
* Regex used to capture characters that need escaping before enclosing |
|
|
277 |
* their containing string in quotes. |
|
|
278 |
* |
|
|
279 |
* @property _SPECIAL_CHARS |
|
|
280 |
* @type {RegExp} |
|
|
281 |
* @protected |
|
|
282 |
*/ |
|
|
283 |
_SPECIAL_CHARS : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
|
|
284 |
|
|
|
285 |
/** |
|
|
286 |
* Character substitution map for common escapes and special characters. |
|
|
287 |
* |
|
|
288 |
* @property _CHARS |
|
|
289 |
* @type {Object} |
|
|
290 |
* @static |
|
|
291 |
* @protected |
|
|
292 |
*/ |
|
|
293 |
_CHARS : { |
|
|
294 |
'\b': '\\b', |
|
|
295 |
'\t': '\\t', |
|
|
296 |
'\n': '\\n', |
|
|
297 |
'\f': '\\f', |
|
|
298 |
'\r': '\\r', |
|
|
299 |
'"' : '\\"', |
|
|
300 |
'\\': '\\\\' |
|
|
301 |
}, |
|
|
302 |
|
|
|
303 |
/** |
|
|
304 |
* Serializes a Date instance as a UTC date string. Used internally by |
|
|
305 |
* stringify. Override this method if you need Dates serialized in a |
|
|
306 |
* different format. |
|
|
307 |
* |
|
|
308 |
* @method dateToString |
|
|
309 |
* @param d {Date} The Date to serialize |
|
|
310 |
* @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ |
|
|
311 |
* @static |
|
|
312 |
*/ |
|
|
313 |
dateToString : function (d) { |
|
|
314 |
function _zeroPad(v) { |
|
|
315 |
return v < 10 ? '0' + v : v; |
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
return QUOTE + d.getUTCFullYear() + '-' + |
|
|
319 |
_zeroPad(d.getUTCMonth() + 1) + '-' + |
|
|
320 |
_zeroPad(d.getUTCDate()) + 'T' + |
|
|
321 |
_zeroPad(d.getUTCHours()) + COLON + |
|
|
322 |
_zeroPad(d.getUTCMinutes()) + COLON + |
|
|
323 |
_zeroPad(d.getUTCSeconds()) + 'Z' + QUOTE; |
|
|
324 |
}, |
|
|
325 |
|
|
|
326 |
/** |
|
|
327 |
* <p>Converts an arbitrary value to a JSON string representation.</p> |
|
|
328 |
* |
|
|
329 |
* <p>Objects with cyclical references will trigger an exception.</p> |
|
|
330 |
* |
|
|
331 |
* <p>If a whitelist is provided, only matching object keys will be |
|
|
332 |
* included. Alternately, a replacer function may be passed as the |
|
|
333 |
* second parameter. This function is executed on every value in the |
|
|
334 |
* input, and its return value will be used in place of the original value. |
|
|
335 |
* This is useful to serialize specialized objects or class instances.</p> |
|
|
336 |
* |
|
|
337 |
* <p>If a positive integer or non-empty string is passed as the third |
|
|
338 |
* parameter, the output will be formatted with carriage returns and |
|
|
339 |
* indentation for readability. If a String is passed (such as "\t") it |
|
|
340 |
* will be used once for each indentation level. If a number is passed, |
|
|
341 |
* that number of spaces will be used.</p> |
|
|
342 |
* |
|
|
343 |
* @method stringify |
|
|
344 |
* @param o {MIXED} any arbitrary value to convert to JSON string |
|
|
345 |
* @param w {Array|Function} (optional) whitelist of acceptable object |
|
|
346 |
* keys to include, or a replacer function to modify the |
|
|
347 |
* raw value before serialization |
|
|
348 |
* @param ind {Number|String} (optional) indentation character or depth of |
|
|
349 |
* spaces to format the output. |
|
|
350 |
* @return {string} JSON string representation of the input |
|
|
351 |
* @static |
|
|
352 |
*/ |
|
|
353 |
stringify : function (o,w,ind) { |
|
|
354 |
|
|
|
355 |
var m = Y.JSON._CHARS, |
|
|
356 |
str_re = Y.JSON._SPECIAL_CHARS, |
|
|
357 |
rep = isFunction(w) ? w : null, |
|
|
358 |
pstack = [], // Processing stack used for cyclical ref protection |
|
|
359 |
_date = Y.JSON.dateToString, // Use configured date serialization |
|
|
360 |
format = _toString.call(ind).match(/String|Number/); |
|
|
361 |
|
|
|
362 |
if (rep || typeof w !== 'object') { |
|
|
363 |
w = undefined; |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
if (format) { |
|
|
367 |
// String instances and primatives are left alone. String objects |
|
|
368 |
// coerce for the indenting operations. |
|
|
369 |
if (format[0] === 'Number') { |
|
|
370 |
|
|
|
371 |
// force numeric indent values between {0,100} per the spec |
|
|
372 |
// This also converts Number instances to primative |
|
|
373 |
ind = new Array(Math.min(Math.max(0,ind),100)+1).join(" "); |
|
|
374 |
} |
|
|
375 |
|
|
|
376 |
// turn off formatting for 0 or empty string |
|
|
377 |
format = ind; |
|
|
378 |
} |
|
|
379 |
|
|
|
380 |
// escape encode special characters |
|
|
381 |
function _char(c) { |
|
|
382 |
if (!m[c]) { |
|
|
383 |
m[c]='\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4); |
|
|
384 |
} |
|
|
385 |
return m[c]; |
|
|
386 |
} |
|
|
387 |
|
|
|
388 |
// Enclose the escaped string in double quotes |
|
|
389 |
function _string(s) { |
|
|
390 |
return QUOTE + s.replace(str_re, _char) + QUOTE; |
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
// Check for cyclical references |
|
|
394 |
function _cyclicalTest(o) { |
|
|
395 |
for (var i = pstack.length - 1; i >= 0; --i) { |
|
|
396 |
if (pstack[i] === o) { |
|
|
397 |
throw new Error("JSON.stringify. Cyclical reference"); |
|
|
398 |
} |
|
|
399 |
} |
|
|
400 |
return false; |
|
|
401 |
} |
|
|
402 |
|
|
|
403 |
function _indent(s) { |
|
|
404 |
return s.replace(/^/gm,ind); |
|
|
405 |
} |
|
|
406 |
|
|
|
407 |
function _object(o,arr) { |
|
|
408 |
// Add the object to the processing stack |
|
|
409 |
pstack.push(o); |
|
|
410 |
|
|
|
411 |
var a = [], |
|
|
412 |
colon = format ? COLON_SP : COLON, |
|
|
413 |
i, j, len, k, v; |
|
|
414 |
|
|
|
415 |
if (arr) { // Array |
|
|
416 |
for (i = o.length - 1; i >= 0; --i) { |
|
|
417 |
a[i] = _stringify(o,i) || NULL; |
|
|
418 |
} |
|
|
419 |
} else { // Object |
|
|
420 |
// If whitelist provided, take only those keys |
|
|
421 |
k = isArray(w) ? w : Y.Object.keys(o); |
|
|
422 |
|
|
|
423 |
for (i = 0, j = 0, len = k.length; i < len; ++i) { |
|
|
424 |
if (typeof k[i] === STRING) { |
|
|
425 |
v = _stringify(o,k[i]); |
|
|
426 |
if (v) { |
|
|
427 |
a[j++] = _string(k[i]) + colon + v; |
|
|
428 |
} |
|
|
429 |
} |
|
|
430 |
} |
|
|
431 |
} |
|
|
432 |
|
|
|
433 |
// remove the array from the stack |
|
|
434 |
pstack.pop(); |
|
|
435 |
|
|
|
436 |
if (format && a.length) { |
|
|
437 |
return arr ? |
|
|
438 |
OPEN_A + CR + _indent(a.join(COMMA_CR)) + CR + CLOSE_A : |
|
|
439 |
OPEN_O + CR + _indent(a.join(COMMA_CR)) + CR + CLOSE_O; |
|
|
440 |
} else { |
|
|
441 |
return arr ? |
|
|
442 |
OPEN_A + a.join(COMMA) + CLOSE_A : |
|
|
443 |
OPEN_O + a.join(COMMA) + CLOSE_O; |
|
|
444 |
} |
|
|
445 |
} |
|
|
446 |
|
|
|
447 |
// Worker function. Fork behavior on data type and recurse objects. |
|
|
448 |
function _stringify(h,key) { |
|
|
449 |
var o = isFunction(rep) ? rep.call(h,key,h[key]) : h[key], |
|
|
450 |
t = type(o); |
|
|
451 |
|
|
|
452 |
if (t === OBJECT) { |
|
|
453 |
if (/String|Number|Boolean/.test(_toString.call(o))) { |
|
|
454 |
o = o.valueOf(); |
|
|
455 |
t = type(o); |
|
|
456 |
} |
|
|
457 |
} |
|
|
458 |
|
|
|
459 |
switch (t) { |
|
|
460 |
case STRING : return _string(o); |
|
|
461 |
case NUMBER : return isFinite(o) ? o+EMPTY : NULL; |
|
|
462 |
case BOOLEAN : return o+EMPTY; |
|
|
463 |
case DATE : return _date(o); |
|
|
464 |
case NULL : return NULL; |
|
|
465 |
case ARRAY : _cyclicalTest(o); return _object(o,true); |
|
|
466 |
case REGEXP : // intentional fall through |
|
|
467 |
case ERROR : // intentional fall through |
|
|
468 |
case OBJECT : _cyclicalTest(o); return _object(o); |
|
|
469 |
default : return undefined; |
|
|
470 |
} |
|
|
471 |
} |
|
|
472 |
|
|
|
473 |
// process the input |
|
|
474 |
return _stringify({'':o},EMPTY); |
|
|
475 |
} |
|
|
476 |
}); |
|
|
477 |
|
|
|
478 |
|
|
|
479 |
}, '3.0.0b1' ); |
|
|
480 |
|
|
|
481 |
|
|
|
482 |
YUI.add('json', function(Y){}, '3.0.0b1' ,{use:['json-parse', 'json-stringify']}); |
|
|
483 |
|