I'm trying to serialize a Json tree into objects using Jackason the Json structure is
{
"TypeA": {
"field1": "x",
"field2": 42,
"child": [
{
"TypeB": {
"field11": "y",
"content": [
{
"TypeC": {
"field66": true
}
}
]
}
},
{
"TypeB": {
"field11": "z",
"content": [
{
"TypeC": {
"field66": false
}
}
]
}
}
]
}
}
The first field name is a hint for the class, all of them are extending the same base class (TypeBaseClass). In the Json above there is reference for three classes. for example, the matching class for TypeA is:
public class TypeA extends TypeBaseClass {
private String field1;
private int field2;
private List<TypeBaseClass> child;
}
I currently solve it using BeanDeserializerModifier that catch all TypeBaseClass:
@Override
public JsonDeserializer<?> modifyDeserializer(
DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (TypeBaseClass.class.isAssignableFrom(beanDesc.getBeanClass())) {
return new TypeBaseClassParser<>(deserializer);
}
return deserializer;
}
and then play with the JsonParser to find the class and then extract the inner object.
- Is there a better way to do it?
I thought I would change the structure so the type will be inside of the object and not wrapping it:
{
"TypeBaseClass": "TypeA",
"field1": "x",
"field2": 42,
"child": [ ...
}
This way I wouldn't need BeanDeserializerModifier and no need for extracting the inner Json. just read the first field.
- Will this improve the performance?
- What is the best way to change the structure like this?
I have hundreds of classes extending TypeBaseClass and the Jsons are big.