org.codehaus.jackson.map.annotate
Annotation Type JsonSerialize


@Target(value={METHOD,FIELD,TYPE,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonSerialize

Annotation used for configuring serialization aspects, by attaching to "getter" methods or fields, or to value classes. When annotating value classes, configuration is used for instances of the value class but can be overridden by more specific annotations (ones that attach to methods or fields).

An example annotation would be:

  @JsonSerialize(using=MySerializer.class,
    as=MySubClass.class,
    include=JsonSerialize.Inclusion.NON_NULL,
    typing=JsonSerialize.Typing.STATIC
  )
(which would be redundant, since some properties block others: specifically, 'using' has precedence over 'as', which has precedence over 'typing' setting)

NOTE: since version 1.2, annotation has also been applicable to (constructor) parameters

Since:
1.1

Optional Element Summary
 Class<?> as
          Supertype (of declared type, which itself is supertype of runtime type) to use as type when locating serializer to use.
 Class<?> contentAs
          Concrete type to serialize content value (elements of a Collection/array, values of Maps) as, instead of type otherwise declared.
 Class<? extends JsonSerializer<?>> contentUsing
          Serializer class to use for serializing contents (elements of a Collection/array, values of Maps) of annotated property.
 JsonSerialize.Inclusion include
          Which properties of annotated Bean are to be included in serialization (has no effect on other types like enums, primitives or collections).
 Class<?> keyAs
          Concrete type to serialize keys of Map as, instead of type otherwise declared.
 Class<? extends JsonSerializer<?>> keyUsing
          Serializer class to use for serializing Map keys of annotated property.
 JsonSerialize.Typing typing
          Whether type detection used is dynamic or static: that is, whether actual runtime type is used (dynamic), or just the declared type (static).
 Class<? extends JsonSerializer<?>> using
          Serializer class to use for serializing associated value.
 

using

public abstract Class<? extends JsonSerializer<?>> using
Serializer class to use for serializing associated value. Depending on what is annotated, value is either an instance of annotated class (used globablly anywhere where class serializer is needed); or only used for serializing property access via a getter method.

Default:
org.codehaus.jackson.map.JsonSerializer.None.class

contentUsing

public abstract Class<? extends JsonSerializer<?>> contentUsing
Serializer class to use for serializing contents (elements of a Collection/array, values of Maps) of annotated property. Can only be used on properties (methods, fields, constructors), and not value classes themselves (as they are typically generic)

Since:
1.8
Default:
org.codehaus.jackson.map.JsonSerializer.None.class

keyUsing

public abstract Class<? extends JsonSerializer<?>> keyUsing
Serializer class to use for serializing Map keys of annotated property. Can only be used on properties (methods, fields, constructors), and not value classes themselves.

Since:
1.8
Default:
org.codehaus.jackson.map.JsonSerializer.None.class

as

public abstract Class<?> as
Supertype (of declared type, which itself is supertype of runtime type) to use as type when locating serializer to use.

Bogus type NoClass can be used to indicate that declared type is used as is (i.e. this annotation property has no setting); this since annotation properties are not allowed to have null value.

Note: if using() is also used it has precedence (since it directly specifies serializer, whereas this would only be used to locate the serializer) and value of this annotation property is ignored.

Default:
org.codehaus.jackson.map.annotate.NoClass.class

keyAs

public abstract Class<?> keyAs
Concrete type to serialize keys of Map as, instead of type otherwise declared. Must be a supertype of declared type; otherwise an exception may be thrown by serializer.

Default:
org.codehaus.jackson.map.annotate.NoClass.class

contentAs

public abstract Class<?> contentAs
Concrete type to serialize content value (elements of a Collection/array, values of Maps) as, instead of type otherwise declared. Must be a supertype of declared type; otherwise an exception may be thrown by serializer.

Default:
org.codehaus.jackson.map.annotate.NoClass.class

typing

public abstract JsonSerialize.Typing typing
Whether type detection used is dynamic or static: that is, whether actual runtime type is used (dynamic), or just the declared type (static).

Since:
1.2
Default:
org.codehaus.jackson.map.annotate.JsonSerialize.Typing.DYNAMIC

include

public abstract JsonSerialize.Inclusion include
Which properties of annotated Bean are to be included in serialization (has no effect on other types like enums, primitives or collections). Choices are "all", "properties that have value other than null" and "properties that have non-default value" (i.e. default value being property setting for a Bean constructed with default no-arg constructor, often null).

Default:
org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS