1 package com.fasterxml.jackson.databind.deser; 2 3 import java.util.List; 4 5 import com.fasterxml.jackson.databind.*; 6 import com.fasterxml.jackson.databind.deser.BeanDeserializer; 7 import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory; 8 import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; 9 import com.fasterxml.jackson.databind.type.ArrayType; 10 import com.fasterxml.jackson.databind.type.CollectionLikeType; 11 import com.fasterxml.jackson.databind.type.CollectionType; 12 import com.fasterxml.jackson.databind.type.MapLikeType; 13 import com.fasterxml.jackson.databind.type.MapType; 14 import com.fasterxml.jackson.databind.type.ReferenceType; 15 16 /** 17 * Abstract class that defines API for objects that can be registered 18 * to participate in constructing {@link JsonDeserializer} instances 19 * (via {@link DeserializerFactory}). 20 * This is typically done by modules that want alter some aspects of deserialization 21 * process; and is preferable to sub-classing of {@link BeanDeserializerFactory}. 22 *<p> 23 * Note that Jackson 2.2 adds more methods for customization; with earlier versions 24 * only {@link BeanDeserializer} instances could be modified, but with 2.2 all types 25 * of deserializers can be changed. 26 *<p> 27 * Sequence in which callback methods are called for {@link BeanDeserializer} is: 28 * <ol> 29 * <li>{@link #updateProperties} is called once all property definitions are 30 * collected, and initial filtering (by ignorable type and explicit ignoral-by-bean) 31 * has been performed. 32 * </li> 33 * <li>{@link #updateBuilder} is called once all initial pieces for building deserializer 34 * have been collected 35 * </li> 36 * <li>{@link #modifyDeserializer} is called after deserializer has been built 37 * by {@link BeanDeserializerBuilder} 38 * but before it is returned to be used 39 * </li> 40 * </ol> 41 *<p> 42 * For other types of deserializers, methods called depend on type of values for 43 * which deserializer is being constructed; and only a single method is called 44 * since the process does not involve builders (unlike that of {@link BeanDeserializer}. 45 *<p> 46 * Default method implementations are "no-op"s, meaning that methods are implemented 47 * but have no effect; this is mostly so that new methods can be added in later 48 * versions. 49 */ 50 public abstract class BeanDeserializerModifier 51 { 52 /** 53 * Method called by {@link BeanDeserializerFactory} when it has collected 54 * initial list of {@link BeanPropertyDefinition}s, and done basic by-name 55 * and by-type filtering, but before constructing builder or actual 56 * property handlers; or arranging order. 57 * 58 * The most common changes to make at this point are to completely remove 59 * specified properties, or rename then: other modifications are easier 60 * to make at later points. 61 */ updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs)62 public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, 63 BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) { 64 return propDefs; 65 } 66 67 /** 68 * Method called by {@link BeanDeserializerFactory} when it has collected 69 * basic information such as tentative list of properties to deserialize. 70 * 71 * Implementations may choose to modify state of builder (to affect deserializer being 72 * built), or even completely replace it (if they want to build different kind of 73 * deserializer). Typically changes mostly concern set of properties to deserialize. 74 */ updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder)75 public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, 76 BeanDescription beanDesc, BeanDeserializerBuilder builder) { 77 return builder; 78 } 79 80 /** 81 * Method called by {@link BeanDeserializerFactory} after constructing default 82 * bean deserializer instance with properties collected and ordered earlier. 83 * Implementations can modify or replace given deserializer and return deserializer 84 * to use. Note that although initial deserializer being passed is usually of type 85 * {@link BeanDeserializer}, modifiers may return deserializers of other types; 86 * and this is why implementations must check for type before casting. 87 *<p> 88 * Since 2.10 this is also called for custom deserializers for types not deemed to 89 * be of any more specific (reference, enum, array, collection(-like), map(-like), 90 * node type) 91 */ modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer)92 public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, 93 BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 94 return deserializer; 95 } 96 97 /* 98 /********************************************************** 99 /* Callback methods for other types (since 2.2) 100 /********************************************************** 101 */ 102 103 /** 104 * Method called by {@link BeanDeserializerFactory} after constructing default 105 * enum type deserializer instance. 106 * 107 * @since 2.2 108 */ modifyEnumDeserializer(DeserializationConfig config, JavaType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)109 public JsonDeserializer<?> modifyEnumDeserializer(DeserializationConfig config, 110 JavaType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 111 return deserializer; 112 } 113 114 /** 115 * Method called by {@link BeanDeserializerFactory} after constructing default 116 * {@link ReferenceType} deserializer instance. 117 * 118 * @since 2.7 119 */ modifyReferenceDeserializer(DeserializationConfig config, ReferenceType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)120 public JsonDeserializer<?> modifyReferenceDeserializer(DeserializationConfig config, 121 ReferenceType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 122 return deserializer; 123 } 124 125 /** 126 * Method called by {@link DeserializerFactory} after it has constructed the 127 * standard deserializer for given 128 * {@link ArrayType} 129 * to make it possible to either replace or augment this deserializer with 130 * additional functionality. 131 * 132 * @param config Configuration in use 133 * @param valueType Type of the value deserializer is used for. 134 * @param beanDesc Description f 135 * @param deserializer Default deserializer that would be used. 136 * 137 * @return Deserializer to use; either <code>deserializer</code> that was passed 138 * in, or an instance method constructed. 139 * 140 * @since 2.2 141 */ modifyArrayDeserializer(DeserializationConfig config, ArrayType valueType, BeanDescription beanDesc, JsonDeserializer<?> deserializer)142 public JsonDeserializer<?> modifyArrayDeserializer(DeserializationConfig config, 143 ArrayType valueType, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 144 return deserializer; 145 } 146 147 /** 148 * Method called by {@link BeanDeserializerFactory} after constructing default 149 * {@link CollectionType} deserializer instance. 150 * 151 * @since 2.2 152 */ modifyCollectionDeserializer(DeserializationConfig config, CollectionType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)153 public JsonDeserializer<?> modifyCollectionDeserializer(DeserializationConfig config, 154 CollectionType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 155 return deserializer; 156 } 157 158 /** 159 * Method called by {@link BeanDeserializerFactory} after constructing default 160 * {@link CollectionLikeType} deserializer instance. 161 * 162 * @since 2.2 163 */ modifyCollectionLikeDeserializer(DeserializationConfig config, CollectionLikeType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)164 public JsonDeserializer<?> modifyCollectionLikeDeserializer(DeserializationConfig config, 165 CollectionLikeType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 166 return deserializer; 167 } 168 169 /** 170 * Method called by {@link BeanDeserializerFactory} after constructing default 171 * {@link MapType} deserializer instance. 172 * 173 * @since 2.2 174 */ modifyMapDeserializer(DeserializationConfig config, MapType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)175 public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, 176 MapType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 177 return deserializer; 178 } 179 180 /** 181 * Method called by {@link BeanDeserializerFactory} after constructing default 182 * {@link MapLikeType} deserializer instance. 183 * 184 * @since 2.2 185 */ modifyMapLikeDeserializer(DeserializationConfig config, MapLikeType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer)186 public JsonDeserializer<?> modifyMapLikeDeserializer(DeserializationConfig config, 187 MapLikeType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { 188 return deserializer; 189 } 190 191 /** 192 * Method called by {@link DeserializerFactory} after it has constructed the 193 * standard key deserializer for given key type. 194 * This make it possible to replace the default key deserializer, or augment 195 * it somehow (including optional use of default deserializer with occasional 196 * override). 197 * 198 * @since 2.2 199 */ modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deserializer)200 public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, 201 JavaType type, KeyDeserializer deserializer) { 202 return deserializer; 203 } 204 }