2 http://www.JSON.org/json_parse.js
7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
9 This file creates a json_parse function.
11 json_parse(text, reviver)
12 This method parses a JSON text to produce an object or array.
13 It can throw a SyntaxError exception.
15 The optional reviver parameter is a function that can filter and
16 transform the results. It receives each of the keys and values,
17 and its return value is used instead of the original value.
18 If it returns what it received, then the structure is not modified.
19 If it returns undefined then the member is deleted.
23 // Parse the text. Values that look like ISO date strings will
24 // be converted to Date objects.
26 myData = json_parse(text, function (key, value) {
28 if (typeof value === 'string') {
30 /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
32 return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
39 This is a reference implementation. You are free to copy, modify, or
42 This code should be minified before deployment.
43 See http://javascript.crockford.com/jsmin.html
45 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
49 /*members "", "\"", "\/", "\\", at, b, call, charAt, f, fromCharCode,
50 hasOwnProperty, message, n, name, push, r, t, text
53 /*global json_parse */
55 json_parse = (function () {
57 // This is a function that can parse a JSON text, producing a JavaScript
58 // data structure. It is a simple, recursive descent parser. It does not use
59 // eval or regular expressions, so it can be used as a model for implementing
60 // a JSON parser in other languages.
62 // We are defining the function inside of another function to avoid creating
65 var at, // The index of the current character
66 ch, // The current character
79 error = function (m) {
81 // Call error when something is wrong.
93 // If a c parameter is provided, verify that it matches the current character.
96 error("Expected '" + c + "' instead of '" + ch + "'");
99 // Get the next character. When there are no more characters,
100 // return the empty string.
102 ch = text.charAt(at);
107 number = function () {
109 // Parse a number value.
118 while (ch >= '0' && ch <= '9') {
124 while (next() && ch >= '0' && ch <= '9') {
128 if (ch === 'e' || ch === 'E') {
131 if (ch === '-' || ch === '+') {
135 while (ch >= '0' && ch <= '9') {
148 string = function () {
150 // Parse a string value.
157 // When parsing for string values, we must look for " and \ characters.
164 } else if (ch === '\\') {
168 for (i = 0; i < 4; i += 1) {
169 hex = parseInt(next(), 16);
170 if (!isFinite(hex)) {
173 uffff = uffff * 16 + hex;
175 string += String.fromCharCode(uffff);
176 } else if (typeof escapee[ch] === 'string') {
177 string += escapee[ch];
189 white = function () {
193 while (ch && ch <= ' ') {
200 // true, false, or null.
223 error("Unexpected '" + ch + "'");
226 value, // Place holder for the value function.
228 array = function () {
230 // Parse an array value.
239 return array; // empty array
255 object = function () {
257 // Parse an object value.
267 return object; // empty object
273 if (Object.hasOwnProperty.call(object, key)) {
274 error('Duplicate key "' + key + '"');
276 object[key] = value();
289 value = function () {
291 // Parse a JSON value. It could be an object, an array, a string, a number,
305 return ch >= '0' && ch <= '9' ? number() : word();
309 // Return the json_parse function. It will have access to all of the above
310 // functions and variables.
312 return function (source, reviver) {
321 error("Syntax error");
324 // If there is a reviver function, we recursively walk the new structure,
325 // passing each name/value pair to the reviver function for possible
326 // transformation, starting with a temporary root object that holds the result
327 // in an empty key. If there is not a reviver function, we simply return the
330 return typeof reviver === 'function' ? (function walk(holder, key) {
331 var k, v, value = holder[key];
332 if (value && typeof value === 'object') {
334 if (Object.hasOwnProperty.call(value, k)) {
336 if (v !== undefined) {
344 return reviver.call(holder, key, value);
345 }({'': result}, '')) : result;