Custom serialization of empty object parameters

50 views
Skip to first unread message

Александр Илюшкин

unread,
Aug 18, 2021, 6:00:18 PM8/18/21
to jackson-user

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?

Tatu Saloranta

unread,
Aug 18, 2021, 6:13:16 PM8/18/21
to jackson-user
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 +-

Александр Илюшкин

unread,
Aug 18, 2021, 6:28:07 PM8/18/21
to jackson-user
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:

Александр Илюшкин

unread,
Aug 19, 2021, 10:17:14 AM8/19/21
to jackson-user
Tatu, could you please, help me to dig deeper and try something else?

четверг, 19 августа 2021 г. в 01:28:07 UTC+3, Александр Илюшкин:

Tatu Saloranta

unread,
Aug 19, 2021, 12:48:11 PM8/19/21
to jackson-user
On Wed, Aug 18, 2021 at 3:28 PM Александр Илюшкин <ailju...@gmail.com> 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 Александр Илюшкин <ailju...@gmail.com> 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 jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/a015d91d-a5dd-4199-a531-96c314037cd1n%40googlegroups.com.

Александр Илюшкин

unread,
Aug 19, 2021, 2:17:24 PM8/19/21
to jackson-user
Thank you so much, Tatu.

четверг, 19 августа 2021 г. в 19:48:11 UTC+3, Tatu Saloranta:
Reply all
Reply to author
Forward
0 new messages