diff --git a/lib/client.js b/lib/client.js index 7df33f48..2e5ec7b7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -333,7 +333,7 @@ var Util, DOM, CloudFunc, join; function ajaxLoad(path, options, panel, callback) { var create = function(error, json) { var RESTful = DOM.RESTful, - obj = Util.parseJSON(json), + obj = Util.json.parse(json), isRefresh = options.refresh, history = options.history; @@ -473,7 +473,7 @@ var Util, DOM, CloudFunc, join; }); } - ret = Util.stringifyJSON(fileTable); + ret = Util.json.stringify(fileTable); return ret; } diff --git a/lib/client/buffer.js b/lib/client/buffer.js index 38c5e824..26386f17 100644 --- a/lib/client/buffer.js +++ b/lib/client/buffer.js @@ -113,7 +113,7 @@ var Util, DOM; if (error) { DOM.Dialog.alert(error); } else if (cp) { - data = Util.parseJSON(cp); + data = Util.json.parse(cp); data.to = path; if (data.from === path) @@ -122,7 +122,7 @@ var Util, DOM; DOM.copyFiles(data); } else if (ct) { - data = Util.parseJSON(ct); + data = Util.json.parse(ct); data.to = path; if (data.from === path) diff --git a/lib/client/dom.js b/lib/client/dom.js index 537c5bd6..aedae479 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -830,7 +830,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; ONE_MEGABYTE = 1024 * 1024 * 1024; if (Util.isObject(data)) - data = Util.stringifyJSON(data); + data = Util.json.stringify(data); length = data.length; if (hash && length < ONE_MEGABYTE) diff --git a/lib/client/edit.js b/lib/client/edit.js index 87e416a9..7897ba74 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -350,7 +350,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI DOM.load.ajax({ url : JSHINT_PATH, success : function(data) { - JSHintConfig = Util.parseJSON(data); + JSHintConfig = Util.json.parse(data); func(); } }); diff --git a/lib/client/load.js b/lib/client/load.js index b493f014..a77115e5 100644 --- a/lib/client/load.js +++ b/lib/client/load.js @@ -174,7 +174,7 @@ var Util, DOM; xhr.responseType = p.responseType; if (isObject || isArray) - data = Util.stringifyJSON(p.data); + data = Util.json.stringify(p.data); else data = p.data; @@ -209,7 +209,7 @@ var Util, DOM; if (p.dataType !== 'text') /* If it's json - parse it as json */ if (type && isContain) - data = Util.parseJSON(xhr.response) || xhr.response; + data = Util.json.parse(xhr.response) || xhr.response; Util.exec(p.success, data, xhr.statusText, xhr); } diff --git a/lib/client/polyfill.js b/lib/client/polyfill.js index db261019..c2ce2794 100644 --- a/lib/client/polyfill.js +++ b/lib/client/polyfill.js @@ -210,10 +210,10 @@ var Util, DOM, jQuery; } if (!window.JSON) { - Util.parseJSON = $.parseJSON; + Util.json.parse = $.parseJSON; /* https://gist.github.com/754454 */ - Util.stringifyJSON = function(obj) { + Util.json.stringify = function(obj) { var n, v, has, ret = '', value = '', @@ -241,7 +241,7 @@ var Util, DOM, jQuery; if (isStr) v = '"' + v + '"'; else if (v && isObj) - v = Util.stringifyJSON(v); + v = Util.json.stringify(v); if (!isArray) value = '"' + n + '":'; diff --git a/lib/client/storage.js b/lib/client/storage.js index 563a9b6b..08af026e 100644 --- a/lib/client/storage.js +++ b/lib/client/storage.js @@ -64,7 +64,7 @@ var Util, DOM; var str, ret = Allowed && name; if (Util.isObject(data)) - str = Util.stringifyJSON(data); + str = Util.json.stringify(data); if (Allowed && name) localStorage.setItem(name, str || data); diff --git a/lib/server/config.js b/lib/server/config.js index 62fa87b2..bc169c10 100644 --- a/lib/server/config.js +++ b/lib/server/config.js @@ -26,7 +26,7 @@ }; module.exports.save = function(callback) { - var data = Util.stringifyJSON(config); + var data = Util.json.stringify(config); Util.checkArgs(arguments, ['callback']); diff --git a/lib/server/ischanged.js b/lib/server/ischanged.js index e57a22fe..454a2f98 100644 --- a/lib/server/ischanged.js +++ b/lib/server/ischanged.js @@ -84,14 +84,14 @@ if (!error && readTime !== fileTime) { timeChanged = true; Times[name] = fileTime; - json = Util.stringifyJSON(Times); + json = Util.json.stringify(Times); fs.writeFile(NAME, json, function(error) { if (error) Util.log(error); }); } - + callback(error, timeChanged); }); }); diff --git a/lib/server/rest.js b/lib/server/rest.js index f9a7e2bd..c05280d0 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -181,7 +181,7 @@ if (isStr) str = data; else - str = Util.stringifyJSON(data); + str = Util.json.stringify(data); } callback(error, options, str); @@ -196,7 +196,7 @@ if (error) { callback(error); } else { - files = Util.parseJSON(body); + files = Util.json.parse(body); Fs.delete(query, path, files, function(error) { var names, msg; @@ -232,7 +232,7 @@ switch(cmd) { case '': - p.data = Util.stringifyJSON({ + p.data = Util.json.stringify({ info: 'Cloud Commander API v1' }); @@ -271,12 +271,12 @@ if (name[0] === '/') cmd = name.replace('/', ''); - files = Util.parseJSON(body); + files = Util.json.parse(body); switch(cmd) { case 'auth': github.auth(body, function(error, token) { - callback(error, Util.stringifyJSON({ + callback(error, Util.json.stringify({ data: token })); }); @@ -391,7 +391,7 @@ config(name, files[name]); }); - json = Util.stringifyJSON(config) + '\n'; + json = Util.json.stringify(config) + '\n'; config.save(function(error) { data = formatMsg('config', name); @@ -461,7 +461,7 @@ isObj = Util.isObject(dataParam); if (isObj) - data = Util.stringifyJSON(dataParam); + data = Util.json.stringify(dataParam); else data = dataParam; diff --git a/lib/util.js b/lib/util.js index 874d0f49..903f7d5b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -168,32 +168,35 @@ return ret; }; - /** - * @param pJSON - */ - this.parseJSON = function(str) { - var obj; - - Util.exec.try(function() { - obj = JSON.parse(str); - }); - - return obj; - }; + this.json = new JsonProto(); - /** - * @param pObj - */ - this.stringifyJSON = function(obj) { - var str; + function JsonProto() { + /** + * @param str + */ + this.parse = function(str) { + var obj; + + Util.exec.try(function() { + obj = JSON.parse(str); + }); + + return obj; + }, - Util.exec.tryLog(function() { - str = JSON.stringify(obj, null, 4); - }); - - return str; - }; - + /** + * @param obj + */ + this.stringify = function(obj) { + var str; + + Util.exec.tryLog(function() { + str = JSON.stringify(obj, null, 4); + }); + + return str; + }; + } /** * function check is strings are equal * @param {String} str1