Created
May 2, 2012 14:20
-
-
Save wondger/2576852 to your computer and use it in GitHub Desktop.
JSON
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
/* | |
* @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