Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active December 22, 2015 06:39
Show Gist options
  • Save jpillora/6432307 to your computer and use it in GitHub Desktop.
Save jpillora/6432307 to your computer and use it in GitHub Desktop.
mini stringify
var stringify = function(obj) {
var t;
if(obj === null)
return 'null';
if(obj instanceof Array) {
t = [];
for(var k in obj) t.push(stringify(obj[k]));
return "["+t.join(",")+"]";
}
switch(typeof obj) {
case 'object':
t = [];
for(var k in obj) t.push('"' + k + '":' + stringify(obj[k]));
return "{"+t.join(",")+"}";
case "string":
return '"'+obj+'"';
default:
return obj;
}
return r;
};
print(stringify({a:{e:null}, b:"test", c:42, d:false, f:[1,"test"]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment