Dealing with null lists, v2.12

50 views
Skip to first unread message

Marcelo Carabolante

unread,
Mar 5, 2021, 1:22:24 PM3/5/21
to jackson-user
Hello folks,

Is there any way to configure Jackson to treat null/missing list a empty collection?

context: we are using immutable objects, so its constructor based deserialization.

I tried a few stuff, but I always seems to hit a wall.

Thanks!



This email and any files transmitted with it contain confidential information and/or privileged or personal advice. This email is intended for the addressee(s) stated above only. If you are not the addressee of the email please do not copy or forward it or otherwise use it or any part of it in any form whatsoever. If you have received this email in error please notify the sender and remove the e-mail from your system. Thank you.

This is an email from the company Just Eat Takeaway.com N.V., a public limited liability company with corporate seat in Amsterdam, the Netherlands, and address at Oosterdoksstraat 80, 1011 DK Amsterdam, registered with the Dutch Chamber of Commerce with number 08142836 and where the context requires, includes its subsidiaries and associated undertakings.

Tatu Saloranta

unread,
Mar 5, 2021, 4:05:14 PM3/5/21
to jackson-user
On Fri, Mar 5, 2021 at 10:22 AM Marcelo Carabolante
<marcelo.c...@justeattakeaway.com> wrote:
>
> Hello folks,
>
> Is there any way to configure Jackson to treat null/missing list a empty collection?
>
> context: we are using immutable objects, so its constructor based deserialization.
>
> I tried a few stuff, but I always seems to hit a wall.

If what you want is to allow handling missing/null Collection value(s)
by one of:

1. Replace with default "empty" value
2. Throw exception
3. Skip assignment (for setter/field), to leave at default (presumable
empty) value

then what you want is the special form of `@JsonSetter` annotation (or
matching "config override" configuration).
It was added in 2.9, and explained on:

https://cowtowncoder.medium.com/jackson-2-9-features-b2a19029e9ff

So you'd add:

@JsonSetter(nulls=Nulls.AS_EMPTY)

on the specific accessor (field, setter, constructor parameter).
To handle the case of missing values (as opposed to just explicitly
received `null`s), you will need to apply it to constructor parameter.

Hope this helps,

-+ Tatu +-



>
> Thanks!
>
>
> ________________________________
> This email and any files transmitted with it contain confidential information and/or privileged or personal advice. This email is intended for the addressee(s) stated above only. If you are not the addressee of the email please do not copy or forward it or otherwise use it or any part of it in any form whatsoever. If you have received this email in error please notify the sender and remove the e-mail from your system. Thank you.
>
> This is an email from the company Just Eat Takeaway.com N.V., a public limited liability company with corporate seat in Amsterdam, the Netherlands, and address at Oosterdoksstraat 80, 1011 DK Amsterdam, registered with the Dutch Chamber of Commerce with number 08142836 and where the context requires, includes its subsidiaries and associated undertakings.
>
> --
> 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/f94adfef-e4be-4210-9b2e-62c87a3dd1c5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages