On Wed, Aug 18, 2021 at 3:28 PM Александр Илюшкин <[email protected]> wrote:
>
> 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.

Ah. Then you will need to implement this yourself, either with a
custom serializer or, perhaps by two-pass processing: first convert
POJO into JsonNode
(mapper.valueToTree()), then change that structure, serialize.
I can't think of another feature that would work for your case.

-+ Tatu +-

> четверг, 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/a015d91d-a5dd-4199-a531-96c314037cd1n%40googlegroups.com.

-- 
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/CAL4a10iST%3DK0viNBCtoDPUHo2wPT4scD%3DycDOYNE7-UqMwOtzg%40mail.gmail.com.

Reply via email to