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 (from databind package).
This annotation can be used both for types (classes) and properties. If both exist, annotation on property has precedence, as it is considered more specific.
When used for properties (fields, methods), this annotation applies
to values: so when applied to structure types
(like java., java., 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.
Note on visibility of type identifier: by default, deserialization
(use during reading of JSON) of type identifier
is completely handled by Jackson, and is not passed to
deserializers. However, if so desired,
it is possible to define property visible = true
in which case property will be passed as-is to deserializers
(and set via setter or field) on deserialization.
On serialization side, Jackson will generate type id by itself,
except if there is a property with name that matches
property(), in which case value of that property is
used instead.
Note
use of type id of "class name" with very general base type
(such as java. or java.)
can potentially open up security holes if deserializing content
generated by untrusted sources. If content can not be trusted,
it is necessary to either use "type name" as type id, or to
limit possible types using other means.
| Modifier and Type | Class and Description |
|---|---|
| public static enum | JsonTypeInfo.As
Definition of standard type inclusion mechanisms for type metadata. |
| public static enum | JsonTypeInfo.Id
Definition of different type identifiers that can be included in JSON during serialization, and used for deserialization. |
| public abstract static class | JsonTypeInfo.None
Deprecated
Since 2.5, use any Annotation type (such as This marker class that is only to be used with JsonTypeInfo),
if such behavior is needed; this is rarely necessary.
defaultImpl
annotation property, to indicate that there is no default implementation
specified.
|
| public static class |
| Modifier and Type | Method and Description |
|---|---|
| public Class | defaultImpl()
Optional property that can be used to specify default implementation class to use for deserialization 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). |
| public JsonTypeInfo. | |
| public String | property()
Property names used when type inclusion method ( |
| public OptBoolean | requireTypeIdForSubtypes()
Specifies whether the type ID should be strictly required during polymorphic deserialization of its subtypes. |
| public JsonTypeInfo. | use()
Specifies kind of type metadata to use when serializing type information for instances of annotated type and its subtypes; as well as what is expected during deserialization. |
| public boolean | visible()
Property that defines whether type identifier value will be passed
as part of JSON stream to deserializer (true), or handled and
removed by |
| defaultImpl | back to summary |
|---|---|
| public Class Optional property that can be used to specify default implementation class to use for deserialization 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). Property has no effect on choice of type id used for serialization; it is only used in deciding what to do for otherwise unmappable cases. 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 subtypes are added, but base type is not changed to reference new subtypes. There are certain special values that indicate alternate behavior:
| |
| include | back to summary |
|---|---|
| public JsonTypeInfo. Specifies mechanism to use for including type metadata (if any; for
Note that for type metadata type of | |
| property | back to summary |
|---|---|
| public String property() Property names used when type inclusion method (
Default property name used if this property is not explicitly defined
(or is set to empty | |
| requireTypeIdForSubtypes | back to summary |
|---|---|
| public OptBoolean requireTypeIdForSubtypes() Specifies whether the type ID should be strictly required during polymorphic deserialization of its subtypes.
If set to Note This setting is specific to this type and will always override
the global configuration of
| |
| use | back to summary |
|---|---|
| public JsonTypeInfo. Specifies kind of type metadata to use when serializing type information for instances of annotated type and its subtypes; as well as what is expected during deserialization. | |
| visible | back to summary |
|---|---|
| public boolean visible() Property that defines whether type identifier value will be passed
as part of JSON stream to deserializer (true), or handled and
removed by
Default value is false, meaning that Jackson handles and removes
the type identifier from JSON content that is passed to
| |
Id#NONE.
May or may not be used for custom types (Id#CUSTOM).
| Modifier and Type | Field and Description |
|---|---|
| public static final JsonTypeInfo. | EXISTING_PROPERTY
Inclusion mechanism similar to |
| public static final JsonTypeInfo. | EXTERNAL_PROPERTY
Inclusion mechanism similar to |
| public static final JsonTypeInfo. | PROPERTY
Inclusion mechanism that uses a single configurable property, included along with actual data (POJO properties) as a separate meta-property. |
| public static final JsonTypeInfo. | WRAPPER_ARRAY
Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a 2-element JSON array: first element is the serialized type identifier, and second element the serialized POJO as JSON Object. |
| public static final JsonTypeInfo. | WRAPPER_OBJECT
Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a JSON Object that has a single entry, where field name is serialized type identifier, and value is the actual JSON value. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static JsonTypeInfo. | |
| public static JsonTypeInfo. |
| EXISTING_PROPERTY | back to summary |
|---|---|
| public static final JsonTypeInfo. Inclusion mechanism similar to
Note that this behavior is quite similar to that of using
| |
| EXTERNAL_PROPERTY | back to summary |
|---|---|
| public static final JsonTypeInfo. Inclusion mechanism similar to
Note also that this mechanism can not be used for container values
(arrays, | |
| PROPERTY | back to summary |
|---|---|
| public static final JsonTypeInfo. Inclusion mechanism that uses a single configurable property, included along with actual data (POJO properties) as a separate meta-property. Default choice for inclusion. | |
| WRAPPER_ARRAY | back to summary |
|---|---|
| public static final JsonTypeInfo. Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a 2-element JSON array: first element is the serialized type identifier, and second element the serialized POJO as JSON Object. | |
| WRAPPER_OBJECT | back to summary |
|---|---|
| public static final JsonTypeInfo. Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a JSON Object that has a single entry, where field name is serialized type identifier, and value is the actual JSON value. Note can only be used if type information can be serialized as String. This is true for standard type metadata types, but not necessarily for custom types. | |
| As | back to summary |
|---|---|
| private As() | |
| valueOf | back to summary |
|---|---|
| public static JsonTypeInfo. | |
| values | back to summary |
|---|---|
| public static JsonTypeInfo. | |
| Modifier and Type | Field and Description |
|---|---|
| private final String | |
| public static final JsonTypeInfo. | CLASS
Means that fully-qualified Java class name is used as the type identifier. |
| public static final JsonTypeInfo. | CUSTOM
Means that typing mechanism uses customized handling, with possibly custom configuration. |
| public static final JsonTypeInfo. | DEDUCTION
Means that no serialized typing-property is used. |
| public static final JsonTypeInfo. | MINIMAL_CLASS
Means that Java class name with minimal path is used as the type identifier. |
| public static final JsonTypeInfo. | NAME
Means that logical type name is used as type information; name will then need to be separately resolved to actual concrete type (Class). |
| public static final JsonTypeInfo. | NONE
This means that no explicit type metadata is included, and typing is purely done using contextual information possibly augmented with other annotations. |
| public static final JsonTypeInfo. | SIMPLE_NAME
Means that the simple name of the Java class, equivalent to the value returned by |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public static JsonTypeInfo. | |
| public static JsonTypeInfo. |
| _defaultPropertyName | back to summary |
|---|---|
| private final String _defaultPropertyName | |
| CLASS | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that fully-qualified Java class name is used as the type identifier. | |
| CUSTOM | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that typing mechanism uses customized handling, with possibly custom configuration. This means that semantics of other properties is not defined by Jackson package, but by the custom implementation. | |
| DEDUCTION | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that no serialized typing-property is used. Types are deduced based
on the fields available. Deduction is limited to the names of fields
(not their values or, consequently, any nested descendants). Exceptions will be
thrown if not enough unique information is present to select a single subtype.
On serialization, no type ID is written, and only regular properties are included.
| |
| MINIMAL_CLASS | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that Java class name with minimal path is used as the type identifier. Minimal means that only the class name, and that part of preceding Java package name is included that is needed to construct fully-qualified name given fully-qualified name of the declared supertype; additionally a single leading dot ('.') must be used to indicate that partial class name is used. For example, for supertype "com.foobar.Base", and concrete type "com.foo.Impl", only ".Impl" would be included; and for "com.foo.impl.Impl2" only ".impl.Impl2" would be included Note leading dot ('.') MUST be used to denote partial (minimal) name; if it is missing, value is assumed to be fully-qualified name. Fully-qualified name is used in cases where subtypes are not in same package (or sub-package thereof) as base class.
If all related classes are in the same Java package, this option can reduce
amount of type information overhead, especially for small types.
However, please note that using this alternative is inherently risky since it
assumes that the
supertype can be reliably detected. Given that it is based on declared type
(since ultimate supertype, | |
| NAME | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that logical type name is used as type information; name will then need to be separately resolved to actual concrete type (Class). | |
| NONE | back to summary |
|---|---|
| public static final JsonTypeInfo. This means that no explicit type metadata is included, and typing is purely done using contextual information possibly augmented with other annotations. | |
| SIMPLE_NAME | back to summary |
|---|---|
| public static final JsonTypeInfo. Means that the simple name of the Java class, equivalent to the value returned by
Note This approach reduces verbosity but requires the simple names to be unique to avoid conflicts. If multiple classes share the same simple name, the last declared one will be used. This approach should be used with careful consideration of your type hierarchy.
| |
| Id | back to summary |
|---|---|
| private Id(String defProp) | |
| getDefaultPropertyName | back to summary |
|---|---|
| public String getDefaultPropertyName() | |
| valueOf | back to summary |
|---|---|
| public static JsonTypeInfo. | |
| values | back to summary |
|---|---|
| public static JsonTypeInfo. | |
Deprecated
Since 2.5, use any Annotation type (such as JsonTypeInfo),
if such behavior is needed; this is rarely necessary.
defaultImpl
annotation property, to indicate that there is no default implementation
specified.
| Access | Constructor and Description |
|---|---|
| public |
| None | back to summary |
|---|---|
| public None() | |
| Modifier and Type | Field and Description |
|---|---|
| protected final Class | |
| protected final JsonTypeInfo. | |
| protected final boolean | |
| protected final JsonTypeInfo. | |
| protected final String | |
| protected final Boolean | |
| protected static final JsonTypeInfo. | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| protected | Value(JsonTypeInfo.
|
| Modifier and Type | Method and Description |
|---|---|
| private static <T> boolean | |
| private static boolean | |
| public static JsonTypeInfo. | construct(JsonTypeInfo.
|
| public boolean | equals(Object
the reference object with which to compare. o)Overrides java. |
| public static JsonTypeInfo. | |
| public Class | |
| public JsonTypeInfo. | |
| public boolean | |
| public JsonTypeInfo. | |
| public String | |
| public Boolean | |
| public int | |
| public static boolean | isEnabled(JsonTypeInfo.
Static helper method for simple(r) checking of whether there's a Value instance that indicates that polymorphic handling is (to be) enabled. |
| public String | |
| public Class | valueFor()
Implements com. |
| public JsonTypeInfo. | |
| public JsonTypeInfo. | |
| public JsonTypeInfo. | |
| public JsonTypeInfo. | |
| public JsonTypeInfo. | |
| public JsonTypeInfo. |
| _defaultImpl | back to summary |
|---|---|
| protected final Class<?> _defaultImpl | |
| _idType | back to summary |
|---|---|
| protected final JsonTypeInfo. | |
| _idVisible | back to summary |
|---|---|
| protected final boolean _idVisible | |
| _inclusionType | back to summary |
|---|---|
| protected final JsonTypeInfo. | |
| _propertyName | back to summary |
|---|---|
| protected final String _propertyName | |
| _requireTypeIdForSubtypes | back to summary |
|---|---|
| protected final Boolean _requireTypeIdForSubtypes | |
| EMPTY | back to summary |
|---|---|
| protected static final JsonTypeInfo. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| Value | back to summary |
|---|---|
| protected Value(JsonTypeInfo. | |
| _equal | back to summary |
|---|---|
| private static <T> boolean _equal(T value1, T value2) | |
| _equals | back to summary |
|---|---|
| private static boolean _equals(JsonTypeInfo. | |
| construct | back to summary |
|---|---|
| public static JsonTypeInfo. | |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| from | back to summary |
|---|---|
| public static JsonTypeInfo. | |
| getDefaultImpl | back to summary |
|---|---|
| public Class | |
| getIdType | back to summary |
|---|---|
| public JsonTypeInfo. | |
| getIdVisible | back to summary |
|---|---|
| public boolean getIdVisible() | |
| getInclusionType | back to summary |
|---|---|
| public JsonTypeInfo. | |
| getPropertyName | back to summary |
|---|---|
| public String getPropertyName() | |
| getRequireTypeIdForSubtypes | back to summary |
|---|---|
| public Boolean getRequireTypeIdForSubtypes() | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isEnabled | back to summary |
|---|---|
| public static boolean isEnabled(JsonTypeInfo. Static helper method for simple(r) checking of whether there's a Value instance that indicates that polymorphic handling is (to be) enabled. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| valueFor | back to summary |
|---|---|
| public Class Implements com. Doc from com. Introspection method that may be used to find actual annotation that may be used as the source for value instance.
| |
| withDefaultImpl | back to summary |
|---|---|
| public JsonTypeInfo. | |
| withIdType | back to summary |
|---|---|
| public JsonTypeInfo. | |
| withIdVisible | back to summary |
|---|---|
| public JsonTypeInfo. | |
| withInclusionType | back to summary |
|---|---|
| public JsonTypeInfo. | |
| withPropertyName | back to summary |
|---|---|
| public JsonTypeInfo. | |
| withRequireTypeIdForSubtypes | back to summary |
|---|---|
| public JsonTypeInfo. | |