1 package com.fasterxml.jackson.databind.jsontype; 2 3 import java.io.IOException; 4 5 import com.fasterxml.jackson.annotation.JsonTypeInfo; 6 import com.fasterxml.jackson.databind.DatabindContext; 7 import com.fasterxml.jackson.databind.JavaType; 8 9 /** 10 * Interface that defines standard API for converting types 11 * to type identifiers and vice versa. Used by type resolvers 12 * ({@link com.fasterxml.jackson.databind.jsontype.TypeSerializer}, 13 * {@link com.fasterxml.jackson.databind.jsontype.TypeDeserializer}) for converting 14 * between type and matching id; id is stored in JSON and needed for 15 * creating instances of proper subtypes when deserializing values. 16 *<p> 17 * NOTE: it is <b>strongly</b> recommended that developers always extend 18 * abstract base class {@link com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase} 19 * instead of directly implementing this interface; this helps prevent 20 * breakage in case new methds need to be added in this interface (something 21 * we try to avoid doing; but which may be necessary in some cases). 22 */ 23 public interface TypeIdResolver 24 { 25 /* 26 /********************************************************** 27 /* Initialization/configuration methods 28 /********************************************************** 29 */ 30 31 /** 32 * Method that will be called once before any type resolution calls; 33 * used to initialize instance with configuration. This is necessary 34 * since instances may be created via reflection, without ability to 35 * call specific constructor to pass in configuration settings. 36 * 37 * @param baseType Base type for which this id resolver instance is 38 * used 39 */ init(JavaType baseType)40 public void init(JavaType baseType); 41 42 /* 43 /********************************************************** 44 /* Conversions between types and type ids 45 /********************************************************** 46 */ 47 48 /** 49 * Method called to serialize type of the type of given value 50 * as a String to include in serialized JSON content. 51 */ idFromValue(Object value)52 public String idFromValue(Object value); 53 54 /** 55 * Alternative method used for determining type from combination of 56 * value and type, using suggested type (that serializer provides) 57 * and possibly value of that type. Most common implementation will 58 * use suggested type as is. 59 */ idFromValueAndType(Object value, Class<?> suggestedType)60 public String idFromValueAndType(Object value, Class<?> suggestedType); 61 62 /** 63 * Method that can be called to figure out type id to use for instances 64 * of base type (declared type of property). This is usually only used 65 * for fallback handling, for cases where real type information is not 66 * available for some reason. 67 */ idFromBaseType()68 public String idFromBaseType(); 69 70 /** 71 * Method called to resolve type from given type identifier. 72 * 73 * @since 2.5 (throws clause added in 2.8) 74 */ typeFromId(DatabindContext context, String id)75 public JavaType typeFromId(DatabindContext context, String id) throws IOException; 76 77 /** 78 * Method called for error-reporting and diagnostics purposes. 79 * 80 * @since 2.7 -- but since 2.5 has existed in {@link com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase} 81 */ getDescForKnownTypeIds()82 public String getDescForKnownTypeIds(); 83 84 /* 85 /********************************************************** 86 /* Accessors for metadata 87 /********************************************************** 88 */ 89 90 /** 91 * Accessor for mechanism that this resolver uses for determining 92 * type id from type. Mostly informational; not required to be called 93 * or used. 94 */ getMechanism()95 public JsonTypeInfo.Id getMechanism(); 96 } 97