Tatu, could you please, help me to dig deeper and try something else? четверг, 19 августа 2021 г. в 01:28:07 UTC+3, Александр Илюшкин:
> I tried custom filter, but it doesn't work as described. If i say, return > true if all your fields are null, it removes the whole field. But instead i > want to have field: null in response. I want to remove only certain set of > nulls, only if it is a set of nested fields, and only if object is empty. > You could try it by yourself, it is not possible to pass information > between different levels while filtering. > четверг, 19 августа 2021 г. в 01:13:16 UTC+3, Tatu Saloranta: > >> 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/2413ab56-7733-4898-b694-aa07c6dceb53n%40googlegroups.com.
