문제

I am in need of an interface to build rules in a web application. There is a need to maintain state and I would like to know what would be the most appropriate method to store a nested logical expression (Example (a>b && a>c) || (a==0))

Is JSON adequate for representing such expressions or are there any alternate ways to achieve the same.

There are no restrictions in place for the user while building an expression.

도움이 되었습니까?

해결책

There are two ways to represent an expresssion in JSON:

  1. As a plain string. This usually works very well, is easy to debug and understand: {"expr":"(a>b && a>c) || (a==0)"}

  2. As a parse tree. Generally, parse trees are hard to understand at best, and often impossible to debug (as usual in the sense "it would be too much effort").

If you go the parse tree approach, then you need to define JSON fragments to represent each piece of the tree and then recursively build it.

And if you have no idea what I'm talking about: I strongly suggest solution #1.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top