How can I tell jackson to ignore a property for which I don't have control over the source code?
With Jackson, you can bypass the property serialization issue using a mixin with an @JsonIgnore
annotation. Here's how:
By using this configuration, Jackson will conveniently ignore propertyToIgnore
during the serialization process of the Target
class.
Customizing serialization without changing source code
If you are in a situation where you need to exclude certain elements from serialization but don't have control over the source code, don't worry! Jackson has got you covered. Let's look at a few possibilities:
-
Ignore unknown properties: You can instruct the
ObjectMapper
to disregard unknown properties like this: -
Additionally, you can use the
@JsonIgnoreProperties
annotation to ignore unpredicted properties at class level:
Employing mixins and filters to tackle serialization
Mixin to rule them all
Jackson lets you define a rule that applies to multiple classes. Enter setMixInResolver
with MixInResolver
implementation. This powerful approach allows you to define the logic for finding mix-in classes for target classes on the fly.
Dynamic filtering using JsonFilter
Using a JsonFilter
can bring even more flexibility to your filtering:
-
Define your filter:
-
Create your filter rule:
-
Apply the filters using an
ObjectWriter
:
Guide to custom deserialization
Feel free to use @JsonCreator
and @JsonProperty
annotations to control deserialization:
Was this article helpful?