I am using Jackson ObjectMapper to serialize a POJO. I have nested fields in the POJO. Eg: serializing class MyClass
public class MyClass {
private A a;
private int i;
//getters and setters
}
public class A {
private String s;
//getters and setters
}
I want that if String s
is null
, the entire property A
does not get serialized. That is, if String s
is null
, I want the output to be:
{"myClass":{"i":10}}
But I am getting {"myClass":{"A":{},"i":10}}
as the output instead.
I have set NON_EMPTY
for serialization inclusion (mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
), but it doesn't solve the problem