Jackson Data-Binding Annotations
In addition to core Jackson annotations, jackson-databind adds a small set of annotations that are tied to types defined in databind package.
These are used for more fine-grained definition of handlers.
Serialization (writing JSON)
-
@JsonSerialize (method, field) can be used to denote:
- Explicit serializer (of type
JsonSerializer) to use, with following properties:-
usingfor value of property itself -
keyUsingfor keys ofjava.util.Mapvalued properties -
contentUsingfor contents of structured values (elements of arrays,java.util.Collections, values ofjava.util.Maps) -
nullUsingwhen serializing Javanullvalued properties (NEW with 2.3)
-
- Explicit type to use (instead of actual run time type) with
-
asfor property value -
keyAsforjava.util.Mapkeys -
contentAsfor elements/values of structured types - NOTE: type used must be compatible with nominal type (a super type)
-
- Whether static or dynamic typing (default: static typing) is to be used:
typingproperty- Allowed values are
DYNAMIC(for dynamic runtime type),STATIC(to use declared, "nominal" type) orDEFAULT_TYPINGto use defaults forObjectMapper
- Allowed values are
- Converter object (of type
Converter) to use-
converterfor property value itself, or -
contentConverterfor values of structured types (elements of array,java.util.Collections; values ofjava.util.Maps)
-
- Explicit serializer (of type
Deserialization (reading JSON)
-
@JsonDeserialize (method, field) can be used to denote:
- Explicit deserializer to use:
-
usingfor values (scalars or structured) -
contentUsingfor values of structured types (elements ofarraysandjava.util.Collections, values ofjava.util.Maps) -
keyUsingfor keys ofjava.util.Maps
-
- Explicit types to use with
-
asproperty (must be compatible with declared type, i.e. subtype) - Similarly
keyAs()andcontentAs()for specifying key type forjava.util.Maps, content type for Collections, arrays.
-
- Builder object (of any type) to use:
builder - Converter object (of type
Converter) to use-
converterfor property value itself, or -
contentConverterfor values of structured types (elements of array,java.util.Collections; values ofjava.util.Maps)
-
- Explicit deserializer to use:
-
@JsonNaming (Class) is used to indicate
PropertyNamingStrategyto use for annotated type- Note that
databindmodule includes implementation of two alternate naming strategies:-
PropertyNamingStrategy.LowerCaseWithUnderScoresStrategy: supports "C-style" naming, likefirst_name(instead of Java "firstName") -
PropertyNamingStrategy.PascalCaseStrategy: supports names like "FirstName"
-
- Note that
-
@JsonPOJOBuilder (Class) is used to configure so-called
Builderobjects; builders can be specified using@JsonDeserialize.builderproperty -
@JsonValueInstantiator (Class) is used to specify custom
ValueInstantiatorto use for annotated type- Use of
ValueInstantiatoris an advanced topic.
- Use of
Polymorphic type handling
Following annotations work together with standard @JsonTypeInfo annotation defined by core annotations package.
- @JsonTypeResolver (class) can be used to plug in a custom type information handler, to replace default one (for specific class)
- @JsonTypeIdResolver (class) can be used to replace standard type id converter (type to/from JSON String) with a custom version; for example, to create more convenient handler for using logical type names.
See also
It is also possible to use JAXB annotations in addition to or instead of these core annotations.