@Target({METHOD,FIELD,TYPE,PARAMETER})
@Retention(RUNTIME)
public @interface JsonSerialize
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
Modifier and Type | Optional Element | Description |
---|---|---|
java.lang.Class<?> |
as |
Supertype (of declared type, which itself is supertype of runtime type)
to use as type when locating serializer to use.
|
java.lang.Class<?> |
contentAs |
Concrete type to serialize content value (elements
of a Collection/array, values of Maps) as,
instead of type otherwise declared.
|
java.lang.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).
|
java.lang.Class<?> |
keyAs |
Concrete type to serialize keys of
Map as,
instead of type otherwise declared. |
java.lang.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).
|
java.lang.Class<? extends JsonSerializer<?>> |
using |
Serializer class to use for
serializing associated value.
|
java.lang.Class<? extends JsonSerializer<?>> using
java.lang.Class<? extends JsonSerializer<?>> contentUsing
java.lang.Class<? extends JsonSerializer<?>> keyUsing
java.lang.Class<?> as
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.
java.lang.Class<?> keyAs
Map
as,
instead of type otherwise declared.
Must be a supertype of declared type; otherwise an exception may be
thrown by serializer.java.lang.Class<?> contentAs
JsonSerialize.Typing typing
JsonSerialize.Inclusion include