Created
November 26, 2014 00:56
-
-
Save Cfeusier/79baf443793baaba24f7 to your computer and use it in GitHub Desktop.
Recursive Reimplementation of JSON.stringify
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
// recursive reimplementation of JSON.stringify | |
var stringifyJSON = function(obj) { | |
// null | |
if (obj === null) { | |
return "null"; | |
} | |
// unstringifiable - functions and undefined | |
if (obj === undefined || obj.constructor === Function) { return; } | |
// strings | |
if (obj.constructor === String) { | |
return '"' + obj + '"'; | |
} | |
// arrays | |
if (obj.constructor === Array) { | |
if (obj.length) { | |
var partialJSON = []; | |
for (var i = 0; i < obj.length; i++) { | |
partialJSON.push(stringifyJSON(obj[i])); // recursion | |
} | |
return '[' + partialJSON.join(",") + ']'; | |
} else { | |
return '[]'; | |
} | |
} | |
// objects | |
if (obj.constructor === Object) { | |
var keys = Object.keys(obj); | |
if (keys.length) { | |
var partialJSON = ''; | |
for (var i = 0; i < keys.length; i++) { | |
var key = keys[i]; | |
if (!key || obj[key] === undefined || typeof key === 'function' || typeof obj[key] === 'function') { | |
} else { | |
if (i === keys.length - 1) { | |
partialJSON += stringifyJSON(key) + ':' + stringifyJSON(obj[key]); // recursion | |
} else { | |
partialJSON += stringifyJSON(key) + ':' + stringifyJSON(obj[key]) + ','; // recursion | |
} | |
} | |
} | |
return '{' + partialJSON + '}'; | |
} else { | |
return '{}'; | |
} | |
} | |
// everything else (numbers, booleans, etc.) | |
return obj.toString(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This implementation cannot stringify nested object.