Skip to content

JacksonAnnotations

Tatu Saloranta edited this page Nov 19, 2013 · 10 revisions

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 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)

Deserialization (reading JSON)

  • @JsonDeserialize (method, field) can be used to denote:
    • Explicit deserializer to use, with using(JsonDeserializer) property
    • Explicit types to use with as(JsonSerializer) property (must be compatible with declared type, i.e. subtype)
    • Similarly keyAs() and contentAs() for specifying key type for Maps, content type for Collections, arrays.

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.