1 package com.fasterxml.jackson.databind.deser; 2 3 import com.fasterxml.jackson.databind.*; 4 import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; 5 import com.fasterxml.jackson.databind.type.*; 6 7 /** 8 * Abstract class that defines API used by {@link DeserializationContext} 9 * to construct actual 10 * {@link JsonDeserializer} instances (which are then cached by 11 * context and/or dedicated cache). 12 *<p> 13 * Since there are multiple broad categories of deserializers, there are 14 * multiple factory methods: 15 *<ul> 16 * <li>For JSON "Array" type, we need 2 methods: one to deal with expected 17 * Java arrays ({@link #createArrayDeserializer}) 18 * and the other for other Java containers like {@link java.util.List}s 19 * and {@link java.util.Set}s ({@link #createCollectionDeserializer}). 20 * Actually there is also a third method for "Collection-like" types; 21 * things like Scala collections that act like JDK collections but do not 22 * implement same interfaces. 23 * </li> 24 * <li>For JSON "Object" type, we need 2 methods: one to deal with 25 * expected Java {@link java.util.Map}s 26 * ({@link #createMapDeserializer}), and another for POJOs 27 * ({@link #createBeanDeserializer}. 28 * As an additional twist there is also a callback for "Map-like" types, 29 * mostly to make it possible to support Scala Maps (which are NOT JDK 30 * Map compatible). 31 * </li> 32 * <li>For Tree Model ({@link com.fasterxml.jackson.databind.JsonNode}) properties there is 33 * {@link #createTreeDeserializer} 34 * <li>For enumerated types ({@link java.lang.Enum}) there is 35 * {@link #createEnumDeserializer} 36 * </li> 37 * <li>For all other types, {@link #createBeanDeserializer} is used. 38 * </ul> 39 *<p> 40 */ 41 public abstract class DeserializerFactory 42 { 43 protected final static Deserializers[] NO_DESERIALIZERS = new Deserializers[0]; 44 45 /* 46 /******************************************************** 47 /* Configuration handling 48 /******************************************************** 49 */ 50 51 /** 52 * Convenience method for creating a new factory instance with additional deserializer 53 * provider. 54 */ withAdditionalDeserializers(Deserializers additional)55 public abstract DeserializerFactory withAdditionalDeserializers(Deserializers additional); 56 57 /** 58 * Convenience method for creating a new factory instance with additional 59 * {@link KeyDeserializers}. 60 */ withAdditionalKeyDeserializers(KeyDeserializers additional)61 public abstract DeserializerFactory withAdditionalKeyDeserializers(KeyDeserializers additional); 62 63 /** 64 * Convenience method for creating a new factory instance with additional 65 * {@link BeanDeserializerModifier}. 66 */ withDeserializerModifier(BeanDeserializerModifier modifier)67 public abstract DeserializerFactory withDeserializerModifier(BeanDeserializerModifier modifier); 68 69 /** 70 * Convenience method for creating a new factory instance with additional 71 * {@link AbstractTypeResolver}. 72 */ withAbstractTypeResolver(AbstractTypeResolver resolver)73 public abstract DeserializerFactory withAbstractTypeResolver(AbstractTypeResolver resolver); 74 75 /** 76 * Convenience method for creating a new factory instance with additional 77 * {@link ValueInstantiators}. 78 */ withValueInstantiators(ValueInstantiators instantiators)79 public abstract DeserializerFactory withValueInstantiators(ValueInstantiators instantiators); 80 81 /* 82 /********************************************************** 83 /* Basic DeserializerFactory API: 84 /********************************************************** 85 */ 86 87 /** 88 * Method that can be called to try to resolve an abstract type 89 * (interface, abstract class) into a concrete type, or at least 90 * something "more concrete" (abstract class instead of interface). 91 * Will either return passed type, or a more specific type. 92 */ mapAbstractType(DeserializationConfig config, JavaType type)93 public abstract JavaType mapAbstractType(DeserializationConfig config, JavaType type) 94 throws JsonMappingException; 95 96 /** 97 * Method that is to find all creators (constructors, factory methods) 98 * for the bean type to deserialize. 99 */ findValueInstantiator(DeserializationContext ctxt, BeanDescription beanDesc)100 public abstract ValueInstantiator findValueInstantiator(DeserializationContext ctxt, 101 BeanDescription beanDesc) 102 throws JsonMappingException; 103 104 /** 105 * Method called to create (or, for completely immutable deserializers, 106 * reuse) a deserializer that can convert JSON content into values of 107 * specified Java "bean" (POJO) type. 108 * At this point it is known that the type is not otherwise recognized 109 * as one of structured types (array, Collection, Map) or a well-known 110 * JDK type (enum, primitives/wrappers, String); this method only 111 * gets called if other options are exhausted. This also means that 112 * this method can be overridden to add support for custom types. 113 * 114 * @param type Type to be deserialized 115 */ createBeanDeserializer(DeserializationContext ctxt, JavaType type, BeanDescription beanDesc)116 public abstract JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ctxt, 117 JavaType type, BeanDescription beanDesc) 118 throws JsonMappingException; 119 120 /** 121 * Method called to create a deserializer that will use specified Builder 122 * class for building value instances. 123 */ createBuilderBasedDeserializer( DeserializationContext ctxt, JavaType type, BeanDescription beanDesc, Class<?> builderClass)124 public abstract JsonDeserializer<Object> createBuilderBasedDeserializer( 125 DeserializationContext ctxt, JavaType type, BeanDescription beanDesc, 126 Class<?> builderClass) 127 throws JsonMappingException; 128 129 createEnumDeserializer(DeserializationContext ctxt, JavaType type, BeanDescription beanDesc)130 public abstract JsonDeserializer<?> createEnumDeserializer(DeserializationContext ctxt, 131 JavaType type, BeanDescription beanDesc) 132 throws JsonMappingException; 133 134 /** 135 * @since 2.7 136 */ createReferenceDeserializer(DeserializationContext ctxt, ReferenceType type, BeanDescription beanDesc)137 public abstract JsonDeserializer<?> createReferenceDeserializer(DeserializationContext ctxt, 138 ReferenceType type, BeanDescription beanDesc) 139 throws JsonMappingException; 140 141 /** 142 * Method called to create and return a deserializer that can construct 143 * JsonNode(s) from JSON content. 144 */ createTreeDeserializer(DeserializationConfig config, JavaType type, BeanDescription beanDesc)145 public abstract JsonDeserializer<?> createTreeDeserializer(DeserializationConfig config, 146 JavaType type, BeanDescription beanDesc) 147 throws JsonMappingException; 148 149 /** 150 * Method called to create (or, for completely immutable deserializers, 151 * reuse) a deserializer that can convert JSON content into values of 152 * specified Java type. 153 * 154 * @param type Type to be deserialized 155 */ createArrayDeserializer(DeserializationContext ctxt, ArrayType type, BeanDescription beanDesc)156 public abstract JsonDeserializer<?> createArrayDeserializer(DeserializationContext ctxt, 157 ArrayType type, BeanDescription beanDesc) 158 throws JsonMappingException; 159 createCollectionDeserializer(DeserializationContext ctxt, CollectionType type, BeanDescription beanDesc)160 public abstract JsonDeserializer<?> createCollectionDeserializer(DeserializationContext ctxt, 161 CollectionType type, BeanDescription beanDesc) 162 throws JsonMappingException; 163 createCollectionLikeDeserializer(DeserializationContext ctxt, CollectionLikeType type, BeanDescription beanDesc)164 public abstract JsonDeserializer<?> createCollectionLikeDeserializer(DeserializationContext ctxt, 165 CollectionLikeType type, BeanDescription beanDesc) 166 throws JsonMappingException; 167 createMapDeserializer(DeserializationContext ctxt, MapType type, BeanDescription beanDesc)168 public abstract JsonDeserializer<?> createMapDeserializer(DeserializationContext ctxt, 169 MapType type, BeanDescription beanDesc) 170 throws JsonMappingException; 171 createMapLikeDeserializer(DeserializationContext ctxt, MapLikeType type, BeanDescription beanDesc)172 public abstract JsonDeserializer<?> createMapLikeDeserializer(DeserializationContext ctxt, 173 MapLikeType type, BeanDescription beanDesc) 174 throws JsonMappingException; 175 176 /** 177 * Method called to find if factory knows how to create a key deserializer 178 * for specified type; currently this means checking if a module has registered 179 * possible deserializers. 180 * 181 * @return Key deserializer to use for specified type, if one found; null if not 182 * (and default key deserializer should be used) 183 */ createKeyDeserializer(DeserializationContext ctxt, JavaType type)184 public abstract KeyDeserializer createKeyDeserializer(DeserializationContext ctxt, 185 JavaType type) 186 throws JsonMappingException; 187 188 /** 189 * Method called to find and create a type information deserializer for given base type, 190 * if one is needed. If not needed (no polymorphic handling configured for type), 191 * should return null. 192 *<p> 193 * Note that this method is usually only directly called for values of container (Collection, 194 * array, Map) types and root values, but not for bean property values. 195 * 196 * @param baseType Declared base type of the value to deserializer (actual 197 * deserializer type will be this type or its subtype) 198 * 199 * @return Type deserializer to use for given base type, if one is needed; null if not. 200 */ findTypeDeserializer(DeserializationConfig config, JavaType baseType)201 public abstract TypeDeserializer findTypeDeserializer(DeserializationConfig config, 202 JavaType baseType) 203 throws JsonMappingException; 204 205 /** 206 * Method that can be used to check if databind module has explicitly declared deserializer 207 * for given (likely JDK) type, explicit meaning that there is specific deserializer for 208 * given type as opposed to auto-generated "Bean" deserializer. Factory itself will check 209 * for known JDK-provided types, but registered {@link com.fasterxml.jackson.databind.Module}s 210 * are also called to see if they might provide explicit deserializer. 211 *<p> 212 * Main use for this method is with Safe Default Typing (and generally Safe Polymorphic 213 * Deserialization), during which it is good to be able to check that given raw type 214 * is explicitly supported and as such "known type" (as opposed to potentially 215 * dangerous "gadget type" which could be exploited). 216 *<p> 217 * This matches {@code Deserializers.Base.hasDeserializerFor(Class)} method, which is 218 * the mechanism used to determine if a {@code Module} might provide an explicit 219 * deserializer instead of core databind. 220 * 221 * @since 2.11 222 */ hasExplicitDeserializerFor(DeserializationConfig config, Class<?> valueType)223 public abstract boolean hasExplicitDeserializerFor(DeserializationConfig config, 224 Class<?> valueType); 225 } 226