public interface MultipleTypeIdResolver
extends com.fasterxml.jackson.databind.jsontype.TypeIdResolver
TypeIdResolver
for serializing and deserializing polymorphic types. The vanilla
implementation of TypeIdResolver only enables a single type id to be serialized with a value, which is not always
sufficient for every consumer of a value with polymorphic typing to identify and instantiate the appropriate type.
This allows more robust polymorphism support, in that consumers of a serialized polymorphic object do not need complete type information. As long as their deserializer can resolve (or, understand) one of the annotated type ids, they can still perform polymorphic deserialization. An example:
class BasicMessage { String id; String message; } class SocialMediaMessage extends BasicMessage { boolean cool; }Let's say we serialized a value into:
{id="123",message="hi",cool=false}.The existing polymorphism support may have annotated this as the following Ion:
SocialMediaMessage::{id="123",message="hi",cool=false}But a consumer who only has BasicMessage in its classloader won't know (or care) what a SocialMediaMessage is, and be stuck. Using this interface enables the following serialization:
SocialMediaMessage::BasicMessage::{id="123",message="hi",cool=false}About particular implementations:
Ion serialization should be using IonAnnotationTypeSerializer
, which is polymorphism-aware, but how should a
MultipleTypeIdResolver handle a call to the non-polymorphic TypeIdResolver.idFromValue(Object)
? I'd probably
do something like return selectId(idsFromValue(value));
, to keep things working when serializing
to non-Ion formats. Throwing a runtime exception is another idea, if you want to forbid non-Ion serialization.
Modifier and Type | Method and Description |
---|---|
String[] |
idsFromValue(Object value)
Provides a list of valid (polymorphic) type ids for the given value.
|
String |
selectId(String[] ids)
Given a set of type ids, select the most 'relevant'.
|
String[] idsFromValue(Object value)
value
- Java object to be serialized.String selectId(String[] ids)
TypeIdResolver.typeFromId(com.fasterxml.jackson.databind.DatabindContext, java.lang.String)
to get a JavaType
. It is a invariant
on this method that its output, if non-null, be valid input for TypeIdResolver.typeFromId(com.fasterxml.jackson.databind.DatabindContext, java.lang.String)
of the
same TypeIdResolver instance.
Note that we're not resolving the array of ids directly into a JavaType because there is code (in the Jackson package, not ours) which consumes the id String itself, not the JavaType object.
ids
- Type ids from a jsonified object. May not be null.Copyright © 2020 FasterXML. All rights reserved.