Created
July 25, 2013 10:04
-
-
Save ryanlewis/6078410 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
JSON.stringify = JSON.stringify || function (obj) { | |
var t = typeof (obj); | |
if (t != "object" || obj === null) { | |
// simple data type | |
if (t == "string") obj = '"'+obj+'"'; | |
return String(obj); | |
} | |
else { | |
// recurse array or object | |
var n, v, json = [], arr = (obj && obj.constructor == Array); | |
for (n in obj) { | |
v = obj[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
Saw this at http://www.sitepoint.com/javascript-json-serialization/