Jackson polymorphic serialization with Autovalue

One of the most powerful features in the Java JSON library Jackson, is support for polymorphic serialization. That is, when deserializaing a JSON (or XML) blob to a supertype, with proper configuration, an object of the correct subtype will be instantiated. Likewise, type information will be included in serialization.

This allows for Java clients and servers to communicate using JSON and use a common interface to share types.

Typically this can be achieved using the JsonTypeInfo and JsonSubTypes annotation above. The JsonTypeInfo annotation allows for considerable flexibility in how the type information is included in the serialized object.

However, I did find one situation where using the @JsonSubTypes annotation directly on the interface is insufficient. This is when using <>AutoValue.

Adding @JsonSubTypes.Type(name="horse", value=Horse.class) to Animal will not provide the symmetric serialization we may expect here. Instead we’ll find the value for type in our JSON markup will be the generated type AutoValue_Horse and as a result we will not be able to deserialize the JSON blob to an Animal as that type isn’t known to the Animal interface.

The workaround here is adding additional type information to Horse.

While it requires an additional annotation, adding JsonTypeName to Horse ensures that even the generated type AutoValue_Horse will include the correct type name in the JSON markup so polymorphic serialization behaves as intended!

Comments are closed.