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 | a5301ba | 2015-12-11 09:47:09 -0800 | [diff] [blame] | 9 | import com.fasterxml.jackson.annotation.JsonFormat; |
Tatu Saloranta | d71dffa | 2012-02-07 21:05:21 -0800 | [diff] [blame] | 10 | import com.fasterxml.jackson.annotation.ObjectIdGenerator; |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 11 | import com.fasterxml.jackson.annotation.ObjectIdResolver; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 12 | import com.fasterxml.jackson.core.*; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 13 | import com.fasterxml.jackson.databind.cfg.ContextAttributes; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 14 | import com.fasterxml.jackson.databind.deser.*; |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 15 | import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 16 | import com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer; |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.introspect.Annotated; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
Tatu Saloranta | 2f82344 | 2011-12-23 20:25:27 -0800 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.type.TypeFactory; |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 24 | import com.fasterxml.jackson.databind.util.*; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 25 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 26 | /** |
Tatu Saloranta | 97f8482 | 2012-01-29 21:38:40 -0800 | [diff] [blame] | 27 | * Context for the process of deserialization a single root-level value. |
| 28 | * Used to allow passing in configuration settings and reusable temporary |
| 29 | * objects (scrap arrays, containers). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 30 | *<p> |
| 31 | * Instance life-cycle is such that an partially configured "blueprint" object |
| 32 | * is registered with {@link ObjectMapper} (and {@link ObjectReader}, |
| 33 | * and when an actual instance is needed for deserialization, |
| 34 | * a fully configured instance will |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 35 | * be created using a method in excented API of sub-class |
| 36 | * ({@link com.fasterxml.jackson.databind.deser.DefaultDeserializationContext#createInstance}). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 37 | * Each instance is guaranteed to only be used from single-threaded context; |
| 38 | * instances may be reused iff no configuration has changed. |
| 39 | *<p> |
| 40 | * Defined as abstract class so that implementations must define methods |
| 41 | * for reconfiguring blueprints and creating instances. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 42 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 43 | public abstract class DeserializationContext |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 44 | extends DatabindContext |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 45 | implements java.io.Serializable |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 46 | { |
Cowtowncoder | 7fee92e | 2015-02-11 16:15:08 -0800 | [diff] [blame] | 47 | private static final long serialVersionUID = 1L; // 2.6 |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 48 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Let's limit length of error messages, for cases where underlying data |
| 51 | * may be very large -- no point in spamming logs with megs of meaningless |
| 52 | * data. |
| 53 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 54 | private final static int MAX_ERROR_STR_LEN = 500; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 55 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 56 | /* |
| 57 | /********************************************************** |
| 58 | /* Configuration, immutable |
| 59 | /********************************************************** |
| 60 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 61 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Object that handle details of {@link JsonDeserializer} caching. |
| 64 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 65 | protected final DeserializerCache _cache; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 66 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 67 | /* |
| 68 | /********************************************************** |
| 69 | /* Configuration, changeable via fluent factories |
| 70 | /********************************************************** |
| 71 | */ |
| 72 | |
| 73 | /** |
| 74 | * Read-only factory instance; exposed to let |
| 75 | * owners (<code>ObjectMapper</code>, <code>ObjectReader</code>) |
| 76 | * access it. |
| 77 | */ |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 78 | protected final DeserializerFactory _factory; |
| 79 | |
| 80 | /* |
| 81 | /********************************************************** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 82 | /* Configuration that gets set for instances (not blueprints) |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 83 | /* (partly denormalized for performance) |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 84 | /********************************************************** |
| 85 | */ |
| 86 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Generic deserialization processing configuration |
| 89 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 90 | protected final DeserializationConfig _config; |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Bitmap of {@link DeserializationFeature}s that are enabled |
| 94 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 95 | protected final int _featureFlags; |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 96 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Currently active view, if any. |
| 99 | */ |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 100 | protected final Class<?> _view; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Currently active parser used for deserialization. |
| 104 | * May be different from the outermost parser |
| 105 | * when content is buffered. |
| 106 | */ |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 107 | protected transient JsonParser _parser; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 108 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Object used for resolving references to injectable |
| 111 | * values. |
| 112 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 113 | protected final InjectableValues _injectableValues; |
| 114 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 115 | /* |
| 116 | /********************************************************** |
| 117 | /* Per-operation reusable helper objects (not for blueprints) |
| 118 | /********************************************************** |
| 119 | */ |
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 ArrayBuilders _arrayBuilders; |
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 ObjectBuffer _objectBuffer; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 124 | |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 125 | protected transient DateFormat _dateFormat; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Lazily-constructed holder for per-call attributes. |
| 129 | * |
| 130 | * @since 2.3 |
| 131 | */ |
| 132 | protected transient ContextAttributes _attributes; |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Type of {@link JsonDeserializer} (or, more specifically, |
Cowtowncoder | 08efeba | 2015-05-05 12:34:11 -0700 | [diff] [blame] | 136 | * {@link ContextualDeserializer}) that is being |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 137 | * contextualized currently. |
| 138 | * |
| 139 | * @since 2.5 |
| 140 | */ |
| 141 | protected LinkedNode<JavaType> _currentType; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | /********************************************************** |
| 145 | /* Life-cycle |
| 146 | /********************************************************** |
| 147 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 148 | |
| 149 | protected DeserializationContext(DeserializerFactory df) { |
| 150 | this(df, null); |
| 151 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 152 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 153 | protected DeserializationContext(DeserializerFactory df, |
| 154 | DeserializerCache cache) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 155 | { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 156 | if (df == null) { |
| 157 | throw new IllegalArgumentException("Can not pass null DeserializerFactory"); |
| 158 | } |
| 159 | _factory = df; |
| 160 | _cache = (cache == null) ? new DeserializerCache() : cache; |
| 161 | |
| 162 | _featureFlags = 0; |
| 163 | _config = null; |
| 164 | _injectableValues = null; |
| 165 | _view = null; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 166 | _attributes = null; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | protected DeserializationContext(DeserializationContext src, |
| 170 | DeserializerFactory factory) |
| 171 | { |
| 172 | _cache = src._cache; |
| 173 | _factory = factory; |
| 174 | |
| 175 | _config = src._config; |
| 176 | _featureFlags = src._featureFlags; |
| 177 | _view = src._view; |
| 178 | _parser = src._parser; |
| 179 | _injectableValues = src._injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 180 | _attributes = src._attributes; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 181 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Constructor used for creating actual per-call instances. |
| 185 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 186 | protected DeserializationContext(DeserializationContext src, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 187 | DeserializationConfig config, JsonParser p, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 188 | InjectableValues injectableValues) |
| 189 | { |
| 190 | _cache = src._cache; |
| 191 | _factory = src._factory; |
| 192 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 193 | _config = config; |
Tatu Saloranta | e3ae58e | 2012-01-28 23:08:16 -0800 | [diff] [blame] | 194 | _featureFlags = config.getDeserializationFeatures(); |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 195 | _view = config.getActiveView(); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 196 | _parser = p; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 197 | _injectableValues = injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 198 | _attributes = config.getAttributes(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 199 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 200 | |
Tatu Saloranta | 23328aa | 2014-10-28 22:38:15 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Copy-constructor for use with <code>copy()</code> by {@link ObjectMapper#copy()} |
| 203 | */ |
| 204 | protected DeserializationContext(DeserializationContext src) { |
| 205 | _cache = new DeserializerCache(); |
| 206 | _factory = src._factory; |
| 207 | |
| 208 | _config = src._config; |
| 209 | _featureFlags = src._featureFlags; |
| 210 | _view = src._view; |
| 211 | _injectableValues = null; |
| 212 | } |
| 213 | |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 214 | /* |
| 215 | /********************************************************** |
| 216 | /* DatabindContext implementation |
| 217 | /********************************************************** |
| 218 | */ |
| 219 | |
| 220 | @Override |
| 221 | public DeserializationConfig getConfig() { return _config; } |
| 222 | |
| 223 | @Override |
| 224 | public final Class<?> getActiveView() { return _view; } |
| 225 | |
| 226 | @Override |
Tatu Saloranta | a5301ba | 2015-12-11 09:47:09 -0800 | [diff] [blame] | 227 | public final boolean canOverrideAccessModifiers() { |
| 228 | return _config.canOverrideAccessModifiers(); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public final boolean isEnabled(MapperFeature feature) { |
| 233 | return _config.isEnabled(feature); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public final JsonFormat.Value getDefaultPropertyFormat(Class<?> baseType) { |
| 238 | return _config.getDefaultPropertyFormat(baseType); |
| 239 | } |
Tatu Saloranta | 4fdbb69 | 2016-04-16 21:41:14 -0700 | [diff] [blame] | 240 | |
Tatu Saloranta | a5301ba | 2015-12-11 09:47:09 -0800 | [diff] [blame] | 241 | @Override |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 242 | public final AnnotationIntrospector getAnnotationIntrospector() { |
| 243 | return _config.getAnnotationIntrospector(); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public final TypeFactory getTypeFactory() { |
| 248 | return _config.getTypeFactory(); |
| 249 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 250 | |
Cowtowncoder | 41bbc96 | 2015-06-19 17:16:08 -0700 | [diff] [blame] | 251 | /** |
| 252 | * Method for accessing default Locale to use: convenience method for |
| 253 | *<pre> |
| 254 | * getConfig().getLocale(); |
| 255 | *</pre> |
| 256 | */ |
| 257 | @Override |
| 258 | public Locale getLocale() { |
| 259 | return _config.getLocale(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Method for accessing default TimeZone to use: convenience method for |
| 264 | *<pre> |
| 265 | * getConfig().getTimeZone(); |
| 266 | *</pre> |
| 267 | */ |
| 268 | @Override |
| 269 | public TimeZone getTimeZone() { |
| 270 | return _config.getTimeZone(); |
| 271 | } |
| 272 | |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 273 | /* |
| 274 | /********************************************************** |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 275 | /* Access to per-call state, like generic attributes (2.3+) |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 276 | /********************************************************** |
| 277 | */ |
| 278 | |
| 279 | @Override |
| 280 | public Object getAttribute(Object key) { |
| 281 | return _attributes.getAttribute(key); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public DeserializationContext setAttribute(Object key, Object value) |
| 286 | { |
| 287 | _attributes = _attributes.withPerCallAttribute(key, value); |
| 288 | return this; |
| 289 | } |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * Accessor to {@link JavaType} of currently contextualized |
| 293 | * {@link ContextualDeserializer}, if any. |
| 294 | * This is sometimes useful for generic {@link JsonDeserializer}s that |
| 295 | * do not get passed (or do not retain) type information when being |
| 296 | * constructed: happens for example for deserializers constructed |
| 297 | * from annotations. |
| 298 | * |
| 299 | * @since 2.5 |
| 300 | * |
| 301 | * @return Type of {@link ContextualDeserializer} being contextualized, |
| 302 | * if process is on-going; null if not. |
| 303 | */ |
| 304 | public JavaType getContextualType() { |
| 305 | return (_currentType == null) ? null : _currentType.value(); |
| 306 | } |
| 307 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 308 | /* |
| 309 | /********************************************************** |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 310 | /* Public API, config setting accessors |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 311 | /********************************************************** |
| 312 | */ |
| 313 | |
| 314 | /** |
Dmitry Katsubo | e70c66f | 2012-09-03 16:00:30 +0200 | [diff] [blame] | 315 | * Method for getting current {@link DeserializerFactory}. |
| 316 | */ |
| 317 | public DeserializerFactory getFactory() { |
| 318 | return _factory; |
| 319 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 320 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 321 | /** |
| 322 | * Convenience method for checking whether specified on/off |
| 323 | * feature is enabled |
| 324 | */ |
Tatu | 9610aff | 2012-02-02 11:30:08 -0800 | [diff] [blame] | 325 | public final boolean isEnabled(DeserializationFeature feat) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 326 | /* 03-Dec-2010, tatu: minor shortcut; since this is called quite often, |
| 327 | * let's use a local copy of feature settings: |
| 328 | */ |
| 329 | return (_featureFlags & feat.getMask()) != 0; |
| 330 | } |
| 331 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 332 | /** |
Cowtowncoder | 23e52ab | 2015-05-19 17:53:26 -0700 | [diff] [blame] | 333 | * Bulk access method for getting the bit mask of all {@link DeserializationFeature}s |
| 334 | * that are enabled. |
| 335 | * |
| 336 | * @since 2.6 |
| 337 | */ |
| 338 | public final int getDeserializationFeatures() { |
| 339 | return _featureFlags; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Bulk access method for checking that all features specified by |
Tatu Saloranta | 0ac36ba | 2013-08-21 18:08:51 -0700 | [diff] [blame] | 344 | * mask are enabled. |
| 345 | * |
| 346 | * @since 2.3 |
| 347 | */ |
| 348 | public final boolean hasDeserializationFeatures(int featureMask) { |
Cowtowncoder | 23e52ab | 2015-05-19 17:53:26 -0700 | [diff] [blame] | 349 | return (_featureFlags & featureMask) == featureMask; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Bulk access method for checking that at least one of features specified by |
| 354 | * mask is enabled. |
| 355 | * |
| 356 | * @since 2.6 |
| 357 | */ |
| 358 | public final boolean hasSomeOfFeatures(int featureMask) { |
| 359 | return (_featureFlags & featureMask) != 0; |
Tatu Saloranta | 0ac36ba | 2013-08-21 18:08:51 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 363 | * Method for accessing the currently active parser. |
| 364 | * May be different from the outermost parser |
| 365 | * when content is buffered. |
| 366 | *<p> |
| 367 | * Use of this method is discouraged: if code has direct access |
| 368 | * to the active parser, that should be used instead. |
| 369 | */ |
| 370 | public final JsonParser getParser() { return _parser; } |
| 371 | |
| 372 | public final Object findInjectableValue(Object valueId, |
| 373 | BeanProperty forProperty, Object beanInstance) |
| 374 | { |
| 375 | if (_injectableValues == null) { |
| 376 | throw new IllegalStateException("No 'injectableValues' configured, can not inject value with id ["+valueId+"]"); |
| 377 | } |
| 378 | return _injectableValues.findInjectableValue(valueId, this, forProperty, beanInstance); |
| 379 | } |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 380 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 381 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 382 | * Convenience method for accessing the default Base64 encoding |
| 383 | * used for decoding base64 encoded binary content. |
| 384 | * Same as calling: |
| 385 | *<pre> |
| 386 | * getConfig().getBase64Variant(); |
| 387 | *</pre> |
| 388 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 389 | public final Base64Variant getBase64Variant() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 390 | return _config.getBase64Variant(); |
| 391 | } |
| 392 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 393 | /** |
| 394 | * Convenience method, functionally equivalent to: |
| 395 | *<pre> |
| 396 | * getConfig().getNodeFactory(); |
| 397 | * </pre> |
| 398 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 399 | public final JsonNodeFactory getNodeFactory() { |
| 400 | return _config.getNodeFactory(); |
| 401 | } |
| 402 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 403 | /* |
| 404 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 405 | /* Public API, pass-through to DeserializerCache |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 406 | /********************************************************** |
| 407 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 408 | |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 409 | @Deprecated // since 2.3, use overloaded variant |
| 410 | public boolean hasValueDeserializerFor(JavaType type) { |
| 411 | return hasValueDeserializerFor(type, null); |
| 412 | } |
| 413 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 414 | /** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 415 | * Method for checking whether we could find a deserializer |
| 416 | * for given type. |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 417 | * |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 418 | * @param type |
| 419 | * @since 2.3 |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 420 | */ |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 421 | public boolean hasValueDeserializerFor(JavaType type, AtomicReference<Throwable> cause) { |
| 422 | try { |
| 423 | return _cache.hasValueDeserializerFor(this, _factory, type); |
| 424 | } catch (JsonMappingException e) { |
| 425 | if (cause != null) { |
| 426 | cause.set(e); |
| 427 | } |
| 428 | } catch (RuntimeException e) { |
| 429 | if (cause == null) { // earlier behavior |
| 430 | throw e; |
| 431 | } |
| 432 | cause.set(e); |
| 433 | } |
| 434 | return false; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 437 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 438 | * Method for finding a value deserializer, and creating a contextual |
| 439 | * version if necessary, for value reached via specified property. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 440 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 441 | @SuppressWarnings("unchecked") |
| 442 | public final JsonDeserializer<Object> findContextualValueDeserializer(JavaType type, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 443 | BeanProperty prop) throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 444 | { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 445 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 446 | if (deser != null) { |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 447 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, prop, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 448 | } |
| 449 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 450 | } |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 451 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 452 | /** |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 453 | * Variant that will try to locate deserializer for current type, but without |
| 454 | * performing any contextualization (unlike {@link #findContextualValueDeserializer}) |
| 455 | * or checking for need to create a {@link TypeDeserializer} (unlike |
| 456 | * {@link #findRootValueDeserializer(JavaType)}. |
| 457 | * This method is usually called from within {@link ResolvableDeserializer#resolve}, |
| 458 | * and expectation is that caller then calls either |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 459 | * {@link #handlePrimaryContextualization(JsonDeserializer, BeanProperty, JavaType)} or |
| 460 | * {@link #handleSecondaryContextualization(JsonDeserializer, BeanProperty, JavaType)} at a |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 461 | * later point, as necessary. |
| 462 | * |
| 463 | * @since 2.5 |
| 464 | */ |
| 465 | public final JsonDeserializer<Object> findNonContextualValueDeserializer(JavaType type) |
| 466 | throws JsonMappingException |
| 467 | { |
| 468 | return _cache.findValueDeserializer(this, _factory, type); |
| 469 | } |
| 470 | |
| 471 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 472 | * Method for finding a deserializer for root-level value. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 473 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 474 | @SuppressWarnings("unchecked") |
Tatu Saloranta | 9b9d043 | 2012-01-30 09:20:26 -0800 | [diff] [blame] | 475 | public final JsonDeserializer<Object> findRootValueDeserializer(JavaType type) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 476 | throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 477 | { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 478 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, |
| 479 | _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 480 | if (deser == null) { // can this occur? |
| 481 | return null; |
| 482 | } |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 483 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, null, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 484 | TypeDeserializer typeDeser = _factory.findTypeDeserializer(_config, type); |
| 485 | if (typeDeser != null) { |
| 486 | // important: contextualize to indicate this is for root value |
| 487 | typeDeser = typeDeser.forProperty(null); |
| 488 | return new TypeWrappedDeserializer(typeDeser, deser); |
| 489 | } |
| 490 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 491 | } |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 492 | |
| 493 | /** |
| 494 | * Convenience method, functionally same as: |
| 495 | *<pre> |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 496 | * getDeserializerProvider().findKeyDeserializer(getConfig(), prop.getType(), prop); |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 497 | *</pre> |
| 498 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 499 | public final KeyDeserializer findKeyDeserializer(JavaType keyType, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 500 | BeanProperty prop) throws JsonMappingException { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 501 | KeyDeserializer kd = _cache.findKeyDeserializer(this, |
| 502 | _factory, keyType); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 503 | // Second: contextualize? |
| 504 | if (kd instanceof ContextualKeyDeserializer) { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 505 | kd = ((ContextualKeyDeserializer) kd).createContextual(this, prop); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 506 | } |
| 507 | return kd; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 510 | /* |
| 511 | /********************************************************** |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 512 | /* Public API, ObjectId handling |
| 513 | /********************************************************** |
| 514 | */ |
| 515 | |
| 516 | /** |
| 517 | * Method called to find and return entry corresponding to given |
| 518 | * Object Id: will add an entry if necessary, and never returns null |
| 519 | */ |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 520 | public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> generator, ObjectIdResolver resolver); |
| 521 | |
Tatu Saloranta | 301ea2d | 2014-03-16 22:26:12 -0700 | [diff] [blame] | 522 | @Deprecated // since 2.4 |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 523 | public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> generator); |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 524 | |
Pascal GĂ©linas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 525 | /** |
| 526 | * Method called to ensure that every object id encounter during processing |
| 527 | * are resolved. |
| 528 | * |
| 529 | * @throws UnresolvedForwardReference |
| 530 | */ |
| 531 | public abstract void checkUnresolvedObjectId() |
| 532 | throws UnresolvedForwardReference; |
| 533 | |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 534 | /* |
| 535 | /********************************************************** |
| 536 | /* Public API, type handling |
| 537 | /********************************************************** |
| 538 | */ |
| 539 | |
| 540 | /** |
| 541 | * Convenience method, functionally equivalent to: |
| 542 | *<pre> |
| 543 | * getConfig().constructType(cls); |
| 544 | * </pre> |
| 545 | */ |
| 546 | public final JavaType constructType(Class<?> cls) { |
| 547 | return _config.constructType(cls); |
| 548 | } |
| 549 | |
| 550 | /** |
Cowtowncoder | a5bd0a2 | 2015-06-25 15:27:07 -0700 | [diff] [blame] | 551 | * Helper method that is to be used when resolving basic class name into |
| 552 | * Class instance, the reason being that it may be necessary to work around |
| 553 | * various ClassLoader limitations, as well as to handle primitive type |
| 554 | * signatures. |
| 555 | * |
| 556 | * @since 2.6 |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 557 | */ |
| 558 | public Class<?> findClass(String className) throws ClassNotFoundException |
| 559 | { |
| 560 | // By default, delegate to ClassUtil: can be overridden with custom handling |
Cowtowncoder | a5bd0a2 | 2015-06-25 15:27:07 -0700 | [diff] [blame] | 561 | return getTypeFactory().findClass(className); |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 562 | } |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 563 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 564 | /* |
| 565 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 566 | /* Public API, helper object recycling |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 567 | /********************************************************** |
| 568 | */ |
| 569 | |
| 570 | /** |
| 571 | * Method that can be used to get access to a reusable ObjectBuffer, |
| 572 | * useful for efficiently constructing Object arrays and Lists. |
| 573 | * Note that leased buffers should be returned once deserializer |
| 574 | * is done, to allow for reuse during same round of deserialization. |
| 575 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 576 | public final ObjectBuffer leaseObjectBuffer() |
| 577 | { |
| 578 | ObjectBuffer buf = _objectBuffer; |
| 579 | if (buf == null) { |
| 580 | buf = new ObjectBuffer(); |
| 581 | } else { |
| 582 | _objectBuffer = null; |
| 583 | } |
| 584 | return buf; |
| 585 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * Method to call to return object buffer previously leased with |
| 589 | * {@link #leaseObjectBuffer}. |
| 590 | * |
| 591 | * @param buf Returned object buffer |
| 592 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 593 | public final void returnObjectBuffer(ObjectBuffer buf) |
| 594 | { |
| 595 | /* Already have a reusable buffer? Let's retain bigger one |
| 596 | * (or if equal, favor newer one, shorter life-cycle) |
| 597 | */ |
| 598 | if (_objectBuffer == null |
| 599 | || buf.initialCapacity() >= _objectBuffer.initialCapacity()) { |
| 600 | _objectBuffer = buf; |
| 601 | } |
| 602 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * Method for accessing object useful for building arrays of |
| 606 | * primitive types (such as int[]). |
| 607 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 608 | public final ArrayBuilders getArrayBuilders() |
| 609 | { |
| 610 | if (_arrayBuilders == null) { |
| 611 | _arrayBuilders = new ArrayBuilders(); |
| 612 | } |
| 613 | return _arrayBuilders; |
| 614 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | /********************************************************** |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 618 | /* Extended API: handler instantiation |
| 619 | /********************************************************** |
| 620 | */ |
| 621 | |
| 622 | public abstract JsonDeserializer<Object> deserializerInstance(Annotated annotated, |
| 623 | Object deserDef) |
| 624 | throws JsonMappingException; |
| 625 | |
| 626 | public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated, |
| 627 | Object deserDef) |
| 628 | throws JsonMappingException; |
| 629 | |
| 630 | /* |
| 631 | /********************************************************** |
| 632 | /* Extended API: resolving contextual deserializers; called |
| 633 | /* by structured deserializers for their value/component |
| 634 | /* deserializers |
| 635 | /********************************************************** |
| 636 | */ |
| 637 | |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 638 | /** |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 639 | * Method called for primary property deserializers (ones |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 640 | * directly created to deserialize values of a POJO property), |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 641 | * to handle details of resolving |
| 642 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 643 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 644 | * @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] | 645 | * |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 646 | * @since 2.5 |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 647 | */ |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 648 | public JsonDeserializer<?> handlePrimaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 649 | BeanProperty prop, JavaType type) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 650 | throws JsonMappingException |
| 651 | { |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 652 | if (deser instanceof ContextualDeserializer) { |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 653 | _currentType = new LinkedNode<JavaType>(type, _currentType); |
| 654 | try { |
| 655 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
| 656 | } finally { |
| 657 | _currentType = _currentType.next(); |
| 658 | } |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 659 | } |
| 660 | return deser; |
| 661 | } |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 662 | |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 663 | /** |
| 664 | * Method called for secondary property deserializers (ones |
| 665 | * NOT directly created to deal with an annotatable POJO property, |
| 666 | * but instead created as a component -- such as value deserializers |
| 667 | * for structured types, or deserializers for root values) |
| 668 | * to handle details of resolving |
| 669 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 670 | * Given that these deserializers are not directly related to given property |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 671 | * (or, in case of root value property, to any property), annotations |
| 672 | * accessible may or may not be relevant. |
| 673 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 674 | * @param prop Property for which deserializer is used, if any; null |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 675 | * when deserializing root values |
| 676 | * |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 677 | * @since 2.5 |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 678 | */ |
| 679 | public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 680 | BeanProperty prop, JavaType type) |
| 681 | throws JsonMappingException |
| 682 | { |
| 683 | if (deser instanceof ContextualDeserializer) { |
| 684 | _currentType = new LinkedNode<JavaType>(type, _currentType); |
| 685 | try { |
| 686 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
| 687 | } finally { |
| 688 | _currentType = _currentType.next(); |
| 689 | } |
| 690 | } |
| 691 | return deser; |
| 692 | } |
| 693 | |
Cowtowncoder | 3686421 | 2015-03-16 14:23:22 -0700 | [diff] [blame] | 694 | @Deprecated // since 2.5; remove from 2.7 |
| 695 | public JsonDeserializer<?> handlePrimaryContextualization(JsonDeserializer<?> deser, BeanProperty prop) throws JsonMappingException { |
| 696 | return handlePrimaryContextualization(deser, prop, TypeFactory.unknownType()); |
| 697 | } |
| 698 | |
| 699 | @Deprecated // since 2.5; remove from 2.7 |
| 700 | public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser, BeanProperty prop) throws JsonMappingException { |
| 701 | if (deser instanceof ContextualDeserializer) { |
| 702 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
| 703 | } |
| 704 | return deser; |
| 705 | } |
| 706 | |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 707 | /* |
| 708 | /********************************************************** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 709 | /* Parsing methods that may use reusable/-cyclable objects |
| 710 | /********************************************************** |
| 711 | */ |
| 712 | |
| 713 | /** |
| 714 | * Convenience method for parsing a Date from given String, using |
| 715 | * currently configured date format (accessed using |
| 716 | * {@link DeserializationConfig#getDateFormat()}). |
| 717 | *<p> |
| 718 | * Implementation will handle thread-safety issues related to |
| 719 | * date formats such that first time this method is called, |
| 720 | * date format is cloned, and cloned instance will be retained |
| 721 | * for use during this deserialization round. |
| 722 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 723 | public Date parseDate(String dateStr) throws IllegalArgumentException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 724 | { |
| 725 | try { |
Tatu Saloranta | ef00ac6 | 2013-11-03 09:56:39 -0800 | [diff] [blame] | 726 | DateFormat df = getDateFormat(); |
| 727 | return df.parse(dateStr); |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 728 | } catch (ParseException e) { |
Tatu Saloranta | 8e9d4b2 | 2015-05-01 21:26:32 -0700 | [diff] [blame] | 729 | throw new IllegalArgumentException(String.format( |
| 730 | "Failed to parse Date value '%s': %s", dateStr, e.getMessage())); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 733 | |
| 734 | /** |
| 735 | * Convenience method for constructing Calendar instance set |
| 736 | * to specified time, to be modified and used by caller. |
| 737 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 738 | public Calendar constructCalendar(Date d) { |
| 739 | // 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] | 740 | Calendar c = Calendar.getInstance(getTimeZone()); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 741 | c.setTime(d); |
| 742 | return c; |
| 743 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 744 | |
| 745 | /* |
| 746 | /********************************************************** |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 747 | /* Convenience methods for reading parsed values |
| 748 | /********************************************************** |
| 749 | */ |
| 750 | |
| 751 | /** |
| 752 | * Convenience method that may be used by composite or container deserializers, |
| 753 | * for reading one-off values contained (for sequences, it is more efficient |
| 754 | * to actually fetch deserializer once for the whole collection). |
| 755 | *<p> |
| 756 | * NOTE: when deserializing values of properties contained in composite types, |
| 757 | * rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)}; |
| 758 | * this method does not allow use of contextual annotations. |
| 759 | * |
| 760 | * @since 2.4 |
| 761 | */ |
| 762 | public <T> T readValue(JsonParser p, Class<T> type) throws IOException { |
| 763 | return readValue(p, getTypeFactory().constructType(type)); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * @since 2.4 |
| 768 | */ |
| 769 | @SuppressWarnings("unchecked") |
| 770 | public <T> T readValue(JsonParser p, JavaType type) throws IOException { |
| 771 | JsonDeserializer<Object> deser = findRootValueDeserializer(type); |
| 772 | if (deser == null) { |
Tatu Saloranta | 5ae6e4b | 2015-10-05 21:49:01 -0700 | [diff] [blame] | 773 | throw mappingException("Could not find JsonDeserializer for type %s", type); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 774 | } |
| 775 | return (T) deser.deserialize(p, this); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Convenience method that may be used by composite or container deserializers, |
| 780 | * for reading one-off values for the composite type, taking into account |
| 781 | * annotations that the property (passed to this method -- usually property that |
| 782 | * has custom serializer that called this method) has. |
| 783 | * |
| 784 | * @since 2.4 |
| 785 | */ |
| 786 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, Class<T> type) throws IOException { |
| 787 | return readPropertyValue(p, prop, getTypeFactory().constructType(type)); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * @since 2.4 |
| 792 | */ |
| 793 | @SuppressWarnings("unchecked") |
| 794 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException { |
| 795 | JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop); |
| 796 | if (deser == null) { |
Tatu Saloranta | 5ae6e4b | 2015-10-05 21:49:01 -0700 | [diff] [blame] | 797 | String propName = (prop == null) ? "NULL" : ("'"+prop.getName()+"'"); |
| 798 | throw mappingException("Could not find JsonDeserializer for type %s (via property %s)", |
| 799 | type, propName); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 800 | } |
| 801 | return (T) deser.deserialize(p, this); |
| 802 | } |
Tatu Saloranta | 5ae6e4b | 2015-10-05 21:49:01 -0700 | [diff] [blame] | 803 | |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 804 | /* |
| 805 | /********************************************************** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 806 | /* Methods for problem handling |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 807 | /********************************************************** |
| 808 | */ |
| 809 | |
| 810 | /** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 811 | * Method that deserializers should call if they encounter an unrecognized |
| 812 | * property (and once that is not explicitly designed as ignorable), to |
| 813 | * inform possibly configured {@link DeserializationProblemHandler}s and |
| 814 | * let it handle the problem. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 815 | * |
| 816 | * @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] | 817 | * problem |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 818 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 819 | public boolean handleUnknownProperty(JsonParser p, JsonDeserializer<?> deser, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 820 | Object instanceOrClass, String propName) |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 821 | throws IOException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 822 | { |
| 823 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 824 | while (h != null) { |
| 825 | // Can bail out if it's handled |
| 826 | if (h.value().handleUnknownProperty(this, p, deser, instanceOrClass, propName)) { |
| 827 | return true; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 828 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 829 | h = h.next(); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 830 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 831 | // Nope, not handled. Potentially that's a problem... |
| 832 | if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { |
| 833 | p.skipChildren(); |
| 834 | return true; |
| 835 | } |
| 836 | // Do we know properties that are expected instead? |
| 837 | Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); |
| 838 | throw UnrecognizedPropertyException.from(_parser, |
| 839 | instanceOrClass, propName, propIds); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 840 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 841 | |
| 842 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 843 | * Method that deserializers should call if they encounter a String value |
| 844 | * that can not be converted to expected key of a {@link java.util.Map} |
| 845 | * valued property. |
| 846 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue} |
| 847 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 848 | * succeed, will throw {@link InvalidFormatException} with given message. |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 849 | * |
| 850 | * @param keyClass Expected type for key |
| 851 | * @param keyValue String value from which to deserialize key |
| 852 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 853 | * @param msgArgs Optional arguments to use for message, if any |
| 854 | * |
| 855 | * @return Key value to use |
| 856 | * |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 857 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 858 | * |
| 859 | * @since 2.8 |
| 860 | */ |
| 861 | public Object handleWeirdKey(Class<?> keyClass, String keyValue, |
| 862 | String msg, Object... msgArgs) |
| 863 | throws IOException |
| 864 | { |
| 865 | // but if not handled, just throw exception |
| 866 | if (msgArgs.length > 0) { |
| 867 | msg = String.format(msg, msgArgs); |
| 868 | } |
| 869 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 870 | while (h != null) { |
| 871 | // Can bail out if it's handled |
| 872 | Object key = h.value().handleWeirdKey(this, keyClass, keyValue, msg); |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 873 | if (key != DeserializationProblemHandler.NOT_HANDLED) { |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 874 | return key; |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 875 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 876 | h = h.next(); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 877 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 878 | throw weirdKeyException(keyClass, keyValue, msg); |
| 879 | } |
| 880 | |
| 881 | /** |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 882 | * Method that deserializers should call if they encounter a String value |
| 883 | * that can not be converted to target property type, in cases where some |
| 884 | * String values could be acceptable (either with different settings, |
| 885 | * or different value). |
| 886 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdStringValue} |
| 887 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 888 | * succeed, will throw {@link InvalidFormatException} with given message. |
| 889 | * |
| 890 | * @param targetClass Type of property into which incoming number should be converted |
| 891 | * @param value String value from which to deserialize property value |
| 892 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 893 | * @param msgArgs Optional arguments to use for message, if any |
| 894 | * |
| 895 | * @return Property value to use |
| 896 | * |
| 897 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
| 898 | * |
| 899 | * @since 2.8 |
| 900 | */ |
| 901 | public Object handleWeirdStringValue(Class<?> targetClass, String value, |
| 902 | String msg, Object... msgArgs) |
| 903 | throws IOException |
| 904 | { |
| 905 | // but if not handled, just throw exception |
| 906 | if (msgArgs.length > 0) { |
| 907 | msg = String.format(msg, msgArgs); |
| 908 | } |
| 909 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 910 | while (h != null) { |
| 911 | // Can bail out if it's handled |
| 912 | Object key = h.value().handleWeirdStringValue(this, targetClass, value, msg); |
| 913 | if (key != DeserializationProblemHandler.NOT_HANDLED) { |
| 914 | return key; |
| 915 | } |
| 916 | h = h.next(); |
| 917 | } |
| 918 | throw weirdStringException(value, targetClass, msg); |
| 919 | } |
| 920 | |
| 921 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 922 | * Method that deserializers should call if they encounter a numeric value |
| 923 | * that can not be converted to target property type, in cases where some |
| 924 | * numeric values could be acceptable (either with different settings, |
| 925 | * or different numeric value). |
| 926 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue} |
| 927 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 928 | * succeed, will throw {@link InvalidFormatException} with given message. |
| 929 | * |
| 930 | * @param targetClass Type of property into which incoming number should be converted |
| 931 | * @param value Number value from which to deserialize property value |
| 932 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 933 | * @param msgArgs Optional arguments to use for message, if any |
| 934 | * |
| 935 | * @return Property value to use |
| 936 | * |
| 937 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
| 938 | * |
| 939 | * @since 2.8 |
| 940 | */ |
| 941 | public Object handleWeirdNumberValue(Class<?> targetClass, Number value, |
| 942 | String msg, Object... msgArgs) |
| 943 | throws IOException |
| 944 | { |
| 945 | // but if not handled, just throw exception |
| 946 | if (msgArgs.length > 0) { |
| 947 | msg = String.format(msg, msgArgs); |
| 948 | } |
| 949 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 950 | while (h != null) { |
| 951 | // Can bail out if it's handled |
| 952 | Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg); |
| 953 | if (key != DeserializationProblemHandler.NOT_HANDLED) { |
| 954 | return key; |
| 955 | } |
| 956 | h = h.next(); |
| 957 | } |
| 958 | throw weirdNumberException(value, targetClass, msg); |
| 959 | } |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 960 | |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 961 | /** |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 962 | * Method that deserializers should call if they encounter a type id |
| 963 | * (for polymorphic deserialization) that can not be resolved to an |
| 964 | * actual type; usually since there is no mapping defined. |
| 965 | * Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId} |
| 966 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 967 | * succeed, will throw exception constructed with {@link #unknownTypeIdException}. |
| 968 | * |
| 969 | * @param baseType Base type from which resolution starts |
| 970 | * @param id Type id that could not be converted |
| 971 | * @param extraDesc Additional problem description to add to default exception message, |
| 972 | * if resolution fails. |
| 973 | * |
| 974 | * @return {@link JavaType} that id resolves to |
| 975 | * |
| 976 | * @throws IOException To indicate unrecoverable problem, if resolution can not |
| 977 | * be made to work |
| 978 | * |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 979 | * @since 2.8 |
| 980 | */ |
| 981 | public JavaType handleUnknownTypeId(JavaType baseType, String id, |
| 982 | String extraDesc) throws IOException |
| 983 | { |
| 984 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 985 | while (h != null) { |
| 986 | // Can bail out if it's handled |
| 987 | JavaType type = h.value().handleUnknownTypeId(this, baseType, id, extraDesc); |
| 988 | if (type != null) { |
| 989 | if (type.hasRawClass(Void.class)) { |
| 990 | return null; |
| 991 | } |
Tatu Saloranta | f2c40cb | 2016-05-10 20:21:32 -0700 | [diff] [blame] | 992 | // But ensure there's type compatibility |
| 993 | if (type.isTypeOrSubTypeOf(baseType.getRawClass())) { |
| 994 | return type; |
| 995 | } |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 996 | throw unknownTypeIdException(baseType, id, |
Tatu Saloranta | f2c40cb | 2016-05-10 20:21:32 -0700 | [diff] [blame] | 997 | "problem handler tried to resolve into non-subtype: "+type); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 998 | } |
| 999 | h = h.next(); |
| 1000 | } |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1001 | throw unknownTypeIdException(baseType, id, extraDesc); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1004 | /** |
| 1005 | * Method that deserializers should call if they fail to instantiate value |
| 1006 | * due to an exception that was thrown by constructor (or other mechanism used |
| 1007 | * to create instances). |
| 1008 | * Default implementation will try to call {@link DeserializationProblemHandler#handleInstantiationProblem} |
| 1009 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 1010 | * succeed, will throw exception constructed with {@link #instantiationException}. |
| 1011 | * |
| 1012 | * @param instClass Type that was to be instantiated |
| 1013 | * @param argument (optional) Argument that was passed to constructor or equivalent |
| 1014 | * instantiator; often a {@link java.lang.String}. |
| 1015 | * @param t Exception that caused failure |
| 1016 | * |
| 1017 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1018 | * |
| 1019 | * @since 2.8 |
| 1020 | */ |
| 1021 | public Object handleInstantiationProblem(Class<?> instClass, Object argument, |
| 1022 | Throwable t) |
| 1023 | throws IOException |
| 1024 | { |
| 1025 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1026 | while (h != null) { |
| 1027 | // Can bail out if it's handled |
| 1028 | Object key = h.value().handleInstantiationProblem(this, instClass, argument, t); |
| 1029 | if (key != DeserializationProblemHandler.NOT_HANDLED) { |
| 1030 | return key; |
| 1031 | } |
| 1032 | h = h.next(); |
| 1033 | } |
| 1034 | // 18-May-2016, tatu: Only wrap if not already a valid type to throw |
| 1035 | if (t instanceof IOException) { |
| 1036 | throw (IOException) t; |
| 1037 | } |
| 1038 | throw instantiationException(instClass, t); |
| 1039 | } |
| 1040 | |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | /********************************************************** |
| 1043 | /* Methods for problem reporting |
| 1044 | /********************************************************** |
| 1045 | */ |
| 1046 | |
| 1047 | /** |
| 1048 | * Helper method for reporting a problem with unhandled unknown property. |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1049 | * |
| 1050 | * @param instanceOrClass Either value being populated (if one has been |
| 1051 | * instantiated), or Class that indicates type that would be (or |
| 1052 | * have been) instantiated |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 1053 | * @param deser Deserializer that had the problem, if called by deserializer |
| 1054 | * (or on behalf of one) |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1055 | * |
| 1056 | * @deprecated Since 2.8 call {@link #handleUnknownProperty} instead |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1057 | */ |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1058 | @Deprecated |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 1059 | public void reportUnknownProperty(Object instanceOrClass, String fieldName, |
| 1060 | JsonDeserializer<?> deser) |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1061 | throws JsonMappingException |
| 1062 | { |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 1063 | if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { |
| 1064 | return; |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1065 | } |
Tatu Saloranta | 05fce93 | 2012-03-19 17:14:05 -0700 | [diff] [blame] | 1066 | // Do we know properties that are expected instead? |
| 1067 | Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); |
| 1068 | throw UnrecognizedPropertyException.from(_parser, |
| 1069 | instanceOrClass, fieldName, propIds); |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1070 | } |
Tatu Saloranta | 8c3eb03 | 2016-04-20 22:52:38 -0700 | [diff] [blame] | 1071 | |
| 1072 | /** |
| 1073 | * @since 2.8 |
| 1074 | */ |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1075 | public void reportInstantiationException(Class<?> instClass, |
Tatu Saloranta | 8c3eb03 | 2016-04-20 22:52:38 -0700 | [diff] [blame] | 1076 | String msg, Object... msgArgs) |
| 1077 | throws JsonMappingException |
| 1078 | { |
| 1079 | if (msgArgs.length > 0) { |
| 1080 | msg = String.format(msg, msgArgs); |
| 1081 | } |
| 1082 | throw instantiationException(instClass, msg); |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * @since 2.8 |
| 1087 | */ |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1088 | public JsonMappingException reportWrongTokenException(JsonParser p, |
Tatu Saloranta | 8c3eb03 | 2016-04-20 22:52:38 -0700 | [diff] [blame] | 1089 | JsonToken expToken, String msg, Object... msgArgs) |
| 1090 | throws JsonMappingException |
| 1091 | { |
| 1092 | if (msgArgs.length > 0) { |
| 1093 | msg = String.format(msg, msgArgs); |
| 1094 | } |
| 1095 | throw wrongTokenException(p, expToken, msg); |
| 1096 | } |
| 1097 | |
| 1098 | /** |
| 1099 | * @since 2.8 |
| 1100 | */ |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1101 | public void reportMappingException(String msg, Object... msgArgs) |
| 1102 | throws JsonMappingException |
| 1103 | { |
| 1104 | if (msgArgs.length > 0) { |
| 1105 | msg = String.format(msg, msgArgs); |
| 1106 | } |
| 1107 | throw mappingException(msg); |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * @since 2.8 |
| 1112 | */ |
| 1113 | public void reportMappingException(Class<?> targetClass, JsonToken t) throws JsonMappingException { |
| 1114 | throw mappingException(targetClass, t); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * @since 2.8 |
| 1119 | */ |
| 1120 | public void reportMappingException(Class<?> targetClass) throws JsonMappingException { |
| 1121 | throw mappingException(targetClass); |
| 1122 | } |
| 1123 | |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1124 | /* |
| 1125 | /********************************************************** |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1126 | /* Methods for constructing exceptions, "untyped" |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1127 | /********************************************************** |
| 1128 | */ |
| 1129 | |
| 1130 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1131 | * Helper method for constructing generic mapping exception for specified type |
Tatu Saloranta | bd3c312 | 2016-05-10 19:33:23 -0700 | [diff] [blame] | 1132 | * |
| 1133 | * @deprecated Since 2.8 use {@link #reportMappingException(Class)} instead |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1134 | */ |
Tatu Saloranta | bd3c312 | 2016-05-10 19:33:23 -0700 | [diff] [blame] | 1135 | @Deprecated |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1136 | public JsonMappingException mappingException(Class<?> targetClass) { |
| 1137 | return mappingException(targetClass, _parser.getCurrentToken()); |
| 1138 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1139 | |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1140 | /** |
Tatu Saloranta | bd3c312 | 2016-05-10 19:33:23 -0700 | [diff] [blame] | 1141 | * @deprecated Since 2.8 use {@link #reportMappingException(Class, JsonToken)} instead |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1142 | */ |
| 1143 | @Deprecated |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 1144 | public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) { |
Tatu Saloranta | 8e9d4b2 | 2015-05-01 21:26:32 -0700 | [diff] [blame] | 1145 | return JsonMappingException.from(_parser, |
| 1146 | String.format("Can not deserialize instance of %s out of %s token", |
| 1147 | _calcName(targetClass), token)); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1148 | } |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1149 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1150 | /** |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1151 | * @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1152 | */ |
Tatu Saloranta | 48437a7 | 2016-05-09 22:36:15 -0700 | [diff] [blame] | 1153 | @Deprecated |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1154 | public JsonMappingException mappingException(String message) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1155 | return JsonMappingException.from(getParser(), message); |
| 1156 | } |
Cowtowncoder | 5dfc92c | 2015-07-08 17:21:20 -0700 | [diff] [blame] | 1157 | |
| 1158 | /** |
| 1159 | * Helper method for constructing generic mapping exception with specified |
| 1160 | * message and current location information |
| 1161 | * |
| 1162 | * @since 2.6 |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1163 | * |
| 1164 | * @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead |
Cowtowncoder | 5dfc92c | 2015-07-08 17:21:20 -0700 | [diff] [blame] | 1165 | */ |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1166 | @Deprecated |
Cowtowncoder | 5dfc92c | 2015-07-08 17:21:20 -0700 | [diff] [blame] | 1167 | public JsonMappingException mappingException(String msgTemplate, Object... args) { |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1168 | if (args != null && args.length > 0) { |
| 1169 | msgTemplate = String.format(msgTemplate, args); |
| 1170 | } |
| 1171 | return JsonMappingException.from(getParser(), msgTemplate); |
Cowtowncoder | 5dfc92c | 2015-07-08 17:21:20 -0700 | [diff] [blame] | 1172 | } |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1173 | |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1174 | /* |
| 1175 | /********************************************************** |
| 1176 | /* Methods for constructing semantic exceptions; usually not |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1177 | /* to be called directly, call `handleXxx()` instead |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1178 | /********************************************************** |
| 1179 | */ |
| 1180 | |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1181 | /** |
Tatu Saloranta | 98dd091 | 2016-05-15 23:13:44 -0700 | [diff] [blame] | 1182 | * Helper method for constructing {@link JsonMappingException} to indicated |
| 1183 | * that the token encountered was of type different than what <b>should</b> |
| 1184 | * be seen at that position, usually within a sequence of expected tokens. |
| 1185 | * Note that most of the time this method should NOT be directly called; |
| 1186 | * instead, {@link #reportWrongTokenException} should be called and will |
| 1187 | * call this method as necessary. |
| 1188 | */ |
| 1189 | public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken, |
| 1190 | String msg0) |
| 1191 | { |
| 1192 | String msg = String.format("Unexpected token (%s), expected %s", |
| 1193 | p.getCurrentToken(), expToken); |
| 1194 | if (msg0 != null) { |
| 1195 | msg = msg + ": "+msg0; |
| 1196 | } |
| 1197 | return JsonMappingException.from(p, msg); |
| 1198 | } |
| 1199 | |
| 1200 | /** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1201 | * Helper method for constructing exception to indicate that given JSON |
| 1202 | * Object field name was not in format to be able to deserialize specified |
| 1203 | * key type. |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1204 | * Note that most of the time this method should NOT be called; instead, |
| 1205 | * {@link #handleWeirdKey} should be called which will call this method |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1206 | * if necessary. |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1207 | */ |
| 1208 | public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, |
| 1209 | String msg) { |
| 1210 | return InvalidFormatException.from(_parser, |
| 1211 | String.format("Can not deserialize Map key of type %s from String %s: %s", |
| 1212 | keyClass.getName(), _quotedString(keyValue), msg), |
| 1213 | keyValue, keyClass); |
| 1214 | } |
| 1215 | |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1216 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1217 | * Helper method for constructing exception to indicate that input JSON |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1218 | * String was not suitable for deserializing into given target type. |
| 1219 | * Note that most of the time this method should NOT be called; instead, |
| 1220 | * {@link #handleWeirdStringValue} should be called which will call this method |
| 1221 | * if necessary. |
| 1222 | * |
| 1223 | * @param value String value from input being deserialized |
| 1224 | * @param instClass Type that String should be deserialized into |
| 1225 | * @param msg Message that describes specific problem |
| 1226 | * |
| 1227 | * @since 2.1 |
| 1228 | */ |
| 1229 | public JsonMappingException weirdStringException(String value, Class<?> instClass, |
| 1230 | String msg) { |
| 1231 | return InvalidFormatException.from(_parser, |
| 1232 | String.format("Can not deserialize value of type %s from String %s: %s", |
| 1233 | instClass.getName(), _quotedString(value), msg), |
| 1234 | value, instClass); |
| 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Helper method for constructing exception to indicate that input JSON |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1239 | * Number was not suitable for deserializing into given target type. |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1240 | * Note that most of the time this method should NOT be called; instead, |
| 1241 | * {@link #handleWeirdNumberValue} should be called which will call this method |
| 1242 | * if necessary. |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1243 | */ |
| 1244 | public JsonMappingException weirdNumberException(Number value, Class<?> instClass, |
| 1245 | String msg) { |
| 1246 | return InvalidFormatException.from(_parser, |
| 1247 | String.format("Can not deserialize value of type %s from number %s: %s", |
| 1248 | instClass.getName(), String.valueOf(value), msg), |
| 1249 | value, instClass); |
| 1250 | } |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame^] | 1251 | |
| 1252 | /** |
| 1253 | * Helper method for constructing instantiation exception for specified type, |
| 1254 | * to indicate problem with physically constructing instance of |
| 1255 | * specified class (missing constructor, exception from constructor) |
| 1256 | *<p> |
| 1257 | * Note that most of the time this method should NOT be called; instead, |
| 1258 | * {@link #handleInstantiationProblem} should be called which will call this method |
| 1259 | * if necessary. |
| 1260 | */ |
| 1261 | public JsonMappingException instantiationException(Class<?> instClass, Throwable t) { |
| 1262 | return JsonMappingException.from(_parser, |
| 1263 | String.format("Can not construct instance of %s, problem: %s", |
| 1264 | instClass.getName(), t.getMessage()), t); |
| 1265 | } |
| 1266 | |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1267 | /** |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1268 | * Helper method for constructing exception to indicate that given type id |
| 1269 | * could not be resolved to a valid subtype of specified base type, during |
| 1270 | * polymorphic deserialization. |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame^] | 1271 | *<p> |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1272 | * Note that most of the time this method should NOT be called; instead, |
| 1273 | * {@link #handleUnknownTypeId} should be called which will call this method |
| 1274 | * if necessary. |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1275 | */ |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1276 | public JsonMappingException unknownTypeIdException(JavaType baseType, String typeId, |
| 1277 | String extraDesc) { |
| 1278 | String msg = String.format("Could not resolve type id '%s' into a subtype of %s", |
| 1279 | typeId, baseType); |
| 1280 | if (extraDesc != null) { |
| 1281 | msg = msg + ": "+extraDesc; |
| 1282 | } |
| 1283 | return InvalidTypeIdException.from(_parser, msg, baseType, typeId); |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1284 | } |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1285 | |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1286 | /* |
| 1287 | /********************************************************** |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame^] | 1288 | /* Methods for constructing semantic exceptions |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1289 | /********************************************************** |
| 1290 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1291 | |
Tatu Saloranta | 7a4193f | 2016-05-05 12:55:05 -0700 | [diff] [blame] | 1292 | /** |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame^] | 1293 | * @deprecated Since 2.8 use {@link #reportInstantiationException} instead |
| 1294 | */ |
Tatu Saloranta | 7a4193f | 2016-05-05 12:55:05 -0700 | [diff] [blame] | 1295 | @Deprecated |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 1296 | public JsonMappingException instantiationException(Class<?> instClass, String msg) { |
Tatu Saloranta | 8e9d4b2 | 2015-05-01 21:26:32 -0700 | [diff] [blame] | 1297 | return JsonMappingException.from(_parser, |
Tatu Saloranta | 4b9bfd0 | 2016-05-09 20:47:06 -0700 | [diff] [blame] | 1298 | String.format("Can not construct instance of %s: %s", |
| 1299 | instClass.getName(), msg)); |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /** |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1303 | * @since 2.5 |
Tatu Saloranta | 2ffca93 | 2016-05-05 12:23:14 -0700 | [diff] [blame] | 1304 | * |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1305 | * @deprecated Since 2.8 use {@link #handleUnknownTypeId} instead |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1306 | */ |
Tatu Saloranta | 2ffca93 | 2016-05-05 12:23:14 -0700 | [diff] [blame] | 1307 | @Deprecated |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1308 | public JsonMappingException unknownTypeException(JavaType type, String id, |
| 1309 | String extraDesc) { |
Tatu Saloranta | 8c3eb03 | 2016-04-20 22:52:38 -0700 | [diff] [blame] | 1310 | String msg = String.format("Could not resolve type id '%s' into a subtype of %s", |
| 1311 | id, type); |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1312 | if (extraDesc != null) { |
| 1313 | msg = msg + ": "+extraDesc; |
| 1314 | } |
| 1315 | return JsonMappingException.from(_parser, msg); |
| 1316 | } |
| 1317 | |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1318 | /** |
| 1319 | * Helper method for constructing exception to indicate that end-of-input was |
| 1320 | * reached while still expecting more tokens to deserialize value of specified type. |
| 1321 | * |
| 1322 | * @deprecated Since 2.8; currently no way to catch EOF at databind level |
| 1323 | */ |
| 1324 | @Deprecated |
| 1325 | public JsonMappingException endOfInputException(Class<?> instClass) { |
| 1326 | return JsonMappingException.from(_parser, "Unexpected end-of-input when trying to deserialize a " |
| 1327 | +instClass.getName()); |
| 1328 | } |
| 1329 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1330 | /* |
| 1331 | /********************************************************** |
| 1332 | /* Overridable internal methods |
| 1333 | /********************************************************** |
| 1334 | */ |
| 1335 | |
| 1336 | protected DateFormat getDateFormat() |
| 1337 | { |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1338 | if (_dateFormat != null) { |
| 1339 | return _dateFormat; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1340 | } |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1341 | /* 24-Feb-2012, tatu: At this point, all timezone configuration |
Cowtowncoder | 8d0d6e4 | 2015-01-22 15:36:13 -0800 | [diff] [blame] | 1342 | * should have occurred, with respect to default dateformat |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1343 | * and timezone configuration. But we still better clone |
| 1344 | * an instance as formatters may be stateful. |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1345 | */ |
| 1346 | DateFormat df = _config.getDateFormat(); |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1347 | _dateFormat = df = (DateFormat) df.clone(); |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1348 | return df; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 1351 | protected String determineClassName(Object instance) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1352 | return ClassUtil.getClassDescription(instance); |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | /********************************************************** |
| 1357 | /* Other internal methods |
| 1358 | /********************************************************** |
| 1359 | */ |
| 1360 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 1361 | protected String _calcName(Class<?> cls) { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1362 | if (cls.isArray()) { |
| 1363 | return _calcName(cls.getComponentType())+"[]"; |
| 1364 | } |
| 1365 | return cls.getName(); |
| 1366 | } |
Tatu Saloranta | 7a3b0b1 | 2016-02-09 16:52:28 -0800 | [diff] [blame] | 1367 | |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 1368 | protected String _valueDesc() { |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1369 | try { |
| 1370 | return _desc(_parser.getText()); |
| 1371 | } catch (Exception e) { |
| 1372 | return "[N/A]"; |
| 1373 | } |
| 1374 | } |
Tatu Saloranta | 80e1b7e | 2014-01-11 20:11:33 -0800 | [diff] [blame] | 1375 | |
| 1376 | protected String _desc(String desc) { |
Tatu Saloranta | 20a67f7 | 2015-11-21 14:17:45 -0800 | [diff] [blame] | 1377 | if (desc == null) { |
| 1378 | return "[N/A]"; |
| 1379 | } |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1380 | // !!! should we quote it? (in case there are control chars, linefeeds) |
| 1381 | if (desc.length() > MAX_ERROR_STR_LEN) { |
| 1382 | desc = desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN); |
| 1383 | } |
| 1384 | return desc; |
| 1385 | } |
Tatu Saloranta | 20a67f7 | 2015-11-21 14:17:45 -0800 | [diff] [blame] | 1386 | |
| 1387 | // @since 2.7 |
| 1388 | protected String _quotedString(String desc) { |
| 1389 | if (desc == null) { |
| 1390 | return "[N/A]"; |
| 1391 | } |
| 1392 | // !!! should we quote it? (in case there are control chars, linefeeds) |
| 1393 | if (desc.length() > MAX_ERROR_STR_LEN) { |
| 1394 | return String.format("\"%s]...[%s\"", |
| 1395 | desc.substring(0, MAX_ERROR_STR_LEN), |
| 1396 | desc.substring(desc.length() - MAX_ERROR_STR_LEN)); |
| 1397 | } |
| 1398 | return "\"" + desc + "\""; |
| 1399 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1400 | } |