mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
refactor(polyfill) stringifyJSON
This commit is contained in:
parent
ffd996bf59
commit
41284e740a
1 changed files with 35 additions and 18 deletions
|
|
@ -205,33 +205,50 @@ var Util, DOM, jQuery;
|
|||
Util.parseJSON = $.parseJSON;
|
||||
|
||||
/* https://gist.github.com/754454 */
|
||||
Util.stringifyJSON = function(pObj) {
|
||||
var lRet;
|
||||
Util.stringifyJSON = function(obj) {
|
||||
var n, v, has,
|
||||
ret = '',
|
||||
value = '',
|
||||
json = [],
|
||||
isStr = Util.isString(obj),
|
||||
isObj = Util.isObject(obj),
|
||||
isArray = Util.isArray(obj);
|
||||
|
||||
if (!Util.isObject(pObj) || pObj === null) {
|
||||
if (!isObj || obj === null) {
|
||||
// simple data type
|
||||
if (Util.isString(pObj)) pObj = '"' + pObj + '"';
|
||||
lRet = String(pObj);
|
||||
if (isStr)
|
||||
obj = '"' + obj + '"';
|
||||
|
||||
ret += obj;
|
||||
} else {
|
||||
// recurse array or object
|
||||
var n, v, json = [], isArray = Util.isArray(pObj);
|
||||
|
||||
for (n in pObj) {
|
||||
v = pObj[n];
|
||||
for (n in obj) {
|
||||
v = obj[n];
|
||||
has = obj.hasOwnProperty(n);
|
||||
|
||||
if (pObj.hasOwnProperty(n)) {
|
||||
if (Util.isString(v))
|
||||
v = '"' + v + '"';
|
||||
else if (v && Util.isObject(v))
|
||||
v = DOM.stringifyJSON(v);
|
||||
|
||||
json.push((isArray ? "" : '"' + n + '":') + String(v));
|
||||
if (has) {
|
||||
isStr = Util.isString(v);
|
||||
isObj = Util.isObject(v);
|
||||
|
||||
if (isStr)
|
||||
v = '"' + v + '"';
|
||||
else if (v && isObj)
|
||||
v = Util.stringifyJSON(v);
|
||||
|
||||
if (!isArray)
|
||||
value = '"' + n + '":';
|
||||
|
||||
json.push(value + v);
|
||||
}
|
||||
}
|
||||
lRet = (isArray ? "[" : "{") + String(json) + (isArray ? "]" : "}");
|
||||
|
||||
if (isArray)
|
||||
ret = '[' + json + ']';
|
||||
else
|
||||
ret = '{' + json + '}';
|
||||
}
|
||||
|
||||
return lRet;
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue