Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.*; |
| 5 | |
| 6 | import com.fasterxml.jackson.core.*; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 7 | |
| 8 | import com.fasterxml.jackson.databind.cfg.MapperConfig; |
Tatu Saloranta | d92c1ed | 2011-12-23 18:23:31 -0800 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.introspect.Annotated; |
Tatu Saloranta | 2f82344 | 2011-12-23 20:25:27 -0800 | [diff] [blame] | 11 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 12 | import com.fasterxml.jackson.databind.type.TypeFactory; |
| 13 | import com.fasterxml.jackson.databind.util.ArrayBuilders; |
| 14 | import com.fasterxml.jackson.databind.util.ObjectBuffer; |
| 15 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 16 | /** |
| 17 | * Context for deserialization process. Used to allow passing in configuration |
| 18 | * settings and reusable temporary objects (scrap arrays, containers). |
| 19 | */ |
| 20 | public abstract class DeserializationContext |
| 21 | { |
| 22 | protected final DeserializationConfig _config; |
| 23 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 24 | protected final int _featureFlags; |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame^] | 25 | |
| 26 | protected final Class<?> _view; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | /********************************************************** |
| 30 | /* Life-cycle |
| 31 | /********************************************************** |
| 32 | */ |
| 33 | |
| 34 | protected DeserializationContext(DeserializationConfig config) |
| 35 | { |
| 36 | _config = config; |
Tatu Saloranta | c733a7f | 2012-01-18 20:25:27 -0800 | [diff] [blame] | 37 | _featureFlags = config.getFeatureFlags(); |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame^] | 38 | _view = config.getActiveView(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | /********************************************************** |
| 43 | /* Configuration methods |
| 44 | /********************************************************** |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Method for accessing configuration setting object for |
| 49 | * currently active deserialization. |
| 50 | */ |
| 51 | public DeserializationConfig getConfig() { return _config; } |
| 52 | |
Tatu Saloranta | bb802d2 | 2012-01-23 22:33:09 -0800 | [diff] [blame] | 53 | public final AnnotationIntrospector getAnnotationIntrospector() { |
Tatu Saloranta | 78dddb9 | 2012-01-15 10:00:17 -0800 | [diff] [blame] | 54 | return _config.getAnnotationIntrospector(); |
| 55 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Convenience method for checking whether specified on/off |
| 59 | * feature is enabled |
| 60 | */ |
Tatu Saloranta | bb802d2 | 2012-01-23 22:33:09 -0800 | [diff] [blame] | 61 | public final boolean isEnabled(DeserializationConfig.Feature feat) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 62 | /* 03-Dec-2010, tatu: minor shortcut; since this is called quite often, |
| 63 | * let's use a local copy of feature settings: |
| 64 | */ |
| 65 | return (_featureFlags & feat.getMask()) != 0; |
| 66 | } |
| 67 | |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 68 | public final boolean isEnabled(MapperConfig.Feature feat) { |
| 69 | return _config.isEnabled(feat); |
| 70 | } |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame^] | 71 | |
| 72 | public final Class<?> getActiveView() { |
| 73 | return _view; |
| 74 | } |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 75 | |
| 76 | public final boolean canOverrideAccessModifiers() { |
| 77 | return _config.canOverrideAccessModifiers(); |
| 78 | } |
| 79 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Convenience method for accessing the default Base64 encoding |
| 82 | * used for decoding base64 encoded binary content. |
| 83 | * Same as calling: |
| 84 | *<pre> |
| 85 | * getConfig().getBase64Variant(); |
| 86 | *</pre> |
| 87 | */ |
| 88 | public Base64Variant getBase64Variant() { |
| 89 | return _config.getBase64Variant(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Accessor for getting access to the underlying JSON parser used |
| 94 | * for deserialization. |
| 95 | */ |
| 96 | public abstract JsonParser getParser(); |
| 97 | |
| 98 | public final JsonNodeFactory getNodeFactory() { |
| 99 | return _config.getNodeFactory(); |
| 100 | } |
| 101 | |
Tatu Saloranta | bb802d2 | 2012-01-23 22:33:09 -0800 | [diff] [blame] | 102 | public final JavaType constructType(Class<?> cls) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 103 | return _config.constructType(cls); |
| 104 | } |
| 105 | |
Tatu Saloranta | bb802d2 | 2012-01-23 22:33:09 -0800 | [diff] [blame] | 106 | public final TypeFactory getTypeFactory() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 107 | return _config.getTypeFactory(); |
| 108 | } |
| 109 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 110 | public abstract Object findInjectableValue(Object valueId, |
| 111 | BeanProperty forProperty, Object beanInstance); |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | /********************************************************** |
Tatu | 9e50422 | 2012-01-24 11:23:59 -0800 | [diff] [blame] | 115 | /* By-pass methods to DeserializerCache |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 116 | /********************************************************** |
| 117 | */ |
| 118 | |
| 119 | /** |
| 120 | * Convenience method, functionally same as: |
| 121 | *<pre> |
| 122 | * getDeserializerProvider().findValueDeserializer(getConfig(), propertyType, property); |
| 123 | *</pre> |
| 124 | */ |
| 125 | public abstract JsonDeserializer<Object> findValueDeserializer(JavaType propertyType, |
| 126 | BeanProperty property) |
| 127 | throws JsonMappingException; |
| 128 | |
| 129 | /** |
| 130 | * Convenience method, functionally same as: |
| 131 | *<pre> |
| 132 | * getDeserializerProvider().findTypedValueDeserializer(getConfig(), propertyType, property); |
| 133 | *</pre> |
| 134 | */ |
| 135 | public abstract JsonDeserializer<Object> findTypedValueDeserializer(JavaType type, |
| 136 | BeanProperty property) |
| 137 | throws JsonMappingException; |
| 138 | |
| 139 | /** |
| 140 | * Convenience method, functionally same as: |
| 141 | *<pre> |
| 142 | * getDeserializerProvider().findKeyDeserializer(getConfig(), propertyType, property); |
| 143 | *</pre> |
| 144 | */ |
| 145 | public abstract KeyDeserializer findKeyDeserializer(JavaType keyType, |
| 146 | BeanProperty property) |
| 147 | throws JsonMappingException; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | /********************************************************** |
| 151 | /* Extended API: handler instantiation |
| 152 | /********************************************************** |
| 153 | */ |
| 154 | |
| 155 | public abstract JsonDeserializer<Object> deserializerInstance(Annotated annotated, |
| 156 | BeanProperty property, Object deserDef) |
| 157 | throws JsonMappingException; |
| 158 | |
| 159 | public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated, |
| 160 | BeanProperty property, Object keyDeserClass) |
| 161 | throws JsonMappingException; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | /********************************************************** |
| 165 | /* Methods for accessing reusable/recyclable helper objects |
| 166 | /********************************************************** |
| 167 | */ |
| 168 | |
| 169 | /** |
| 170 | * Method that can be used to get access to a reusable ObjectBuffer, |
| 171 | * useful for efficiently constructing Object arrays and Lists. |
| 172 | * Note that leased buffers should be returned once deserializer |
| 173 | * is done, to allow for reuse during same round of deserialization. |
| 174 | */ |
| 175 | public abstract ObjectBuffer leaseObjectBuffer(); |
| 176 | |
| 177 | /** |
| 178 | * Method to call to return object buffer previously leased with |
| 179 | * {@link #leaseObjectBuffer}. |
| 180 | * |
| 181 | * @param buf Returned object buffer |
| 182 | */ |
| 183 | public abstract void returnObjectBuffer(ObjectBuffer buf); |
| 184 | |
| 185 | /** |
| 186 | * Method for accessing object useful for building arrays of |
| 187 | * primitive types (such as int[]). |
| 188 | */ |
| 189 | public abstract ArrayBuilders getArrayBuilders(); |
| 190 | |
| 191 | /* |
| 192 | /********************************************************** |
| 193 | /* Parsing methods that may use reusable/-cyclable objects |
| 194 | /********************************************************** |
| 195 | */ |
| 196 | |
| 197 | /** |
| 198 | * Convenience method for parsing a Date from given String, using |
| 199 | * currently configured date format (accessed using |
| 200 | * {@link DeserializationConfig#getDateFormat()}). |
| 201 | *<p> |
| 202 | * Implementation will handle thread-safety issues related to |
| 203 | * date formats such that first time this method is called, |
| 204 | * date format is cloned, and cloned instance will be retained |
| 205 | * for use during this deserialization round. |
| 206 | */ |
| 207 | public abstract java.util.Date parseDate(String dateStr) |
| 208 | throws IllegalArgumentException; |
| 209 | |
| 210 | /** |
| 211 | * Convenience method for constructing Calendar instance set |
| 212 | * to specified time, to be modified and used by caller. |
| 213 | */ |
| 214 | public abstract Calendar constructCalendar(Date d); |
| 215 | |
| 216 | /* |
| 217 | /********************************************************** |
| 218 | /* Methods for problem handling, reporting |
| 219 | /********************************************************** |
| 220 | */ |
| 221 | |
| 222 | /** |
| 223 | * Method deserializers can call to inform configured {@link DeserializationProblemHandler}s |
| 224 | * of an unrecognized property. |
| 225 | * |
| 226 | * @return True if there was a configured problem handler that was able to handle the |
Tatu Saloranta | 0e3b383 | 2012-01-22 22:08:20 -0800 | [diff] [blame] | 227 | * problem |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 228 | */ |
| 229 | public abstract boolean handleUnknownProperty(JsonParser jp, JsonDeserializer<?> deser, Object instanceOrClass, String propName) |
| 230 | throws IOException, JsonProcessingException; |
| 231 | |
| 232 | /** |
| 233 | * Helper method for constructing generic mapping exception for specified type |
| 234 | */ |
| 235 | public abstract JsonMappingException mappingException(Class<?> targetClass); |
| 236 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 237 | public abstract JsonMappingException mappingException(Class<?> targetClass, JsonToken t); |
| 238 | |
| 239 | /** |
| 240 | * Helper method for constructing generic mapping exception with specified |
| 241 | * message and current location information |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 242 | */ |
| 243 | public JsonMappingException mappingException(String message) |
| 244 | { |
| 245 | return JsonMappingException.from(getParser(), message); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Helper method for constructing instantiation exception for specified type, |
| 250 | * to indicate problem with physically constructing instance of |
| 251 | * specified class (missing constructor, exception from constructor) |
| 252 | */ |
| 253 | public abstract JsonMappingException instantiationException(Class<?> instClass, Throwable t); |
| 254 | |
| 255 | public abstract JsonMappingException instantiationException(Class<?> instClass, String msg); |
| 256 | |
| 257 | /** |
| 258 | * Helper method for constructing exception to indicate that input JSON |
| 259 | * String was not in recognized format for deserializing into given type. |
| 260 | */ |
| 261 | public abstract JsonMappingException weirdStringException(Class<?> instClass, String msg); |
| 262 | |
| 263 | /** |
| 264 | * Helper method for constructing exception to indicate that input JSON |
| 265 | * Number was not suitable for deserializing into given type. |
| 266 | */ |
| 267 | public abstract JsonMappingException weirdNumberException(Class<?> instClass, String msg); |
| 268 | |
| 269 | /** |
| 270 | * Helper method for constructing exception to indicate that given JSON |
| 271 | * Object field name was not in format to be able to deserialize specified |
| 272 | * key type. |
| 273 | */ |
| 274 | public abstract JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg); |
| 275 | |
| 276 | /** |
| 277 | * Helper method for indicating that the current token was expected to be another |
| 278 | * token. |
| 279 | */ |
| 280 | public abstract JsonMappingException wrongTokenException(JsonParser jp, JsonToken expToken, String msg); |
| 281 | |
| 282 | /** |
| 283 | * Helper method for constructing exception to indicate that JSON Object |
| 284 | * field name did not map to a known property of type being |
| 285 | * deserialized. |
| 286 | * |
| 287 | * @param instanceOrClass Either value being populated (if one has been |
| 288 | * instantiated), or Class that indicates type that would be (or |
| 289 | * have been) instantiated |
| 290 | */ |
| 291 | public abstract JsonMappingException unknownFieldException(Object instanceOrClass, String fieldName); |
| 292 | |
| 293 | /** |
| 294 | * Helper method for constructing exception to indicate that given |
| 295 | * type id (parsed from JSON) could not be converted to a Java type. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 296 | */ |
| 297 | public abstract JsonMappingException unknownTypeException(JavaType baseType, String id); |
| 298 | } |