3

How i can hide empty {} objects after serialize? I have object with null values, after objectMapper.writeValueAsString i get JSON like :

{
"stringValue": "1",
"objectValue": {}
}

How can i exclude empty (field2) fileds from json? I can't set null to every object becouse i serialize many classes

P.S.i already setup

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Raheela Aslam
  • 452
  • 2
  • 13
Igor Fedorov
  • 321
  • 3
  • 17
  • Can you please try @JsonInclude(JsonInclude.Include.NON_NULL) annotation on fields which you want to exclude. – Raheela Aslam Nov 22 '18 at 11:08
  • This may be helpful https://stackoverflow.com/questions/40366524/how-to-tell-jackson-to-ignore-empty-object-during-deserialization the reversed issue but I think custom serializer can work as well – Akceptor Nov 22 '18 at 11:08
  • @RaheelaAslam thx, but this don't work with empty object. I try use @JsonInclude(JsonInclude.Include.NON_NULL) on filed, but this filed is not null – Igor Fedorov Nov 22 '18 at 11:33
  • @Akceptor thx, but this is using in library, I can`t write custom serializer for each domain class – Igor Fedorov Nov 22 '18 at 11:35
  • @IgorFedorov then did you tried NON_EMPTY annotation on field ? – Raheela Aslam Nov 22 '18 at 11:40
  • @RaheelaAslam yea, i tried this. Only custom serializer can exclude empty objects from result json, but it doesn't suit me. Thanks – Igor Fedorov Nov 22 '18 at 14:35

0 Answers0