Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.annotation;
Annotation used for configuring details of if and how type information is used with JSON serialization and deserialization, to preserve information about actual class of Object instances. This is necessarily for polymorphic types, and may also be needed to link abstract declared types and matching concrete implementation.

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.util.Collection, java.util.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.

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.

 public @interface JsonTypeInfo
     /* Value enumerations used for properties
Definition of different type identifiers that can be included in JSON during serialization, and used for deserialization.
     public enum Id {
This means that no explicit type metadata is included, and typing is purely done using contextual information possibly augmented with other annotations.

Means that fully-qualified Java class name is used as the type identifier.

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 "", only ".Impl" would be included; and for "" 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, java.lang.Object would not be very useful reference point), this may not always work as expected.


Means that logical type name is used as type information; name will then need to be separately resolved to actual concrete type (Class).

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.
        private final String _defaultPropertyName;
        private Id(String defProp) {
             = defProp;
        public String getDefaultPropertyName() { return ; }

Definition of standard type inclusion mechanisms for type metadata. Used for standard metadata types, except for JsonTypeInfo.Id.NONE. May or may not be used for custom types (JsonTypeInfo.Id.CUSTOM).
    public enum As {
Inclusion mechanism that uses a single configurable property, included along with actual data (POJO properties) as a separate meta-property.

Default choice for inclusion.


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.


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.
Inclusion mechanism similar to PROPERTY, except that property is included one-level higher in hierarchy, i.e. as sibling property at same level as JSON Object to type. Note that this choice can only be used for properties, not for types (classes). Trying to use it for classes will result in inclusion strategy of basic PROPERTY instead.
    /* Annotation properties
What kind of type metadata is to be used for serializing and deserializing type information for instances of annotated type (and its subtypes unless overridden)
    public Id use();    
What mechanism is used for including type metadata (if any; for Id.NONE nothing is included). Default

Note that for type metadata type of Id.CUSTOM, this setting may or may not have any effect.

    public As include() default .;

Property names used when type inclusion method (As.PROPERTY) is used (or possibly when using type metadata of type Id.CUSTOM). If POJO itself has a property with same name, value of property will be set with type id metadata: if no such property exists, type id is only used for determining actual type.

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.

    public String property() default "";

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

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.

    public Class<?> defaultImpl() default None.class;

Property that defines whether type identifier value will be passed as part of JSON stream to deserializer (true), or handled and removed by TypeDeserializer (false).

Default value is false, meaning that Jackson handles and removes the type identifier from JSON content that is passed to JsonDeserializer.

    public boolean visible() default false;
    /* Helper classes

This marker class that is only to be used with defaultImpl annotation property, to indicate that there is no default implementation specified.
    public abstract static class None { }
New to GrepCode? Check out our FAQ X