I am using Jackson JsonParser to parse the json tokens one by one. The idea is to create a list of parameters that are passed to the HTTPServletRequest object as a JSON Post Body. The Body may contain simple types like integer, double, true, false values in addition to array of simple and object values plus objects themselves. The aim to create list of parameters is that I pass the method name and the class name in the URL itself and so I know which method to call. The sequence of parameters in the request body exactly matches the expected arguments by that method. For doing this, I am able to add simple objects and simple types in the parameter list. However, the parameters might contain an array of remote objects as well.
In that case it looks like this:
{
"remoteClass": "com.example.Component",
"child": [
{
"remoteClass": "com.example.Component",
"field": "ID",
"fieldType": null,
"operator": "=",
"value": 1710
},
{
"remoteClass": "com.example.Component",
"field": "ID",
"fieldType": null,
"operator": "=",
"value": 1711
},
{
"remoteClass": "com.example.Component",
"field": "Time",
"fieldType": null,
"operator": ">=",
"value": "2013-04-04T18:30:00.000Z"
},
{
"remoteClass": "com.example.Component",
"field": "Time",
"fieldType": null,
"operator": "<=",
"value": "2013-04-25T18:29:59.999Z"
}
]
}
My Component class representation is like:
public class Component implements Serializable
{
private static final long serialVersionUID = 1L;
public Component[] child;
// Field name
public String field;
// Field type
public String fieldType;
// Operator
public String operator;
// Value
public Object value;
}
Since, I am using JsonParser and parsing token one by one. Whenever, I receive any token which starts with JsonToken.START_OBJECT, I want to directly convert the object to the class specified by remoteClass field, instead of parsing further for each token and creating map or json manually. Since, the class is known before hand, I can use jackson to automatically give me the object. However, I want to know if once JsonParser gives me the JsonToken.START_OBJECT token, how can I read the whole JSON till JsonToken.END_OBJECT and convert it to the relevant type specified, so that when the Jsonparser fetches the next token, it fetches after the object encountered has been completed converted and added in my list of parameters.
Any help is highly appreciated.