Created
April 3, 2011 03:09
-
-
Save raliste/900146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!this.JSON) this.JSON = function () { | |
function f(n) { | |
return n < 10 ? '0' + n : n; | |
} | |
Date.prototype.toJSON = function () { | |
return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; | |
}; | |
var m = { | |
'\b': '\\b', | |
'\t': '\\t', | |
'\n': '\\n', | |
'\f': '\\f', | |
'\r': '\\r', | |
'"': '\\"', | |
'\\': '\\\\' | |
}; | |
function stringify(value, whitelist) { | |
var a, i, k, l, v; | |
switch (typeof value) { | |
case 'string': | |
return (new RegExp('[\x00-\x1f\\\\"]')).test(value) ? '"' + value.replace(/[\x00-\x1f\\"]/g, function (a) { | |
var c = m[a]; | |
if (c) return c; | |
c = a.charCodeAt(); | |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); | |
}) + '"' : '"' + value + '"'; | |
case 'number': | |
return isFinite(value) ? String(value) : 'null'; | |
case 'boolean': | |
return String(value); | |
case 'null': | |
return 'null'; | |
case 'object': | |
if (DOM.isNode(value)) return null; | |
if (!value) return 'null'; | |
if (typeof value.toJSON === 'function') return stringify(value.toJSON()); | |
a = []; | |
if (typeof value.length === 'number' && !(propertyIsEnumerable(value, 'length'))) { | |
l = value.length; | |
for (i = 0; i < l; i += 1) a.push(stringify(value[i], whitelist) || 'null'); | |
return '[' + a.join(',') + ']'; | |
} | |
if (whitelist) { | |
l = whitelist.length; | |
for (i = 0; i < l; i += 1) { | |
k = whitelist[i]; | |
if (typeof k === 'string') { | |
v = stringify(value[k], whitelist); | |
if (v) a.push(stringify(k) + ':' + v); | |
} | |
} | |
} else for (k in value) if (typeof k === 'string') { | |
v = stringify(value[k], whitelist); | |
if (v) a.push(stringify(k) + ':' + v); | |
} | |
return '{' + a.join(',') + '}'; | |
} | |
} | |
return { | |
stringify: stringify, | |
parse: function (text, filter) { | |
var j; | |
function walk(k, v) { | |
var i, n; | |
if (v && typeof v === 'object') for (i in v) if (Object.prototype.hasOwnProperty.apply(v, [i])) { | |
n = walk(i, v[i]); | |
if (n !== undefined) v[i] = n; | |
} | |
return filter(k, v); | |
} | |
if (text && /^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { | |
j = eval('(' + text + ')'); | |
return typeof filter === 'function' ? walk('', j) : j; | |
} | |
throw new SyntaxError('decodeJSON'); | |
} | |
}; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment