Welcome to Jackson!
Simple serialization/deserialization via ObjectMapper
What fields will be serialized/deserialized?
- public fields
- if getter is available (serializable and deserializable)
- if setter is available (only deserializable)
- apply
@JsonAutoDetect
(for example,@JsonAutoDetect(fieldVisibility = Visibility.ANY)
)
Note: marshalling is getters-based, if there is getCalculatedValue()
method (and there is no such field), Jackson will serialize calculatedValue
. Use @JsonAutoDetect(fieldVisibility=Visibility.ANY, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE, setterVisibility=Visibility.NONE)
to chose what you’d like to marshall.
@JsonIgnore
can be added to a field itself or to getter/setter.
Jackson requires either no-arg constructor or @JsonCreator. The latter works for custom deserialization.
Note: won’t work for non-static inner class http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html
Rename fields, set required flag
@JsonProperty
is for a field@JsonGetter
/@JsonSetter
are for getter/setter.@JsonAlias
- several possible names for one field
Enum (and not only enum) to single value
Jackson Polymorphic Type Handling Annotations
@JsonTypeInfo
– indicates details of what type information to include in serialization@JsonSubTypes
– indicates sub-types of the annotated type@JsonTypeName
– defines a logical type name to use for annotated class
Good example is available on Baeldung
Changes in original objects
Skip unknown properties:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);