Json Expression Evaluator
- Write a full expression parser and evaluator, that takes in an Expression and a Json
- Expression will contain:
- Variable (which will start with $), could also be nested (dot separated)
- Constant:
- String within quotes
- Boolean - true or false
- Decimal
- Number
- Logical Operator:
- AND
- OR
- NOT
- ( ) (Brackets)
- Comparison Operators 1) == 2) EXISTS
- Evaluation will be done on a json string input. Use variable values from this json, to evaluate it against the json.
- Returns true if expression is true on evaluation against a json
-
Expression: "$color == 'red'"
Json: {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
Output: true
-
Exp: "$mattress.name == 'king' AND $cost == 100.0"
Json: {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
Output: true
-
Exp: "NOT EXISTS $color"
Json: {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
output: false
-
Exp: "( $cost == 100.0 AND ( $mattress.big == false ) ) OR $size == 100"
Json: {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
Output: false
-
InputData.java: This module reads the input & returns the data
-
Init.java: This module is the entry point of the application. Application starts its execution from here. It reads the input data and give it to Parser to process
-
Parser.java: This module will take the input & process it.
-
JSONParser.java: This module will take the string representation of json & converts it into json object.
-
JSONObject.java: This module will take the json object created by JSONParser.java & json specific operations have been defined here.
-
ExpressionParser.java: This module will take the string representation of expression & split it into tokens
-
Condition.java: This module takes a condition in string format & converts it into a condition object, which will be used in ExpressionEvaluator.java
-
ExpressionEvaluator.java: This module will take care of the evaluation of the expression based on the json object.
- Stack.java: This sub-module will help in evaluation of expression condition. Example: to get the topmost token etc
- JSONParserHelper.java: This sub-module contains helper functions
-
I have assumed the given input is valid. (Both expression & json).
-
I have assumed the input format is as same as shown below. Valid expressions formats 1. "$color == 'red'" 2. "$mattress.name == 'king' AND $cost == 100.0" 3. "NOT EXISTS $color" 4. "( $cost == 100.0 AND ( $mattress.big == false ) ) OR $size == 100" etc
Valid JSON Strings formats 1. {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
-
If the expression contains any keys whose value is Array, then I am considering it as null. Example:
expression: "$legs.length == 4" Json: {"color":"red","size":10,"cost":100.0,"mattress":{"name":"king"},"big":true,"legs":[{"length":4}]}
Here in the json 'legs' is a key , whose 'value' is array. When one says 'legs.length', we don't know which element in the array to check. So these kind of expressions will be yield to "false" result.
- Circuit break pattern is not implemented.
- Restrictions in the inputs.
- No test cases
- Install java & set up the env variables
- Import the project into IntelliJ or Eclipse IDE and start executing from Init.java
Name: Sachin Kalsi
Ph: +91 9945746470
email: sachinkalsi15@gmail.com
website: https://sachinkalsi.github.io/
Github: https://github.com/sachinkalsi
NOTE:
- PLEASE LET ME KNOW AT ANY TIME IF YOU FIND ANY DIFFICULTY/TROUBLE IN EXECUTING THE APPLICATION.