Skip to content

Instantly share code, notes, and snippets.

@wondger
Created May 2, 2012 14:20
Show Gist options
  • Save wondger/2576852 to your computer and use it in GitHub Desktop.
Save wondger/2576852 to your computer and use it in GitHub Desktop.
JSON
/*
* @name:JSON.js
* @description:
* @author:[email protected]
* @date:2012-05-02
* @param:
* @todo:
* @changelog:
*/
window.JSON = window.JSON || {
isObject:function(obj){
return Object.prototype.toString.call(obj) == '[object Object]';
},
isString:function(obj){
return Object.prototype.toString.call(obj) == '[object String]';
},
isNull:function(obj){
return Object.prototype.toString.call(obj) == '[object Null]';
},
isUndefined:function(obj){
return void 0 === obj;
},
stringify:function(obj){
if(!this.isObject(obj)) return;
var ret = '';
ret = this.strfy(obj);
return '{'+ret+'}';
},
strfy:function(obj){
if(!this.isObject(obj)) return this.isString(obj) ? '"'+obj+'"' : obj.toString();
var ret = [];
for(var k in obj){
if(this.isUndefined(obj[k])) continue;
else if(this.isNull(obj[k])) ret.push('"'+k+'":null');
else if(this.isObject(obj[k])) ret.push('"'+k+'":{'+this.strfy(obj[k])+'}');
else ret.push('"'+k+'":'+this.strfy(obj[k]));
}
return ret.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment