I have a model that I'm trying to serialize. The model is contained in a Map. The model contains multiple instances of ClassA that are referenced in two different parts of the model.
The first part is where ClassX references ClassA. In this case I want to serialize ClassX and have a JsonValueProcessor convert ClassA to a single value (the ID).
The second part is where the model contains a List of ClassA. In this case I want to serialize the List, and have a JsonBeanProcessor convert each instance of ClassA into a JSONObject containing the name and ID attributes.
Is it possible to configure the serializer to handle both these scenarios within a single serialize operation?
Thanks,
-Jeff
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I believe JsonConfig.registerJsonValueProcessor* might expose what you are looking for. You may register a ValueBeanProcessor in 4 different ways:
1 beanClass + propertytype = matches all properties of type propertyType on instances of that class only*
2 beanClass + key = matches the property whose name is key on instances of that class only*
3 propertyType = matches all properties of type propertyType of all instances regardless of bean class
4 key = matches all properties with name key of all instances regardless of bean class
* can be changed if you register a JsonValueProcessorMatcher
I have a model that I'm trying to serialize. The model is contained in a Map. The model contains multiple instances of ClassA that are referenced in two different parts of the model.
The first part is where ClassX references ClassA. In this case I want to serialize ClassX and have a JsonValueProcessor convert ClassA to a single value (the ID).
The second part is where the model contains a List of ClassA. In this case I want to serialize the List, and have a JsonBeanProcessor convert each instance of ClassA into a JSONObject containing the name and ID attributes.
Is it possible to configure the serializer to handle both these scenarios within a single serialize operation?
Thanks,
-Jeff
Jeff,
I believe JsonConfig.registerJsonValueProcessor* might expose what you are looking for. You may register a ValueBeanProcessor in 4 different ways:
1 beanClass + propertytype = matches all properties of type propertyType on instances of that class only*
2 beanClass + key = matches the property whose name is key on instances of that class only*
3 propertyType = matches all properties of type propertyType of all instances regardless of bean class
4 key = matches all properties with name key of all instances regardless of bean class
* can be changed if you register a JsonValueProcessorMatcher
So assuming that
ClassX {
ClassA classA
List<ClassA> listOfClassA
}
you may register your processors like
jsonConfig.registerJsonValueProcessor( ClassX.class, ClassA.class, JsonValueProcessorForClassAProperty )
jsonConfig.registerJsonValueProcessor( ClassX.class, "listOfClassA", JsonValueProcessorForListOfA )
Cheers,
Andres