Annotation Type JsonTypeInfo


  • @Target({ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})
    @Retention(RUNTIME)
    public @interface JsonTypeInfo
    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 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.

    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 Object or Serializable) 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.

    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      JsonTypeInfo.Id 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.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.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).
      JsonTypeInfo.As include
      Specifies mechanism to use for including type metadata (if any; for JsonTypeInfo.Id.NONE nothing is included); used when serializing, and expected when deserializing.
      java.lang.String property
      Property names used when type inclusion method (JsonTypeInfo.As.PROPERTY) is used (or possibly when using type metadata of type JsonTypeInfo.Id.CUSTOM).
      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 TypeDeserializer (false).
    • Element Detail

      • use

        JsonTypeInfo.Id 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.
      • include

        JsonTypeInfo.As include
        Specifies mechanism to use for including type metadata (if any; for JsonTypeInfo.Id.NONE nothing is included); used when serializing, and expected when deserializing.

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

        Default:
        com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY
      • property

        java.lang.String property
        Property names used when type inclusion method (JsonTypeInfo.As.PROPERTY) is used (or possibly when using type metadata of type JsonTypeInfo.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.

        Default:
        ""
      • defaultImpl

        java.lang.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). 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 sub-types are added, but base type is not changed to reference new sub-types.

        There are certain special values that indicate alternate behavior:

        • Void means that objects with unmappable (or missing) type are to be mapped to null references. For backwards compatibility (2.5 and below), value of com.fasterxml.jackson.databind.annotation.NoClass is also allowed for such usage.
        • Placeholder value of JsonTypeInfo (that is, this annotation type itself} means "there is no default implementation" (in which case an error results from unmappable type). For backwards compatibility with earlier versions (2.5 and below), value of JsonTypeInfo.None may also be used.
        Default:
        com.fasterxml.jackson.annotation.JsonTypeInfo.class
      • visible

        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 TypeDeserializer (false). Property has no effect on serialization.

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

        Since:
        2.0
        Default:
        false