What is the correct way to define a custom bean is empty for serialization by Jackson?
I want to filter unwanted fields by Json->POJO->Json. I suppose the custom bean object does not appear when serialize to JSON if all variables inside the custom bean object are null. If any variables inside is not null, the custom bean object should appear.
Currently, I have an extra method to assign null value to custom bean if all variables belongs to it are null. What I am looking for is if this could be done by Jackson.
public class JsonFileContext {
//...Some variables
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonView(Views.In.class)
private ContentRecognize contentRecognize;
//...getter/setter method
}
ContentRecognize class
public class ContentRecognize {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String type;
@JsonInclude(JsonInclude.Include.NON_NULL)
private RecognizePattern targetId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private RecognizePattern msgId;
//...getter/setter method
}
Expected Output
{
"var1":"var1Content",
"var2":"var2Content"
}
AND
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{
"type":"typeContent"
}
}
Current Output
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{}
}
AND
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{
"type":"typeContent"
}
}