Annotation Interface JsonTypeInfo
Some examples of typical annotations:
// Include Java class name ("com.myempl.ImplClass") as JSON property "class" @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class") // Include logical type name (defined in impl classes) as wrapper; 2 annotations @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT) @JsonSubTypes({com.myemp.Impl1.class, com.myempl.Impl2.class})Alternatively you can also define fully customized type handling by using
JsonTypeResolver
annotation.
NOTE: originally this annotation was only available to use with types (classes), but starting with 1.7, it is also allowed for properties (fields, methods, constructor parameters).
When used for properties (fields, methods), this annotation applies
to values: so when applied to structure types
(like Collection
, Map
, arrays),
will apply to contained values, not the container;
for non-structured types there is no difference.
This is identical to how JAXB handles type information
annotations; and is chosen since it is the dominant use case.
There is no per-property way to force type information to be included
for type of container (structured type); for container types one has
to use annotation for type declaration.
- Since:
- 1.5 (but available to fields, methods and constructor parameters since 1.7)
- Author:
- tatu
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Definition of standard type inclusion mechanisms for type metadata.static enum
Definition of different type identifiers that can be included in JSON during serialization, and used for deserialization.static class
This marker class that is only to be used withdefaultImpl
annotation property, to indicate that there is no default implementation specified. -
Required Element Summary
Required ElementsModifier and TypeRequired ElementDescriptionWhat kind of type metadata is to be used for serializing and deserializing type information for instances of annotated type (and its subtypes unless overridden) -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionClass<?>
Optional property that can be used to specify default implementation class to use if type identifier is either not present, or can not be mapped to a registered type (which can occur for ids, but not when specifying explicit class to use).What mechanism is used for including type metadata (if any; forJsonTypeInfo.Id.NONE
nothing is included).Property names used when type inclusion method (JsonTypeInfo.As.PROPERTY
) is used (or possibly when using type metadata of typeJsonTypeInfo.Id.CUSTOM
).
-
Element Details
-
use
JsonTypeInfo.Id useWhat kind of type metadata is to be used for serializing and deserializing type information for instances of annotated type (and its subtypes unless overridden)
-
-
-
include
JsonTypeInfo.As includeWhat mechanism is used for including type metadata (if any; forJsonTypeInfo.Id.NONE
nothing is included). DefaultNote that for type metadata type of
JsonTypeInfo.Id.CUSTOM
, this setting may or may not have any effect.- Default:
- PROPERTY
-
property
String propertyProperty names used when type inclusion method (JsonTypeInfo.As.PROPERTY
) is used (or possibly when using type metadata of typeJsonTypeInfo.Id.CUSTOM
).Default property name used if this property is not explicitly defined (or is set to empty String) is based on type metadata type (
use()
) used.- Default:
- ""
-
defaultImpl
Class<?> defaultImplOptional property that can be used to specify default implementation class to use if type identifier is either not present, or can not be mapped to a registered type (which can occur for ids, but not when specifying explicit class to use).Note that while this property allows specification of the default implementation to use, it does not help with structural issues that may arise if type information is missing. This means that most often this is used with type-name -based resolution, to cover cases where new sub-types are added, but base type is not changed to reference new sub-types.
- Since:
- 1.9
- Default:
- org.codehaus.jackson.annotate.JsonTypeInfo.None.class
-