Jackson Polymorphic Deserialization Overview | Generated by AI

Home 2025.10

Overview of Jackson Polymorphic Deserialization

Jackson is a Java library for JSON serialization and deserialization. Polymorphic deserialization allows Jackson to automatically instantiate the correct subclass of a base class during deserialization, based on type information embedded in the JSON. This is useful when dealing with inheritance hierarchies, ensuring the right object type is created from JSON data.

Key components:

Without these, Jackson would default to deserializing all objects as the base class, potentially losing subclass-specific data.

How It Works Step-by-Step

  1. Annotations on the Base Class:
    • Use @JsonTypeInfo to specify where type info is embedded (e.g., as a property in the JSON object).
    • Example:
      @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
      @JsonSubTypes({
          @JsonSubType(value = Cat.class, name = "cat"),
          @JsonSubType(value = Dog.class, name = "dog")
      })
      public abstract class Animal {
          public String name;
      }
      
      • use = JsonTypeInfo.Id.NAME: Uses a name (string identifier) for the type.
      • include = JsonTypeInfo.As.PROPERTY: Adds the type info as a property (“@type”) in the JSON object.
      • @JsonSubTypes: Maps subclass names to their Java classes (e.g., “cat” → Cat.class).
  2. Serialization Process:
    • When serializing a Cat or Dog object, Jackson adds the type identifier to the JSON.
    • Example output: {"@type": "cat", "name": "Whiskers", "purr": true} (if Cat has a “purr” field).
  3. Deserialization Process:
    • Jackson reads the JSON and checks the type info (e.g., “@type” property).
    • It maps the identifier (“cat”) back to the registered subclass (Cat.class) using @JsonSubTypes.
    • Instantiates the correct subclass and populates its fields.
    • If no match or missing type info, it defaults to the base class or throws exceptions (configurable via defaultImpl).
  4. Supported Type Info Formats:
    • @JsonTypeInfo.As.PROPERTY: Type as a field (e.g., {"@type": "cat", ...}).
    • @JsonTypeInfo.As.WRAPPER_OBJECT: Wraps the object in a wrapper with the type as the key (e.g., {"cat": {...}}).
    • @JsonTypeInfo.As.WRAPPER_ARRAY: Uses an array format.
    • @JsonTypeInfo.As.EXTERNAL_PROPERTY: Type info in a separate field (advanced, for XML-like structures).

Advanced Configuration and Edge Cases

For full docs, see Jackson’s official guide: Polymorphic Type Handling. This feature has been stable since Jackson 2.x, with similar concepts in other JSON libraries like Gson. If customizing further, extend PolymorphicTypeValidator for security in untrusted inputs.


Back

x-ai/grok-code-fast-1

Donate