On Mon, Jan 1, 2018 at 1:45 PM, Rich MacDonald <[email protected]> wrote:
> Every time I use jackson json I realize how much I still don't know. That is
> ok. Today's problem is deserializing a class with an optional json
> attribute. If I don't mark the field with @JsonIgnore, I'll get a
> "Unrecognized field" error. If I do mark it with @JsonIgnore, it won't be
> populated.
>
> I can make things work by making the field as @JsonIgnore then implementing
> a @JsonAnySetter to set it "manually", but this was interesting. Surely such
> a common problem has a better solution?

Couple of ways to go about this:

1. It is possible to have "split" properties, so that getter method
has `@JsonIgnore`, but setter `@JsonProperty`. If so, JSON would not
have property, but pojo would get it serialized.
2.  Since 2.6, there is also `@JsonProperty(access=Access.READ_ONLY)`
(and `WRITE_ONLY`, `READ_WRITE`) which allows declaring read-only /
write-only without having to annotate multiple accessors

Second option is probably more readable, leads to less duplication,
and specifically works even there is no getter or setter to annotate.

I hope this helps,

-+ Tatu +-


>
> Regards.
>
> --
> 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 post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to