I have a json which is complex/nested. My json file consists of two equivalent java objects. One is Complex_Expression and another is Simple_Expression.
Complex_Expression is in the following form:
{
"SomeOpearator":0,
"ASpecificKey":1, //the value 1 is fixed for complex expression.
"Expressions":[ ] //array of one or more expressions
}
Simple Expression is in the following form:
{
"Operand":"Some String Value",
"ASpecificKey":0, //the value 0 is fixed for simple expressions.
"SomeComparisionOpearator":1, // enums which associates int to different comparison operators.
"Value":["String1"] //this is an array of strings.
}
The Expressions in turn can have Complex_Expression and/or Simple_Expression. The json file always starts from Complex_Expression. I have to deserialize this JSON file. My final goal is to make an expression usingComplex_Expression and Simple_Expression objects and with some logics in these classes. I don't mind using jackson or gson or maybe other dependencies.
Till now I have created a base class called Expression. Complex_Expression and Simple_Expression both inherits this class. Then I started writing Custom Json Deserializer. But in the custom deserializer I am stuck and I don't know how should I proceed. Please help me on this. My Simple_Expression class looks like this and somewhat similar is the Complex_Expression class.
public class Simple_Expression extends Expression
{
@JsonProperty("Operand") //use jackson deserializer for this class.
public String Operand;
@JsonProperty("SomeComparisionOpearator")
public SomeComparisionOpearator someCompareOperator;
@JsonProperty("Value")
public Object value;
public Simple_Expression()
{
super(ASpecificKey.Simple); //Simple corresponds to 0
}
}
Update
Some more description about my input and output. With input given a JSON string like this:
{
"SomeOpearator": 0,
"ASpecificKey": 1,
"Expressions": [
{
"SomeOpearator": 1,
"ASpecificKey": 1,
"Expressions": [
{
"Operand": "People",
"ASpecificKey": 0,
"SomeComparisionOpearator": 14,
"Value": [
"Rich"
]
}
]
},
{
"SomeOpearator": 1,
"ASpecificKey": 1,
"Expressions": [
{
"Operand": "Grade",
"ASpecificKey": 0,
"SomeComparisionOpearator": 2,
"Value": [
"Grade A"
]
}
]
}
]
}
I should be able to do something like this, assuming jackson deserializer:
ObjectMapper mapper = new ObjectMapper();
Expression myExpressionObject = mapper.convertValue(jsonString, Expression.class);
It should give me the deserialized object into the myExpressionObject which will consists of a list of expressions (Arraylist or Array, no problem).