Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind; |
| 2 | |
| 3 | import java.io.IOException; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 4 | import java.text.DateFormat; |
| 5 | import java.text.ParseException; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 6 | import java.util.*; |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 7 | import java.util.concurrent.atomic.AtomicReference; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 8 | |
Tatu Saloranta | d71dffa | 2012-02-07 21:05:21 -0800 | [diff] [blame] | 9 | import com.fasterxml.jackson.annotation.ObjectIdGenerator; |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 10 | import com.fasterxml.jackson.annotation.ObjectIdResolver; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 11 | import com.fasterxml.jackson.core.*; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 12 | import com.fasterxml.jackson.databind.cfg.ContextAttributes; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 13 | import com.fasterxml.jackson.databind.deser.*; |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 14 | import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 15 | import com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer; |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 16 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.introspect.Annotated; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
Tatu Saloranta | 2f82344 | 2011-12-23 20:25:27 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.type.TypeFactory; |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.util.*; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 23 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 24 | /** |
Tatu Saloranta | 97f8482 | 2012-01-29 21:38:40 -0800 | [diff] [blame] | 25 | * Context for the process of deserialization a single root-level value. |
| 26 | * Used to allow passing in configuration settings and reusable temporary |
| 27 | * objects (scrap arrays, containers). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 28 | *<p> |
| 29 | * Instance life-cycle is such that an partially configured "blueprint" object |
| 30 | * is registered with {@link ObjectMapper} (and {@link ObjectReader}, |
| 31 | * and when an actual instance is needed for deserialization, |
| 32 | * a fully configured instance will |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 33 | * be created using a method in excented API of sub-class |
| 34 | * ({@link com.fasterxml.jackson.databind.deser.DefaultDeserializationContext#createInstance}). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 35 | * Each instance is guaranteed to only be used from single-threaded context; |
| 36 | * instances may be reused iff no configuration has changed. |
| 37 | *<p> |
| 38 | * Defined as abstract class so that implementations must define methods |
| 39 | * for reconfiguring blueprints and creating instances. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 40 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 41 | public abstract class DeserializationContext |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 42 | extends DatabindContext |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 43 | implements java.io.Serializable |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 44 | { |
Tatu Saloranta | 953eab2 | 2014-05-15 21:35:28 -0700 | [diff] [blame] | 45 | private static final long serialVersionUID = -4290063686213707727L; |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 46 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Let's limit length of error messages, for cases where underlying data |
| 49 | * may be very large -- no point in spamming logs with megs of meaningless |
| 50 | * data. |
| 51 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 52 | private final static int MAX_ERROR_STR_LEN = 500; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 53 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 54 | /* |
| 55 | /********************************************************** |
| 56 | /* Configuration, immutable |
| 57 | /********************************************************** |
| 58 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 59 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Object that handle details of {@link JsonDeserializer} caching. |
| 62 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 63 | protected final DeserializerCache _cache; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 64 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 65 | /* |
| 66 | /********************************************************** |
| 67 | /* Configuration, changeable via fluent factories |
| 68 | /********************************************************** |
| 69 | */ |
| 70 | |
| 71 | /** |
| 72 | * Read-only factory instance; exposed to let |
| 73 | * owners (<code>ObjectMapper</code>, <code>ObjectReader</code>) |
| 74 | * access it. |
| 75 | */ |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 76 | protected final DeserializerFactory _factory; |
| 77 | |
| 78 | /* |
| 79 | /********************************************************** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 80 | /* Configuration that gets set for instances (not blueprints) |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 81 | /* (partly denormalized for performance) |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 82 | /********************************************************** |
| 83 | */ |
| 84 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Generic deserialization processing configuration |
| 87 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 88 | protected final DeserializationConfig _config; |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Bitmap of {@link DeserializationFeature}s that are enabled |
| 92 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 93 | protected final int _featureFlags; |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 94 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Currently active view, if any. |
| 97 | */ |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 98 | protected final Class<?> _view; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Currently active parser used for deserialization. |
| 102 | * May be different from the outermost parser |
| 103 | * when content is buffered. |
| 104 | */ |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 105 | protected transient JsonParser _parser; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 106 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Object used for resolving references to injectable |
| 109 | * values. |
| 110 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 111 | protected final InjectableValues _injectableValues; |
| 112 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 113 | /* |
| 114 | /********************************************************** |
| 115 | /* Per-operation reusable helper objects (not for blueprints) |
| 116 | /********************************************************** |
| 117 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 118 | |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 119 | protected transient ArrayBuilders _arrayBuilders; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 120 | |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 121 | protected transient ObjectBuffer _objectBuffer; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 122 | |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 123 | protected transient DateFormat _dateFormat; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Lazily-constructed holder for per-call attributes. |
| 127 | * |
| 128 | * @since 2.3 |
| 129 | */ |
| 130 | protected transient ContextAttributes _attributes; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | /********************************************************** |
| 134 | /* Life-cycle |
| 135 | /********************************************************** |
| 136 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 137 | |
| 138 | protected DeserializationContext(DeserializerFactory df) { |
| 139 | this(df, null); |
| 140 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 141 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 142 | protected DeserializationContext(DeserializerFactory df, |
| 143 | DeserializerCache cache) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 144 | { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 145 | if (df == null) { |
| 146 | throw new IllegalArgumentException("Can not pass null DeserializerFactory"); |
| 147 | } |
| 148 | _factory = df; |
| 149 | _cache = (cache == null) ? new DeserializerCache() : cache; |
| 150 | |
| 151 | _featureFlags = 0; |
| 152 | _config = null; |
| 153 | _injectableValues = null; |
| 154 | _view = null; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 155 | _attributes = null; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | protected DeserializationContext(DeserializationContext src, |
| 159 | DeserializerFactory factory) |
| 160 | { |
| 161 | _cache = src._cache; |
| 162 | _factory = factory; |
| 163 | |
| 164 | _config = src._config; |
| 165 | _featureFlags = src._featureFlags; |
| 166 | _view = src._view; |
| 167 | _parser = src._parser; |
| 168 | _injectableValues = src._injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 169 | _attributes = src._attributes; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 170 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Constructor used for creating actual per-call instances. |
| 174 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 175 | protected DeserializationContext(DeserializationContext src, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 176 | DeserializationConfig config, JsonParser p, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 177 | InjectableValues injectableValues) |
| 178 | { |
| 179 | _cache = src._cache; |
| 180 | _factory = src._factory; |
| 181 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 182 | _config = config; |
Tatu Saloranta | e3ae58e | 2012-01-28 23:08:16 -0800 | [diff] [blame] | 183 | _featureFlags = config.getDeserializationFeatures(); |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 184 | _view = config.getActiveView(); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 185 | _parser = p; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 186 | _injectableValues = injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 187 | _attributes = config.getAttributes(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 188 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 189 | |
Tatu Saloranta | 23328aa | 2014-10-28 22:38:15 -0700 | [diff] [blame^] | 190 | /** |
| 191 | * Copy-constructor for use with <code>copy()</code> by {@link ObjectMapper#copy()} |
| 192 | */ |
| 193 | protected DeserializationContext(DeserializationContext src) { |
| 194 | _cache = new DeserializerCache(); |
| 195 | _factory = src._factory; |
| 196 | |
| 197 | _config = src._config; |
| 198 | _featureFlags = src._featureFlags; |
| 199 | _view = src._view; |
| 200 | _injectableValues = null; |
| 201 | } |
| 202 | |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 203 | /* |
| 204 | /********************************************************** |
| 205 | /* DatabindContext implementation |
| 206 | /********************************************************** |
| 207 | */ |
| 208 | |
| 209 | @Override |
| 210 | public DeserializationConfig getConfig() { return _config; } |
| 211 | |
| 212 | @Override |
| 213 | public final Class<?> getActiveView() { return _view; } |
| 214 | |
| 215 | @Override |
| 216 | public final AnnotationIntrospector getAnnotationIntrospector() { |
| 217 | return _config.getAnnotationIntrospector(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public final TypeFactory getTypeFactory() { |
| 222 | return _config.getTypeFactory(); |
| 223 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | /********************************************************** |
| 227 | /* Generic attributes (2.3+) |
| 228 | /********************************************************** |
| 229 | */ |
| 230 | |
| 231 | @Override |
| 232 | public Object getAttribute(Object key) { |
| 233 | return _attributes.getAttribute(key); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public DeserializationContext setAttribute(Object key, Object value) |
| 238 | { |
| 239 | _attributes = _attributes.withPerCallAttribute(key, value); |
| 240 | return this; |
| 241 | } |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 242 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 243 | /* |
| 244 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 245 | /* Public API, accessors |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 246 | /********************************************************** |
| 247 | */ |
| 248 | |
| 249 | /** |
Dmitry Katsubo | e70c66f | 2012-09-03 16:00:30 +0200 | [diff] [blame] | 250 | * Method for getting current {@link DeserializerFactory}. |
| 251 | */ |
| 252 | public DeserializerFactory getFactory() { |
| 253 | return _factory; |
| 254 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 255 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Convenience method for checking whether specified on/off |
| 258 | * feature is enabled |
| 259 | */ |
Tatu | 9610aff | 2012-02-02 11:30:08 -0800 | [diff] [blame] | 260 | public final boolean isEnabled(DeserializationFeature feat) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 261 | /* 03-Dec-2010, tatu: minor shortcut; since this is called quite often, |
| 262 | * let's use a local copy of feature settings: |
| 263 | */ |
| 264 | return (_featureFlags & feat.getMask()) != 0; |
| 265 | } |
| 266 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 267 | /** |
Tatu Saloranta | 0ac36ba | 2013-08-21 18:08:51 -0700 | [diff] [blame] | 268 | * "Bulk" access method for checking that all features specified by |
| 269 | * mask are enabled. |
| 270 | * |
| 271 | * @since 2.3 |
| 272 | */ |
| 273 | public final boolean hasDeserializationFeatures(int featureMask) { |
| 274 | return _config.hasDeserializationFeatures(featureMask); |
| 275 | } |
| 276 | |
| 277 | /** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 278 | * Method for accessing the currently active parser. |
| 279 | * May be different from the outermost parser |
| 280 | * when content is buffered. |
| 281 | *<p> |
| 282 | * Use of this method is discouraged: if code has direct access |
| 283 | * to the active parser, that should be used instead. |
| 284 | */ |
| 285 | public final JsonParser getParser() { return _parser; } |
| 286 | |
| 287 | public final Object findInjectableValue(Object valueId, |
| 288 | BeanProperty forProperty, Object beanInstance) |
| 289 | { |
| 290 | if (_injectableValues == null) { |
| 291 | throw new IllegalStateException("No 'injectableValues' configured, can not inject value with id ["+valueId+"]"); |
| 292 | } |
| 293 | return _injectableValues.findInjectableValue(valueId, this, forProperty, beanInstance); |
| 294 | } |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 295 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 296 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 297 | * Convenience method for accessing the default Base64 encoding |
| 298 | * used for decoding base64 encoded binary content. |
| 299 | * Same as calling: |
| 300 | *<pre> |
| 301 | * getConfig().getBase64Variant(); |
| 302 | *</pre> |
| 303 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 304 | public final Base64Variant getBase64Variant() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 305 | return _config.getBase64Variant(); |
| 306 | } |
| 307 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Convenience method, functionally equivalent to: |
| 310 | *<pre> |
| 311 | * getConfig().getNodeFactory(); |
| 312 | * </pre> |
| 313 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 314 | public final JsonNodeFactory getNodeFactory() { |
| 315 | return _config.getNodeFactory(); |
| 316 | } |
| 317 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 318 | /** |
Tatu Saloranta | e6faa45 | 2012-02-23 19:44:42 -0800 | [diff] [blame] | 319 | * Method for accessing default Locale to use: convenience method for |
| 320 | *<pre> |
| 321 | * getConfig().getLocale(); |
| 322 | *</pre> |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 323 | */ |
| 324 | public Locale getLocale() { |
Tatu Saloranta | e6faa45 | 2012-02-23 19:44:42 -0800 | [diff] [blame] | 325 | return _config.getLocale(); |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Tatu Saloranta | e6faa45 | 2012-02-23 19:44:42 -0800 | [diff] [blame] | 328 | /** |
| 329 | * Method for accessing default TimeZone to use: convenience method for |
| 330 | *<pre> |
| 331 | * getConfig().getTimeZone(); |
| 332 | *</pre> |
| 333 | */ |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 334 | public TimeZone getTimeZone() { |
Tatu Saloranta | e6faa45 | 2012-02-23 19:44:42 -0800 | [diff] [blame] | 335 | return _config.getTimeZone(); |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 336 | } |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 337 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 338 | /* |
| 339 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 340 | /* Public API, pass-through to DeserializerCache |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 341 | /********************************************************** |
| 342 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 343 | |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 344 | @Deprecated // since 2.3, use overloaded variant |
| 345 | public boolean hasValueDeserializerFor(JavaType type) { |
| 346 | return hasValueDeserializerFor(type, null); |
| 347 | } |
| 348 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 349 | /** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 350 | * Method for checking whether we could find a deserializer |
| 351 | * for given type. |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 352 | * |
| 353 | * @param type |
| 354 | * @since 2.3 |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 355 | */ |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 356 | public boolean hasValueDeserializerFor(JavaType type, AtomicReference<Throwable> cause) { |
| 357 | try { |
| 358 | return _cache.hasValueDeserializerFor(this, _factory, type); |
| 359 | } catch (JsonMappingException e) { |
| 360 | if (cause != null) { |
| 361 | cause.set(e); |
| 362 | } |
| 363 | } catch (RuntimeException e) { |
| 364 | if (cause == null) { // earlier behavior |
| 365 | throw e; |
| 366 | } |
| 367 | cause.set(e); |
| 368 | } |
| 369 | return false; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 372 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 373 | * Method for finding a value deserializer, and creating a contextual |
| 374 | * version if necessary, for value reached via specified property. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 375 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 376 | @SuppressWarnings("unchecked") |
| 377 | public final JsonDeserializer<Object> findContextualValueDeserializer(JavaType type, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 378 | BeanProperty prop) throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 379 | { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 380 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 381 | if (deser != null) { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 382 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, prop); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 383 | } |
| 384 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 385 | } |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 386 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 387 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 388 | * Method for finding a deserializer for root-level value. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 389 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 390 | @SuppressWarnings("unchecked") |
Tatu Saloranta | 9b9d043 | 2012-01-30 09:20:26 -0800 | [diff] [blame] | 391 | public final JsonDeserializer<Object> findRootValueDeserializer(JavaType type) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 392 | throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 393 | { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 394 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, |
| 395 | _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 396 | if (deser == null) { // can this occur? |
| 397 | return null; |
| 398 | } |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 399 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, null); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 400 | TypeDeserializer typeDeser = _factory.findTypeDeserializer(_config, type); |
| 401 | if (typeDeser != null) { |
| 402 | // important: contextualize to indicate this is for root value |
| 403 | typeDeser = typeDeser.forProperty(null); |
| 404 | return new TypeWrappedDeserializer(typeDeser, deser); |
| 405 | } |
| 406 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 407 | } |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Convenience method, functionally same as: |
| 411 | *<pre> |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 412 | * getDeserializerProvider().findKeyDeserializer(getConfig(), prop.getType(), prop); |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 413 | *</pre> |
| 414 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 415 | public final KeyDeserializer findKeyDeserializer(JavaType keyType, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 416 | BeanProperty prop) throws JsonMappingException { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 417 | KeyDeserializer kd = _cache.findKeyDeserializer(this, |
| 418 | _factory, keyType); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 419 | // Second: contextualize? |
| 420 | if (kd instanceof ContextualKeyDeserializer) { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 421 | kd = ((ContextualKeyDeserializer) kd).createContextual(this, prop); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 422 | } |
| 423 | return kd; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 426 | /* |
| 427 | /********************************************************** |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 428 | /* Public API, ObjectId handling |
| 429 | /********************************************************** |
| 430 | */ |
| 431 | |
| 432 | /** |
| 433 | * Method called to find and return entry corresponding to given |
| 434 | * Object Id: will add an entry if necessary, and never returns null |
| 435 | */ |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 436 | public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> generator, ObjectIdResolver resolver); |
| 437 | |
Tatu Saloranta | 301ea2d | 2014-03-16 22:26:12 -0700 | [diff] [blame] | 438 | @Deprecated // since 2.4 |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 439 | public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> generator); |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 440 | |
Pascal GĂ©linas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 441 | /** |
| 442 | * Method called to ensure that every object id encounter during processing |
| 443 | * are resolved. |
| 444 | * |
| 445 | * @throws UnresolvedForwardReference |
| 446 | */ |
| 447 | public abstract void checkUnresolvedObjectId() |
| 448 | throws UnresolvedForwardReference; |
| 449 | |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 450 | /* |
| 451 | /********************************************************** |
| 452 | /* Public API, type handling |
| 453 | /********************************************************** |
| 454 | */ |
| 455 | |
| 456 | /** |
| 457 | * Convenience method, functionally equivalent to: |
| 458 | *<pre> |
| 459 | * getConfig().constructType(cls); |
| 460 | * </pre> |
| 461 | */ |
| 462 | public final JavaType constructType(Class<?> cls) { |
| 463 | return _config.constructType(cls); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Helper method to use for locating Class for given name. Should be used |
| 468 | * instead of basic <code>Class.forName(className);</code> as it can |
| 469 | * try using contextual class loader, or use platform-specific workarounds |
| 470 | * (like on Android, GAE). |
| 471 | */ |
| 472 | public Class<?> findClass(String className) throws ClassNotFoundException |
| 473 | { |
| 474 | // By default, delegate to ClassUtil: can be overridden with custom handling |
| 475 | return ClassUtil.findClass(className); |
| 476 | } |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 477 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 478 | /* |
| 479 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 480 | /* Public API, helper object recycling |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 481 | /********************************************************** |
| 482 | */ |
| 483 | |
| 484 | /** |
| 485 | * Method that can be used to get access to a reusable ObjectBuffer, |
| 486 | * useful for efficiently constructing Object arrays and Lists. |
| 487 | * Note that leased buffers should be returned once deserializer |
| 488 | * is done, to allow for reuse during same round of deserialization. |
| 489 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 490 | public final ObjectBuffer leaseObjectBuffer() |
| 491 | { |
| 492 | ObjectBuffer buf = _objectBuffer; |
| 493 | if (buf == null) { |
| 494 | buf = new ObjectBuffer(); |
| 495 | } else { |
| 496 | _objectBuffer = null; |
| 497 | } |
| 498 | return buf; |
| 499 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * Method to call to return object buffer previously leased with |
| 503 | * {@link #leaseObjectBuffer}. |
| 504 | * |
| 505 | * @param buf Returned object buffer |
| 506 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 507 | public final void returnObjectBuffer(ObjectBuffer buf) |
| 508 | { |
| 509 | /* Already have a reusable buffer? Let's retain bigger one |
| 510 | * (or if equal, favor newer one, shorter life-cycle) |
| 511 | */ |
| 512 | if (_objectBuffer == null |
| 513 | || buf.initialCapacity() >= _objectBuffer.initialCapacity()) { |
| 514 | _objectBuffer = buf; |
| 515 | } |
| 516 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 517 | |
| 518 | /** |
| 519 | * Method for accessing object useful for building arrays of |
| 520 | * primitive types (such as int[]). |
| 521 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 522 | public final ArrayBuilders getArrayBuilders() |
| 523 | { |
| 524 | if (_arrayBuilders == null) { |
| 525 | _arrayBuilders = new ArrayBuilders(); |
| 526 | } |
| 527 | return _arrayBuilders; |
| 528 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | /********************************************************** |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 532 | /* Extended API: handler instantiation |
| 533 | /********************************************************** |
| 534 | */ |
| 535 | |
| 536 | public abstract JsonDeserializer<Object> deserializerInstance(Annotated annotated, |
| 537 | Object deserDef) |
| 538 | throws JsonMappingException; |
| 539 | |
| 540 | public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated, |
| 541 | Object deserDef) |
| 542 | throws JsonMappingException; |
| 543 | |
| 544 | /* |
| 545 | /********************************************************** |
| 546 | /* Extended API: resolving contextual deserializers; called |
| 547 | /* by structured deserializers for their value/component |
| 548 | /* deserializers |
| 549 | /********************************************************** |
| 550 | */ |
| 551 | |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 552 | /** |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 553 | * Method called for primary property deserializers (ones |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 554 | * directly created to deserialize values of a POJO property), |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 555 | * to handle details of resolving |
| 556 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 557 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 558 | * @param prop Property for which the given primary deserializer is used; never null. |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 559 | * |
| 560 | * @since 2.3 |
| 561 | */ |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 562 | public JsonDeserializer<?> handlePrimaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 563 | BeanProperty prop) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 564 | throws JsonMappingException |
| 565 | { |
Tatu Saloranta | 47ac6bd | 2013-09-05 20:55:00 -0700 | [diff] [blame] | 566 | if (deser != null) { |
| 567 | if (deser instanceof ContextualDeserializer) { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 568 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
Tatu Saloranta | 47ac6bd | 2013-09-05 20:55:00 -0700 | [diff] [blame] | 569 | } |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 570 | } |
| 571 | return deser; |
| 572 | } |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 573 | |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 574 | /** |
| 575 | * Method called for secondary property deserializers (ones |
| 576 | * NOT directly created to deal with an annotatable POJO property, |
| 577 | * but instead created as a component -- such as value deserializers |
| 578 | * for structured types, or deserializers for root values) |
| 579 | * to handle details of resolving |
| 580 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 581 | * Given that these deserializers are not directly related to given property |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 582 | * (or, in case of root value property, to any property), annotations |
| 583 | * accessible may or may not be relevant. |
| 584 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 585 | * @param prop Property for which deserializer is used, if any; null |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 586 | * when deserializing root values |
| 587 | * |
| 588 | * @since 2.3 |
| 589 | */ |
| 590 | public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 591 | BeanProperty prop) |
| 592 | throws JsonMappingException { |
| 593 | if (deser != null && (deser instanceof ContextualDeserializer)) { |
| 594 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 595 | } |
| 596 | return deser; |
| 597 | } |
| 598 | |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 599 | /* |
| 600 | /********************************************************** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 601 | /* Parsing methods that may use reusable/-cyclable objects |
| 602 | /********************************************************** |
| 603 | */ |
| 604 | |
| 605 | /** |
| 606 | * Convenience method for parsing a Date from given String, using |
| 607 | * currently configured date format (accessed using |
| 608 | * {@link DeserializationConfig#getDateFormat()}). |
| 609 | *<p> |
| 610 | * Implementation will handle thread-safety issues related to |
| 611 | * date formats such that first time this method is called, |
| 612 | * date format is cloned, and cloned instance will be retained |
| 613 | * for use during this deserialization round. |
| 614 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 615 | public Date parseDate(String dateStr) throws IllegalArgumentException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 616 | { |
| 617 | try { |
Tatu Saloranta | ef00ac6 | 2013-11-03 09:56:39 -0800 | [diff] [blame] | 618 | DateFormat df = getDateFormat(); |
| 619 | return df.parse(dateStr); |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 620 | } catch (ParseException e) { |
| 621 | throw new IllegalArgumentException("Failed to parse Date value '"+dateStr+"': "+e.getMessage()); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 624 | |
| 625 | /** |
| 626 | * Convenience method for constructing Calendar instance set |
| 627 | * to specified time, to be modified and used by caller. |
| 628 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 629 | public Calendar constructCalendar(Date d) { |
| 630 | // 08-Jan-2008, tatu: not optimal, but should work for the most part; let's revise as needed. |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 631 | Calendar c = Calendar.getInstance(getTimeZone()); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 632 | c.setTime(d); |
| 633 | return c; |
| 634 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | /********************************************************** |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 638 | /* Convenience methods for reading parsed values |
| 639 | /********************************************************** |
| 640 | */ |
| 641 | |
| 642 | /** |
| 643 | * Convenience method that may be used by composite or container deserializers, |
| 644 | * for reading one-off values contained (for sequences, it is more efficient |
| 645 | * to actually fetch deserializer once for the whole collection). |
| 646 | *<p> |
| 647 | * NOTE: when deserializing values of properties contained in composite types, |
| 648 | * rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)}; |
| 649 | * this method does not allow use of contextual annotations. |
| 650 | * |
| 651 | * @since 2.4 |
| 652 | */ |
| 653 | public <T> T readValue(JsonParser p, Class<T> type) throws IOException { |
| 654 | return readValue(p, getTypeFactory().constructType(type)); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * @since 2.4 |
| 659 | */ |
| 660 | @SuppressWarnings("unchecked") |
| 661 | public <T> T readValue(JsonParser p, JavaType type) throws IOException { |
| 662 | JsonDeserializer<Object> deser = findRootValueDeserializer(type); |
| 663 | if (deser == null) { |
| 664 | } |
| 665 | return (T) deser.deserialize(p, this); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Convenience method that may be used by composite or container deserializers, |
| 670 | * for reading one-off values for the composite type, taking into account |
| 671 | * annotations that the property (passed to this method -- usually property that |
| 672 | * has custom serializer that called this method) has. |
| 673 | * |
| 674 | * @since 2.4 |
| 675 | */ |
| 676 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, Class<T> type) throws IOException { |
| 677 | return readPropertyValue(p, prop, getTypeFactory().constructType(type)); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * @since 2.4 |
| 682 | */ |
| 683 | @SuppressWarnings("unchecked") |
| 684 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException { |
| 685 | JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop); |
| 686 | if (deser == null) { |
| 687 | |
| 688 | } |
| 689 | return (T) deser.deserialize(p, this); |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | /********************************************************** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 694 | /* Methods for problem handling, reporting |
| 695 | /********************************************************** |
| 696 | */ |
| 697 | |
| 698 | /** |
| 699 | * Method deserializers can call to inform configured {@link DeserializationProblemHandler}s |
| 700 | * of an unrecognized property. |
| 701 | * |
| 702 | * @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] | 703 | * problem |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 704 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 705 | /** |
| 706 | * Method deserializers can call to inform configured {@link DeserializationProblemHandler}s |
| 707 | * of an unrecognized property. |
| 708 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 709 | public boolean handleUnknownProperty(JsonParser p, JsonDeserializer<?> deser, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 710 | Object instanceOrClass, String propName) |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 711 | throws IOException, JsonProcessingException |
| 712 | { |
| 713 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 714 | if (h != null) { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 715 | while (h != null) { |
| 716 | // Can bail out if it's handled |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 717 | if (h.value().handleUnknownProperty(this, p, deser, instanceOrClass, propName)) { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 718 | return true; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 719 | } |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 720 | h = h.next(); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | return false; |
| 724 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 725 | |
| 726 | /** |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 727 | * Helper method for reporting a problem with unhandled unknown exception |
| 728 | * |
| 729 | * @param instanceOrClass Either value being populated (if one has been |
| 730 | * instantiated), or Class that indicates type that would be (or |
| 731 | * have been) instantiated |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 732 | * @param deser Deserializer that had the problem, if called by deserializer |
| 733 | * (or on behalf of one) |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 734 | */ |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 735 | public void reportUnknownProperty(Object instanceOrClass, String fieldName, |
| 736 | JsonDeserializer<?> deser) |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 737 | throws JsonMappingException |
| 738 | { |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 739 | if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { |
| 740 | return; |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 741 | } |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 742 | // Do we know properties that are expected instead? |
| 743 | Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); |
| 744 | throw UnrecognizedPropertyException.from(_parser, |
| 745 | instanceOrClass, fieldName, propIds); |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* |
| 749 | /********************************************************** |
| 750 | /* Methods for constructing exceptions |
| 751 | /********************************************************** |
| 752 | */ |
| 753 | |
| 754 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 755 | * Helper method for constructing generic mapping exception for specified type |
| 756 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 757 | public JsonMappingException mappingException(Class<?> targetClass) { |
| 758 | return mappingException(targetClass, _parser.getCurrentToken()); |
| 759 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 760 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 761 | public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) { |
| 762 | return JsonMappingException.from(_parser, "Can not deserialize instance of "+_calcName(targetClass)+" out of "+token+" token"); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 763 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 764 | |
| 765 | /** |
| 766 | * Helper method for constructing generic mapping exception with specified |
| 767 | * message and current location information |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 768 | */ |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 769 | public JsonMappingException mappingException(String message) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 770 | return JsonMappingException.from(getParser(), message); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Helper method for constructing instantiation exception for specified type, |
| 775 | * to indicate problem with physically constructing instance of |
| 776 | * specified class (missing constructor, exception from constructor) |
| 777 | */ |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 778 | public JsonMappingException instantiationException(Class<?> instClass, Throwable t) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 779 | return JsonMappingException.from(_parser, |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 780 | "Can not construct instance of "+instClass.getName()+", problem: "+t.getMessage(), t); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 781 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 782 | |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 783 | public JsonMappingException instantiationException(Class<?> instClass, String msg) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 784 | return JsonMappingException.from(_parser, "Can not construct instance of "+instClass.getName()+", problem: "+msg); |
| 785 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 786 | |
| 787 | /** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 788 | * Method that will construct an exception suitable for throwing when |
| 789 | * some String values are acceptable, but the one encountered is not. |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 790 | * |
| 791 | * |
| 792 | * @deprecated Since 2.1 should use variant that takes value |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 793 | */ |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 794 | @Deprecated |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 795 | public JsonMappingException weirdStringException(Class<?> instClass, String msg) { |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 796 | return weirdStringException(null, instClass, msg); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Method that will construct an exception suitable for throwing when |
| 801 | * some String values are acceptable, but the one encountered is not. |
| 802 | * |
| 803 | * @param value String value from input being deserialized |
| 804 | * @param instClass Type that String should be deserialized into |
| 805 | * @param msg Message that describes specific problem |
| 806 | * |
| 807 | * @since 2.1 |
| 808 | */ |
| 809 | public JsonMappingException weirdStringException(String value, Class<?> instClass, String msg) { |
| 810 | return InvalidFormatException.from(_parser, |
| 811 | "Can not construct instance of "+instClass.getName()+" from String value '"+_valueDesc()+"': "+msg, |
| 812 | value, instClass); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 813 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 814 | |
| 815 | /** |
| 816 | * Helper method for constructing exception to indicate that input JSON |
| 817 | * Number was not suitable for deserializing into given type. |
| 818 | */ |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 819 | @Deprecated |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 820 | public JsonMappingException weirdNumberException(Class<?> instClass, String msg) { |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 821 | return weirdStringException(null, instClass, msg); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 822 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 823 | |
| 824 | /** |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 825 | * Helper method for constructing exception to indicate that input JSON |
| 826 | * Number was not suitable for deserializing into given target type. |
| 827 | */ |
| 828 | public JsonMappingException weirdNumberException(Number value, Class<?> instClass, String msg) { |
| 829 | return InvalidFormatException.from(_parser, |
| 830 | "Can not construct instance of "+instClass.getName()+" from number value ("+_valueDesc()+"): "+msg, |
| 831 | null, instClass); |
| 832 | } |
| 833 | |
| 834 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 835 | * Helper method for constructing exception to indicate that given JSON |
| 836 | * Object field name was not in format to be able to deserialize specified |
| 837 | * key type. |
| 838 | */ |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 839 | public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg) { |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 840 | return InvalidFormatException.from(_parser, |
| 841 | "Can not construct Map key of type "+keyClass.getName()+" from String \""+_desc(keyValue)+"\": "+msg, |
| 842 | keyValue, keyClass); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 843 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 844 | |
| 845 | /** |
| 846 | * Helper method for indicating that the current token was expected to be another |
| 847 | * token. |
| 848 | */ |
Tatu Saloranta | 953eab2 | 2014-05-15 21:35:28 -0700 | [diff] [blame] | 849 | public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken, String msg0) { |
| 850 | String msg = "Unexpected token ("+p.getCurrentToken()+"), expected "+expToken; |
| 851 | if (msg0 != null) { |
| 852 | msg = msg + ": "+msg0; |
| 853 | } |
| 854 | return JsonMappingException.from(p, msg); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 855 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * Helper method for constructing exception to indicate that given |
| 859 | * 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] | 860 | */ |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 861 | public JsonMappingException unknownTypeException(JavaType type, String id) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 862 | return JsonMappingException.from(_parser, "Could not resolve type id '"+id+"' into a subtype of "+type); |
| 863 | } |
| 864 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 865 | public JsonMappingException endOfInputException(Class<?> instClass) { |
Tatu Saloranta | 5bd1809 | 2012-09-14 20:19:43 -0700 | [diff] [blame] | 866 | return JsonMappingException.from(_parser, "Unexpected end-of-input when trying to deserialize a " |
| 867 | +instClass.getName()); |
| 868 | } |
| 869 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 870 | /* |
| 871 | /********************************************************** |
| 872 | /* Overridable internal methods |
| 873 | /********************************************************** |
| 874 | */ |
| 875 | |
| 876 | protected DateFormat getDateFormat() |
| 877 | { |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 878 | if (_dateFormat != null) { |
| 879 | return _dateFormat; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 880 | } |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 881 | /* 24-Feb-2012, tatu: At this point, all timezone configuration |
| 882 | * should have occured, with respect to default dateformat |
| 883 | * and timezone configuration. But we still better clone |
| 884 | * an instance as formatters may be stateful. |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 885 | */ |
| 886 | DateFormat df = _config.getDateFormat(); |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 887 | _dateFormat = df = (DateFormat) df.clone(); |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 888 | return df; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 891 | protected String determineClassName(Object instance) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 892 | return ClassUtil.getClassDescription(instance); |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | /********************************************************** |
| 897 | /* Other internal methods |
| 898 | /********************************************************** |
| 899 | */ |
| 900 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 901 | protected String _calcName(Class<?> cls) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 902 | if (cls.isArray()) { |
| 903 | return _calcName(cls.getComponentType())+"[]"; |
| 904 | } |
| 905 | return cls.getName(); |
| 906 | } |
| 907 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 908 | protected String _valueDesc() { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 909 | try { |
| 910 | return _desc(_parser.getText()); |
| 911 | } catch (Exception e) { |
| 912 | return "[N/A]"; |
| 913 | } |
| 914 | } |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 915 | |
| 916 | protected String _desc(String desc) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 917 | // !!! should we quote it? (in case there are control chars, linefeeds) |
| 918 | if (desc.length() > MAX_ERROR_STR_LEN) { |
| 919 | desc = desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN); |
| 920 | } |
| 921 | return desc; |
| 922 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 923 | } |