Skip to content

Instantly share code, notes, and snippets.

@Demwunz
Created June 26, 2012 14:09
Show Gist options
  • Select an option

  • Save Demwunz/2995992 to your computer and use it in GitHub Desktop.

Select an option

Save Demwunz/2995992 to your computer and use it in GitHub Desktop.
json stringify
var JSON = {};
JSON.stringify = JSON.stringify ||
function (a) {
var t = typeof (a);
if (t != "object" || a === null) {
if (t == "string") a = '"' + a + '"';
return String(a);
} else {
var n, v, json = [],
arr = (a && a.constructor == Array);
for (n in a) {
if (n) {
v = a[n];
t = typeof (v);
if (t == "string") v = '"' + v + '"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment