Skip to content

Commit

Permalink
refactor: parseJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
avwo committed Jun 13, 2018
1 parent 8970605 commit d664996
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": [
"react"
],
"ignore": [
"./biz/webui/htdocs/src/js/components/json/eval.js"
]
}
38 changes: 19 additions & 19 deletions biz/webui/htdocs/js/index.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions biz/webui/htdocs/src/js/components/json/eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var JSON_RE = /^\s*(?:\{[\w\W]*\}|\[[\w\W]*\])\s*$/;

function evalJson(str) {
if (!JSON_RE.test(str)) {
return;
}
var ctx = { console: undefined };
for (var i in window) {
ctx[i] = undefined;
}
with(ctx) {
try {
return eval('(' + str + ')');
} catch(e) {}
}
}

module.exports = evalJson;
4 changes: 3 additions & 1 deletion biz/webui/htdocs/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ function checkJson(item) {
try {
JSON.parse(item.value);
} catch(e) {
message.warn('Warning: the value of ' + item.name + ' can\`t be parsed into json. ' + e.message);
if (!util.evalJson(item.value)) {
message.warn('Warning: the value of ' + item.name + ' can\`t be parsed into json. ' + e.message);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion biz/webui/htdocs/src/js/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ function parseJson(str) {
}
message.error('Error: not a json object.');
} catch (e) {
if (json = util.evalJson(str)) {
return json;
}
message.error('Error: ' + e.message);
}
}
Expand Down Expand Up @@ -297,7 +300,7 @@ var List = React.createClass({
var item = this.currentFocusItem;
if (item) {
if (parseJson(item.value)) {
message.success('Good');
message.success('Good JSON Object.');
}
}
break;
Expand Down
15 changes: 4 additions & 11 deletions biz/webui/htdocs/src/js/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var $ = require('jquery');
var json2 = require('./components/json');
var evalJson = require('./components/json/eval');

var BIG_NUM_RE = /[:\[][\s\n\r]*-?[\d.]{16,}[\s\n\r]*[,\}\]]/;
var dragCallbacks = {};
Expand Down Expand Up @@ -343,14 +344,6 @@ exports.getPath = function(url) {
return index == -1 ? '/' : url.substring(index);
};

var HEX_CHAR_RE = /\\x([\da-f]{2})/ig;
var unescapeHexChar = function(all, $1) {
try {
return String.fromCharCode('0x' + $1);
} catch(e) {}
return all;
};

var parseJ = function (str, resolve) {
var result;
if (resolve && BIG_NUM_RE.test(str)) {
Expand All @@ -362,6 +355,8 @@ var parseJ = function (str, resolve) {
return typeof result === 'object' ? result : null;
};

exports.evalJson = evalJson;

function parseJSON(str, resolve) {
if (!str || !(str = str.trim())) {
return;
Expand All @@ -375,9 +370,7 @@ function parseJSON(str, resolve) {
try {
return parseJ(str, resolve);
} catch(e) {
try {
return parseJ(str.replace(HEX_CHAR_RE, unescapeHexChar), resolve);
} catch(e) {}
return evalJson(str);
}
}

Expand Down

0 comments on commit d664996

Please sign in to comment.