-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Databind annotations
Tatu Saloranta edited this page Nov 19, 2013
·
6 revisions
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.
-
@JsonSerialize (method, field) can be used to denote:
- Explicit serializer to use, with
using(JsonSerializer)
property - Explicit type to use (instead of actual run time type), with
as(JsonSerializer)
property (must be compatible with run time type, i.e. super-type) - Which bean properties to include (based on kind of value they have) with
include()
property: by default all properties are included, but can omit null-valued properties or properties with "default" value (value assigned by default bean constructor)
- Explicit serializer to use, with
-
@JsonDeserialize
(method, field) can be used to denote:- Explicit deserializer to use:
-
using
for values (scalars or structured) -
contentUsing
for values of structured types (elements ofarrays
andjava.util.Collection
s, values ofjava.util.Map
s) -
keyUsing
for keys ofjava.util.Map
s
-
- Explicit types to use with
-
as
property (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-
converter
for property value itself, or -
contentConverter
for values of structured types (elements of array,java.util.Collection
s; values ofjava.util.Map
s)
-
- Explicit deserializer to use:
-
@JsonNaming
(Class) is used to indicatePropertyNamingStrategy
to use for annotated type- Note that
databind
module 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-calledBuilder
objects; builders can be specified using@JsonDeserialize.builder
property -
@JsonValueInstantiator
(Class) is used to specify customValueInstantiator
to use for annotated type- Use of
ValueInstantiator
is an advanced topic.
- Use of
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.
It is also possible to use JAXB annotations in addition to or instead of these core annotations.