1 package com.fasterxml.jackson.databind.ser; 2 3 import java.util.List; 4 5 import com.fasterxml.jackson.databind.*; 6 import com.fasterxml.jackson.databind.deser.DeserializerFactory; 7 import com.fasterxml.jackson.databind.type.*; 8 9 /** 10 * Abstract class that defines API for objects that can be registered (for {@link BeanSerializerFactory} 11 * to participate in constructing {@link BeanSerializer} instances. 12 * This is typically done by modules that want alter some aspects of serialization 13 * process; and is preferable to sub-classing of {@link BeanSerializerFactory}. 14 *<p> 15 * Sequence in which callback methods are called is as follows: 16 * <ol> 17 * <li>After factory has collected tentative set of properties (instances of 18 * <code>BeanPropertyWriter</code>) is sent for modification via 19 * {@link #changeProperties}. Changes can include removal, addition and 20 * replacement of suggested properties. 21 * <li>Resulting set of properties are ordered (sorted) by factory, as per 22 * configuration, and then {@link #orderProperties} is called to allow 23 * modifiers to alter ordering. 24 * <li>After all bean properties and related information is accumulated, 25 * {@link #updateBuilder} is called with builder, to allow builder state 26 * to be modified (including possibly replacing builder itself if necessary) 27 * <li>Once all bean information has been determined, 28 * factory creates default {@link BeanSerializer} instance and passes 29 * it to modifiers using {@link #modifySerializer}, for possible 30 * modification or replacement (by any {@link com.fasterxml.jackson.databind.JsonSerializer} instance) 31 * </ol> 32 *<p> 33 * Default method implementations are "no-op"s, meaning that methods are implemented 34 * but have no effect. 35 */ 36 public abstract class BeanSerializerModifier 37 { 38 /** 39 * Method called by {@link BeanSerializerFactory} with tentative set 40 * of discovered properties. 41 * Implementations can add, remove or replace any of passed properties. 42 * 43 * Properties <code>List</code> passed as argument is modifiable, and returned List must 44 * likewise be modifiable as it may be passed to multiple registered 45 * modifiers. 46 */ changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties)47 public List<BeanPropertyWriter> changeProperties(SerializationConfig config, 48 BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { 49 return beanProperties; 50 } 51 52 /** 53 * Method called by {@link BeanSerializerFactory} with set of properties 54 * to serialize, in default ordering (based on defaults as well as 55 * possible type annotations). 56 * Implementations can change ordering any way they like. 57 * 58 * Properties <code>List</code> passed as argument is modifiable, and returned List must 59 * likewise be modifiable as it may be passed to multiple registered 60 * modifiers. 61 */ orderProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties)62 public List<BeanPropertyWriter> orderProperties(SerializationConfig config, 63 BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { 64 return beanProperties; 65 } 66 67 /** 68 * Method called by {@link BeanSerializerFactory} after collecting all information 69 * regarding POJO to serialize and updating builder with it, but before constructing 70 * serializer. 71 * Implementations may choose to modify state of builder (to affect serializer being 72 * built), or even completely replace it (if they want to build different kind of 73 * serializer). Typically, however, passed-in builder is returned, possibly with 74 * some modifications. 75 */ updateBuilder(SerializationConfig config, BeanDescription beanDesc, BeanSerializerBuilder builder)76 public BeanSerializerBuilder updateBuilder(SerializationConfig config, 77 BeanDescription beanDesc, BeanSerializerBuilder builder) { 78 return builder; 79 } 80 81 /** 82 * Method called by {@link BeanSerializerFactory} after constructing default 83 * bean serializer instance with properties collected and ordered earlier. 84 * Implementations can modify or replace given serializer and return serializer 85 * to use. Note that although initial serializer being passed is of type 86 * {@link BeanSerializer}, modifiers may return serializers of other types; 87 * and this is why implementations must check for type before casting. 88 *<p> 89 * NOTE: since 2.2, gets called for serializer of those non-POJO types that 90 * do not go through any of more specific <code>modifyXxxSerializer</code> 91 * methods; mostly for JDK types like {@link java.util.Iterator} and such. 92 */ modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer)93 public JsonSerializer<?> modifySerializer(SerializationConfig config, 94 BeanDescription beanDesc, JsonSerializer<?> serializer) { 95 return serializer; 96 } 97 98 /* 99 /********************************************************** 100 /* Callback methods for other types (since 2.2) 101 /********************************************************** 102 */ 103 104 /** 105 * Method called by {@link DeserializerFactory} after it has constructed the 106 * standard serializer for given 107 * {@link ArrayType} 108 * to make it possible to either replace or augment this serializer with 109 * additional functionality. 110 * 111 * @param config Configuration in use 112 * @param valueType Type of the value serializer is used for. 113 * @param beanDesc Details of the type in question, to allow checking class annotations 114 * @param serializer Default serializer that would be used. 115 * 116 * @return Serializer to use; either <code>serializer</code> that was passed 117 * in, or an instance method constructed. 118 * 119 * @since 2.2 120 */ modifyArraySerializer(SerializationConfig config, ArrayType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)121 public JsonSerializer<?> modifyArraySerializer(SerializationConfig config, 122 ArrayType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 123 return serializer; 124 } 125 126 /** 127 * @since 2.2 128 */ modifyCollectionSerializer(SerializationConfig config, CollectionType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)129 public JsonSerializer<?> modifyCollectionSerializer(SerializationConfig config, 130 CollectionType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 131 return serializer; 132 } 133 134 /** 135 * @since 2.2 136 */ modifyCollectionLikeSerializer(SerializationConfig config, CollectionLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)137 public JsonSerializer<?> modifyCollectionLikeSerializer(SerializationConfig config, 138 CollectionLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 139 return serializer; 140 } 141 142 /** 143 * @since 2.2 144 */ modifyMapSerializer(SerializationConfig config, MapType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)145 public JsonSerializer<?> modifyMapSerializer(SerializationConfig config, 146 MapType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 147 return serializer; 148 } 149 150 /** 151 * @since 2.2 152 */ modifyMapLikeSerializer(SerializationConfig config, MapLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)153 public JsonSerializer<?> modifyMapLikeSerializer(SerializationConfig config, 154 MapLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 155 return serializer; 156 } 157 158 /** 159 * @since 2.2 160 */ modifyEnumSerializer(SerializationConfig config, JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)161 public JsonSerializer<?> modifyEnumSerializer(SerializationConfig config, 162 JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 163 return serializer; 164 } 165 166 /** 167 * Method called by {@link DeserializerFactory} after it has constructed the 168 * default key serializer to use for serializing {@link java.util.Map} keys of 169 * given type. 170 * This makes it possible to either replace or augment default serializer with 171 * additional functionality. 172 * 173 * @param config Configuration in use 174 * @param valueType Type of keys the serializer is used for. 175 * @param beanDesc Details of the type in question, to allow checking class annotations 176 * @param serializer Default serializer that would be used. 177 * 178 * @return Serializer to use; either <code>serializer</code> that was passed 179 * in, or an instance method constructed. 180 * 181 * @since 2.2 182 */ modifyKeySerializer(SerializationConfig config, JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer)183 public JsonSerializer<?> modifyKeySerializer(SerializationConfig config, 184 JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { 185 return serializer; 186 } 187 } 188