Add or ignore attribute on JSON serialization depending on it's value

17 views
Skip to first unread message

Pablo Pazos

unread,
Aug 7, 2019, 12:38:20 PM8/7/19
to jackson-user
Hi, I have a complex model, where some attributes are collections and if the collection is empty I need to ignore adding if to the serialized JSON object, because right now I see items: [] everywhere (many of my classes have that attribute but it's empty most of the time.

Is there a way to do this in Jackson?

Thanks!

Tatu Saloranta

unread,
Aug 7, 2019, 1:27:35 PM8/7/19
to jackson-user
On Wed, Aug 7, 2019 at 9:38 AM Pablo Pazos <pabl...@gmail.com> wrote:
>
> Hi, I have a complex model, where some attributes are collections and if the collection is empty I need to ignore adding if to the serialized JSON object, because right now I see items: [] everywhere (many of my classes have that attribute but it's empty most of the time.
>
> Is there a way to do this in Jackson?

Via annotations you would add

@JsonInclude(include = Include.NON_EMPTY)

but there is also "config override" setting to define default; usage
maybe best seen in

src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeOverrideTest.java

but you can basically define default inclusion setting for all
properties, or properties of certain type (based on declared property
type)

Hope this helps,

-+ Tatu +-

>
> Thanks!
>
> --
> 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/e1f875ee-6d36-4a03-89a7-0e8bee7157d4%40googlegroups.com.

Pablo Pazos

unread,
Aug 7, 2019, 3:24:43 PM8/7/19
to jackson-user
Thanks! how can that annotation be included in a MixIn?


On Wednesday, August 7, 2019 at 2:27:35 PM UTC-3, Tatu Saloranta wrote:
On Wed, Aug 7, 2019 at 9:38 AM Pablo Pazos <pabl...@gmail.com> wrote:
>
> Hi, I have a complex model, where some attributes are collections and if the collection is empty I need to ignore adding if to the serialized JSON object, because right now I see items: [] everywhere (many of my classes have that attribute but it's empty most of the time.
>
> Is there a way to do this in Jackson?

Via annotations you would add

@JsonInclude(include = Include.NON_EMPTY)

but there is also "config override" setting to define default; usage
maybe best seen in

src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeOverrideTest.java

but you can basically define default inclusion setting for all
properties, or properties of certain type (based on declared property
type)

Hope this helps,

-+ Tatu +-

>
> Thanks!
>
> --
> 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 jackso...@googlegroups.com.

Tatu Saloranta

unread,
Aug 7, 2019, 5:31:20 PM8/7/19
to jackson-user
On Wed, Aug 7, 2019 at 12:24 PM Pablo Pazos <pabl...@gmail.com> wrote:
>
> Thanks! how can that annotation be included in a MixIn?

Same was as any annotation, marking either getter/setter of field.
Mix-ins work for members as well as class annotations.

But configOverride approach may be more convenient, if you want wide defaults.

-+ Tatu +-
> 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/052b9d63-64ed-4a51-afa8-479ed1166896%40googlegroups.com.

Pablo Pazos

unread,
Aug 7, 2019, 8:35:43 PM8/7/19
to jackson-user
Reply all
Reply to author
Forward
0 new messages