There is a well-known case when we expand a nested object and write its fields to the main object, and I need to do the inverse problem.
I have a POJO:
class A {
private String id = "id1";
@JsonWrap("properties")
private String property1 = "...";
@JsonWrap("properties")
private String property2 = "...";
}
The default serializer will produce as expected.
{
"id": "id1",
"property1": "...",
"property2": "..."
}
But my JSON must comply with certain specifications, and to do that I need to wrap property1and property2inside the propertieswrapper. The result should look like this:
{
"id": "id1",
"properties":
{
"property1": "...",
"property2": "..."
}
}
I do not want to change the structure of the POJO, so I see 3 possible ways:
- Create your own serializer. But, it seems to me, writing such a serializer will require more effort, and then serialize the objects with your hands.
- Create a Java proxy object that will display the correct JSON structure and serialize such a proxy.
- JSON . (, JSON).
- Serializer JSON , ?
custom serializer BeanSerializer, :
- .
- bean, , . (
jgen.writeEndObject();) - .
- .