最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing + Evaluating Expressions From JSON Object without using Eval()? - Stack Overflow

programmeradmin0浏览0评论

I'm building a node app in which users are (ideally) able to define styling - for geographic data - using a series of JSON objects:

{
    "style":
        {
            "test": "year",
            "condition": "<= 1954 AND >= 1936",
            "color": "red"
        }
}

In the case above, I like to evaluate that style as

if (year <= 1954 && year >= 1936){
    object.color = red;
}

Is there an easy way to parse + evaluate such expressions/build them from such an object? I'm especially interested in letting people string together plex expressions built using <=, >=, ||, && etc.

I'd like to avoid using eval(), if possible.

I'm building a node app in which users are (ideally) able to define styling - for geographic data - using a series of JSON objects:

{
    "style":
        {
            "test": "year",
            "condition": "<= 1954 AND >= 1936",
            "color": "red"
        }
}

In the case above, I like to evaluate that style as

if (year <= 1954 && year >= 1936){
    object.color = red;
}

Is there an easy way to parse + evaluate such expressions/build them from such an object? I'm especially interested in letting people string together plex expressions built using <=, >=, ||, && etc.

I'd like to avoid using eval(), if possible.

Share Improve this question edited Jul 4, 2013 at 3:58 sirKitty asked Jul 4, 2013 at 3:23 sirKittysirKitty 1432 silver badges11 bronze badges 2
  • 1 You might want to check out my project language called Bianca: github./aaditmshah/bianca. It parses a program and spits out a JSON AST, which is kind of the exact opposite of what your program does. Nevertheless it may give you some valuable insights. – Aadit M Shah Commented Jul 4, 2013 at 4:27
  • I definitely will check it out! Thanks! – sirKitty Commented Jul 4, 2013 at 4:42
Add a ment  | 

2 Answers 2

Reset to default 6

If you don't wish to use eval, you would have to write your own little parser and create a definition language like this:

"condition": ["and", ["<=", 1954], [">=", 1936]],

This is a partial implementation you could consider:

function do_and(args, value)
{
  for (var i = 0; i < args.length; ++i) {
    if (!evaluate(args[i], value)) {
      return false;
    }
  }
  return true;
}

function evaluate(condition, value)
{
  switch (condition[0]) {
    case "and":
      return do_and(condition.slice(1), value);

    case "<=":
      return value <= condition[1];

    case ">=":
      return value >= condition[1];
  }
}

This is how you would use it:

var style = {
    "test": "year",
    "condition": ["and", ["<=", 1954], [">=", 1936]],
    "color": "red"
}, context = {
  "year": 1940
};    

if (evaluate(style.condition, context[style.test])) {
  console.log(style.color); // "red"
}

Demo

Something like

var obj = JSON.parse(str);
switch (obj.style.operator){
    case '=':
    if (window[obj.style.condition] === obj.style){//assuming that the conditions are global
        object.color = obj.style;
    }
    break;
    ...
}
发布评论

评论列表(0)

  1. 暂无评论