Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top