Skip to content

Instantly share code, notes, and snippets.

@mexitek
Created May 18, 2011 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mexitek/979295 to your computer and use it in GitHub Desktop.
Save mexitek/979295 to your computer and use it in GitHub Desktop.
Quick and Dirty JSON object for any small app
// JSON detection
var JSON = JSON || {};
// implement JSON.stringify serialization
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 ? "]" : "}");
}
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
if (str === "") str = '""';
eval("var p=" + str + ";");
return p;
};
@mexitek
Copy link
Author

mexitek commented May 18, 2011

I don't know who to give credit for this little JSON alternative, but I found it used on a coding sample at: http://ipinfodb.com/ip_location_api_json.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment