On Wed, Aug 18, 2021 at 3:00 PM Александр Илюшкин <[email protected]> wrote: > > Problem: > > I'm using spring mvc. > I have a requirement to output object values. All null values should be > included, but nested object should be replaced by nulls if it's empty > (consists from only null fields). > > For instance, this is the wrong answer: > > { "name" : null, "age" : null, "objectGroup1" : { "id" : null, "name" : null > }, "objectGroup2" : null } > > The correct answer, that I must respond with: > > { "name" : null, "age" : null, "objectGroup1" : null, "objectGroup2" : null } > > objectGroup1 was set to null but it is not filtered out! > > I tried to create a Value Filter but I realized that I can't mark certain > objects as null if it is empty, there is just no way to keep the key not > filtered out, separated to its value. > I also tried to write recursive ResponseBodyAdvice in spring, and it worked, > it recursively set null for empty objects, but it looks a bit ugly. > > I want somehow to be able to override object serialization of any kind of > class (for any DTO class) to be able to set nested objects as null if its > "empty" inside without having to write some recursive code on top of the > whole jackson serialization process. > > How can I do this in a most efficient way?
I think `@JsonInclude` is the way to go. Although it'd be great if `JsonInclude.Include.NON_EMPTY` worked for POJOs, it does not currently (I think). But custom filter would work: https://www.logicbig.com/tutorials/misc/jackson/json-include-customized.html You'd need to define criteria (and simple filter class) but that should be doable. -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10gKHdPwM0NPpy4FyDo_pYKCU_7zUd%2BSAEfQOzzxQ%2Bex4A%40mail.gmail.com.
