json_parse = (function () {
// This is a function that can parse a JSON text, producing a JavaScript
// data structure. It is a simple, recursive descent parser. It does not use
// eval or regular expressions, so it can be used as a model for implementing
// a JSON parser in other languages.
// We are defining the function inside of another function to avoid creating
var at, // The index of the current character
ch, // The current character
// Call error when something is wrong.
// If a c parameter is provided, verify that it matches the current character.
error("Expected '" + c + "' instead of '" + ch + "'");
// Get the next character. When there are no more characters,
// return the empty string.
while (ch >= '0' && ch <= '9') {
while (next() && ch >= '0' && ch <= '9') {
if (ch === 'e' || ch === 'E') {
if (ch === '-' || ch === '+') {
while (ch >= '0' && ch <= '9') {
// When parsing for string values, we must look for " and \ characters.
} else if (ch === '\\') {
for (i = 0; i < 4; i += 1) {
hex = parseInt(next(), 16);
uffff = uffff * 16 + hex;
string += String.fromCharCode(uffff);
} else if (typeof escapee[ch] === 'string') {
while (ch && ch <= ' ') {
error("Unexpected '" + ch + "'");
value, // Place holder for the value function.
return array; // empty array
// Parse an object value.
return object; // empty object
if (Object.hasOwnProperty.call(object, key)) {
error('Duplicate key "' + key + '"');
// Parse a JSON value. It could be an object, an array, a string, a number,
return ch >= '0' && ch <= '9' ? number() : word();
// Return the json_parse function. It will have access to all of the above
// functions and variables.
return function (source, reviver) {
// If there is a reviver function, we recursively walk the new structure,
// passing each name/value pair to the reviver function for possible
// transformation, starting with a temporary root object that holds the result
// in an empty key. If there is not a reviver function, we simply return the
return typeof reviver === 'function' ? (function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
if (Object.hasOwnProperty.call(value, k)) {
return reviver.call(holder, key, value);
}({'': result}, '')) : result;