Created
December 5, 2009 16:58
-
-
Save anonymous/249760 to your computer and use it in GitHub Desktop.
This file contains 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
// removing prototype.js #toJSON implementations as they breaks JSON.stringify | |
delete String.prototype.toJSON; | |
delete Array.prototype.toJSON; | |
delete Number.prototype.toJSON; | |
Date.prototype.toISOString = function() { | |
return this.getUTCFullYear() + '-' + | |
(this.getUTCMonth() + 1).toPaddedString(2) + '-' + | |
this.getUTCDate().toPaddedString(2) + 'T' + | |
this.getUTCHours().toPaddedString(2) + ':' + | |
this.getUTCMinutes().toPaddedString(2) + ':' + | |
this.getUTCSeconds().toPaddedString(2) + '.' + | |
this.getUTCMilliseconds().toPaddedString(3) + 'Z'; | |
}; | |
Date.prototype.toJSON = function(key) { | |
return (Object.isNumber(this) && !isFinite(this) ? null : this.toISOString()); | |
}; | |
Object.toJSON = function(value /*, replacer, space */) { | |
var replacer = arguments[1], | |
space = arguments[2] || '', | |
hash = {}, | |
stack = [], | |
gap = '', | |
toString = Object.prototype.toString, | |
propertyList, | |
str, v, i, length; | |
if (toString.call(replacer) === "[object Array]") { | |
propertyList = []; | |
for (i = 0, length = replacer.length; i < length; i++) { | |
v = replacer[i]; | |
if (toString.call(v) === "[object Number]" || toString.call(v) === "[object String]") { | |
v = String(v); | |
if (!hash.hasOwnProperty(' ' + v)) { | |
hash[' ' + v] = true; | |
propertyList.push(v); | |
} | |
} | |
} | |
} | |
switch (toString.call(space)) { | |
case "[object Number]" : gap = ' '.substr(0, space); break; | |
case "[object String]" : gap = space.substr(0, 10); break; | |
} | |
function quote(string) { | |
var replaceChars = { | |
'\b': '\\b', | |
'\t': '\\t', | |
'\n': '\\n', | |
'\f': '\\f', | |
'\r': '\\r', | |
'\\': '\\\\', | |
'"' : '\\"' | |
}; | |
string = String(string).replace(/[\x00-\x1f\\"]/g, function(character) { | |
if (replaceChars.hasOwnProperty(character)) { | |
return replaceChars[character]; | |
} | |
character = character.charCodeAt(0).toString(16); | |
return '\\u' + '0000'.substr(character.length) + character; | |
}); | |
return '"' + string + '"'; | |
} | |
function serializeObject(value, indent) { | |
var partial = [], | |
braces = ['{', '}'], i, strP, length, k; | |
for (i = stack.length - 1; i >= 0; i--) { | |
if (stack[i] === value) { | |
throw new TypeError(); | |
} | |
} | |
stack.push(value); | |
if (toString.call(value) === "[object Array]") { | |
braces = ['[', ']']; | |
// can't use each for sparse array | |
for(i = 0, length = value.length; i < length; i++) { | |
strP = str(i, value, indent + gap); | |
partial.push(typeof(strP) === "undefined" ? "null" : strP); | |
} | |
} else { | |
k = propertyList || Object.keys(value); | |
for(i = 0, length = k.length; i < length; i++) { | |
strP = str(k[i], value, indent + gap); | |
if (typeof(strP) !== "undefined") { | |
partial.push(quote(k[i]) + (gap ? ': ' : ':') + strP); | |
} | |
} | |
} | |
stack.pop(); | |
return braces[0] + (partial.length && gap ? '\n' + indent + gap + partial.join(',\n' + indent + gap) + '\n' + indent : partial.join(',')) + braces[1]; | |
} | |
str = function (key, holder, indent) { | |
var value = holder[key]; | |
if (value !== null && (typeof(value) === 'object' || typeof(value) === 'function') && typeof(value.toJSON) === "function") { | |
value = value.toJSON(key); | |
} | |
//if (Object.isElement(value)) { return; } //? | |
if (typeof(replacer) === "function") { | |
value = replacer.call(holder, key, value); | |
} | |
if (value === null) { | |
return 'null'; | |
} | |
switch (toString.call(value)) { | |
case "[object Boolean]" : return String(value); | |
case "[object String]" : return quote(value); | |
case "[object Number]" : return (isFinite(value) ? String(value) : 'null'); | |
} | |
if (typeof value === 'object') { | |
return serializeObject(value, indent); | |
} | |
}; | |
return str('', {'': value}, ''); | |
}; | |
(window.JSON = window.JSON || {}).stringify = Object.toJSON;// FF uses a little diff. implementation ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment