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 | d068a40 | 2016-10-05 23:21:57 -0700 | [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 | 6faae87 | 2016-10-22 19:43:50 -0700 | [diff] [blame] | 12 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 13 | import com.fasterxml.jackson.core.*; |
Tatu Saloranta | 6faae87 | 2016-10-22 19:43:50 -0700 | [diff] [blame] | 14 | |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 15 | import com.fasterxml.jackson.databind.cfg.ContextAttributes; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 16 | import com.fasterxml.jackson.databind.deser.*; |
Tatu Saloranta | 9d1fb75 | 2016-06-07 22:47:04 -0700 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader; |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer; |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; |
Tatu Saloranta | dd34856 | 2012-07-18 17:25:02 -0700 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 24 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
Tatu Saloranta | 39294f9 | 2018-09-04 22:25:50 -0700 | [diff] [blame] | 25 | import com.fasterxml.jackson.databind.exc.ValueInstantiationException; |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.introspect.Annotated; |
Tatu Saloranta | b3e4194 | 2019-09-11 14:18:02 -0700 | [diff] [blame] | 27 | import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
Tatu Saloranta | bb06aa0 | 2016-09-08 22:38:23 -0700 | [diff] [blame] | 28 | import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
Tatu Saloranta | d6230fd | 2016-05-30 21:11:19 -0700 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; |
Tatu Saloranta | 2f82344 | 2011-12-23 20:25:27 -0800 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 32 | import com.fasterxml.jackson.databind.type.TypeFactory; |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 33 | import com.fasterxml.jackson.databind.util.*; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 34 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 35 | /** |
Tatu Saloranta | 97f8482 | 2012-01-29 21:38:40 -0800 | [diff] [blame] | 36 | * Context for the process of deserialization a single root-level value. |
| 37 | * Used to allow passing in configuration settings and reusable temporary |
| 38 | * objects (scrap arrays, containers). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 39 | *<p> |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 40 | * Instance life-cycle is such that a partially configured "blueprint" object |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 41 | * is registered with {@link ObjectMapper} (and {@link ObjectReader}, |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 42 | * and when actual instance is needed for deserialization, |
| 43 | * a fully configured instance will be created using a method in extended internal |
| 44 | * API of sub-class |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 45 | * ({@link com.fasterxml.jackson.databind.deser.DefaultDeserializationContext#createInstance}). |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 46 | * Each instance is guaranteed to only be used from single-threaded context; |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 47 | * instances may be reused if (and only if) no configuration has changed. |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 48 | *<p> |
| 49 | * Defined as abstract class so that implementations must define methods |
| 50 | * for reconfiguring blueprints and creating instances. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 51 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 52 | public abstract class DeserializationContext |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 53 | extends DatabindContext |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 54 | implements java.io.Serializable |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 55 | { |
Cowtowncoder | 7fee92e | 2015-02-11 16:15:08 -0800 | [diff] [blame] | 56 | private static final long serialVersionUID = 1L; // 2.6 |
Tatu Saloranta | 0e11411 | 2012-10-06 10:45:41 -0700 | [diff] [blame] | 57 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 58 | /* |
| 59 | /********************************************************** |
| 60 | /* Configuration, immutable |
| 61 | /********************************************************** |
| 62 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 63 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Object that handle details of {@link JsonDeserializer} caching. |
| 66 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 67 | protected final DeserializerCache _cache; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 68 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 69 | /* |
| 70 | /********************************************************** |
| 71 | /* Configuration, changeable via fluent factories |
| 72 | /********************************************************** |
| 73 | */ |
| 74 | |
| 75 | /** |
| 76 | * Read-only factory instance; exposed to let |
| 77 | * owners (<code>ObjectMapper</code>, <code>ObjectReader</code>) |
| 78 | * access it. |
| 79 | */ |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 80 | protected final DeserializerFactory _factory; |
| 81 | |
| 82 | /* |
| 83 | /********************************************************** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 84 | /* Configuration that gets set for instances (not blueprints) |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 85 | /* (partly denormalized for performance) |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 86 | /********************************************************** |
| 87 | */ |
| 88 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Generic deserialization processing configuration |
| 91 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 92 | protected final DeserializationConfig _config; |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Bitmap of {@link DeserializationFeature}s that are enabled |
| 96 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 97 | protected final int _featureFlags; |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 98 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Currently active view, if any. |
| 101 | */ |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 102 | protected final Class<?> _view; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Currently active parser used for deserialization. |
| 106 | * May be different from the outermost parser |
| 107 | * when content is buffered. |
| 108 | */ |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 109 | protected transient JsonParser _parser; |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 110 | |
Tatu Saloranta | 71e876b | 2012-02-05 19:15:48 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Object used for resolving references to injectable |
| 113 | * values. |
| 114 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 115 | protected final InjectableValues _injectableValues; |
| 116 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 117 | /* |
| 118 | /********************************************************** |
| 119 | /* Per-operation reusable helper objects (not for blueprints) |
| 120 | /********************************************************** |
| 121 | */ |
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 ArrayBuilders _arrayBuilders; |
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 ObjectBuffer _objectBuffer; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 126 | |
Tatu Saloranta | f521158 | 2012-10-05 17:09:47 -0700 | [diff] [blame] | 127 | protected transient DateFormat _dateFormat; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Lazily-constructed holder for per-call attributes. |
| 131 | * |
| 132 | * @since 2.3 |
| 133 | */ |
| 134 | protected transient ContextAttributes _attributes; |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Type of {@link JsonDeserializer} (or, more specifically, |
Cowtowncoder | 08efeba | 2015-05-05 12:34:11 -0700 | [diff] [blame] | 138 | * {@link ContextualDeserializer}) that is being |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 139 | * contextualized currently. |
| 140 | * |
| 141 | * @since 2.5 |
| 142 | */ |
| 143 | protected LinkedNode<JavaType> _currentType; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | /********************************************************** |
| 147 | /* Life-cycle |
| 148 | /********************************************************** |
| 149 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 150 | |
| 151 | protected DeserializationContext(DeserializerFactory df) { |
| 152 | this(df, null); |
| 153 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 154 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 155 | protected DeserializationContext(DeserializerFactory df, |
| 156 | DeserializerCache cache) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 157 | { |
Tatu Saloranta | 0238e9d | 2019-09-12 12:53:16 -0700 | [diff] [blame] | 158 | _factory = Objects.requireNonNull(df, "Cannot pass null DeserializerFactory"); |
Tatu Saloranta | 43fbd71 | 2016-11-09 21:43:27 -0800 | [diff] [blame] | 159 | if (cache == null) { |
| 160 | cache = new DeserializerCache(); |
| 161 | } |
| 162 | _cache = cache; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 163 | _featureFlags = 0; |
| 164 | _config = null; |
| 165 | _injectableValues = null; |
| 166 | _view = null; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 167 | _attributes = null; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | protected DeserializationContext(DeserializationContext src, |
| 171 | DeserializerFactory factory) |
| 172 | { |
| 173 | _cache = src._cache; |
| 174 | _factory = factory; |
| 175 | |
| 176 | _config = src._config; |
| 177 | _featureFlags = src._featureFlags; |
| 178 | _view = src._view; |
| 179 | _parser = src._parser; |
| 180 | _injectableValues = src._injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 181 | _attributes = src._attributes; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 182 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Constructor used for creating actual per-call instances. |
| 186 | */ |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 187 | protected DeserializationContext(DeserializationContext src, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 188 | DeserializationConfig config, JsonParser p, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 189 | InjectableValues injectableValues) |
| 190 | { |
| 191 | _cache = src._cache; |
| 192 | _factory = src._factory; |
| 193 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 194 | _config = config; |
Tatu Saloranta | e3ae58e | 2012-01-28 23:08:16 -0800 | [diff] [blame] | 195 | _featureFlags = config.getDeserializationFeatures(); |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 196 | _view = config.getActiveView(); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 197 | _parser = p; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 198 | _injectableValues = injectableValues; |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 199 | _attributes = config.getAttributes(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 200 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 201 | |
Tatu Saloranta | 23328aa | 2014-10-28 22:38:15 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Copy-constructor for use with <code>copy()</code> by {@link ObjectMapper#copy()} |
| 204 | */ |
| 205 | protected DeserializationContext(DeserializationContext src) { |
| 206 | _cache = new DeserializerCache(); |
| 207 | _factory = src._factory; |
| 208 | |
| 209 | _config = src._config; |
| 210 | _featureFlags = src._featureFlags; |
| 211 | _view = src._view; |
| 212 | _injectableValues = null; |
| 213 | } |
| 214 | |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 215 | /* |
| 216 | /********************************************************** |
| 217 | /* DatabindContext implementation |
| 218 | /********************************************************** |
| 219 | */ |
| 220 | |
| 221 | @Override |
| 222 | public DeserializationConfig getConfig() { return _config; } |
| 223 | |
| 224 | @Override |
| 225 | public final Class<?> getActiveView() { return _view; } |
| 226 | |
| 227 | @Override |
Tatu Saloranta | a5301ba | 2015-12-11 09:47:09 -0800 | [diff] [blame] | 228 | public final boolean canOverrideAccessModifiers() { |
| 229 | return _config.canOverrideAccessModifiers(); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public final boolean isEnabled(MapperFeature feature) { |
| 234 | return _config.isEnabled(feature); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public final JsonFormat.Value getDefaultPropertyFormat(Class<?> baseType) { |
| 239 | return _config.getDefaultPropertyFormat(baseType); |
| 240 | } |
Tatu Saloranta | 4fdbb69 | 2016-04-16 21:41:14 -0700 | [diff] [blame] | 241 | |
Tatu Saloranta | a5301ba | 2015-12-11 09:47:09 -0800 | [diff] [blame] | 242 | @Override |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 243 | public final AnnotationIntrospector getAnnotationIntrospector() { |
| 244 | return _config.getAnnotationIntrospector(); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public final TypeFactory getTypeFactory() { |
| 249 | return _config.getTypeFactory(); |
| 250 | } |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 251 | |
Cowtowncoder | 41bbc96 | 2015-06-19 17:16:08 -0700 | [diff] [blame] | 252 | /** |
| 253 | * Method for accessing default Locale to use: convenience method for |
| 254 | *<pre> |
| 255 | * getConfig().getLocale(); |
| 256 | *</pre> |
| 257 | */ |
| 258 | @Override |
| 259 | public Locale getLocale() { |
| 260 | return _config.getLocale(); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Method for accessing default TimeZone to use: convenience method for |
| 265 | *<pre> |
| 266 | * getConfig().getTimeZone(); |
| 267 | *</pre> |
| 268 | */ |
| 269 | @Override |
| 270 | public TimeZone getTimeZone() { |
| 271 | return _config.getTimeZone(); |
| 272 | } |
| 273 | |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 274 | /* |
| 275 | /********************************************************** |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 276 | /* Access to per-call state, like generic attributes (2.3+) |
Tatu Saloranta | 2f26e6a | 2013-10-06 22:05:32 -0700 | [diff] [blame] | 277 | /********************************************************** |
| 278 | */ |
| 279 | |
| 280 | @Override |
| 281 | public Object getAttribute(Object key) { |
| 282 | return _attributes.getAttribute(key); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public DeserializationContext setAttribute(Object key, Object value) |
| 287 | { |
| 288 | _attributes = _attributes.withPerCallAttribute(key, value); |
| 289 | return this; |
| 290 | } |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * Accessor to {@link JavaType} of currently contextualized |
| 294 | * {@link ContextualDeserializer}, if any. |
| 295 | * This is sometimes useful for generic {@link JsonDeserializer}s that |
| 296 | * do not get passed (or do not retain) type information when being |
| 297 | * constructed: happens for example for deserializers constructed |
| 298 | * from annotations. |
| 299 | * |
| 300 | * @since 2.5 |
| 301 | * |
| 302 | * @return Type of {@link ContextualDeserializer} being contextualized, |
| 303 | * if process is on-going; null if not. |
| 304 | */ |
| 305 | public JavaType getContextualType() { |
| 306 | return (_currentType == null) ? null : _currentType.value(); |
| 307 | } |
| 308 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 309 | /* |
| 310 | /********************************************************** |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 311 | /* Public API, config setting accessors |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 312 | /********************************************************** |
| 313 | */ |
| 314 | |
| 315 | /** |
Dmitry Katsubo | e70c66f | 2012-09-03 16:00:30 +0200 | [diff] [blame] | 316 | * Method for getting current {@link DeserializerFactory}. |
| 317 | */ |
| 318 | public DeserializerFactory getFactory() { |
| 319 | return _factory; |
| 320 | } |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 321 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 322 | /** |
| 323 | * Convenience method for checking whether specified on/off |
| 324 | * feature is enabled |
| 325 | */ |
Tatu | 9610aff | 2012-02-02 11:30:08 -0800 | [diff] [blame] | 326 | public final boolean isEnabled(DeserializationFeature feat) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 327 | /* 03-Dec-2010, tatu: minor shortcut; since this is called quite often, |
| 328 | * let's use a local copy of feature settings: |
| 329 | */ |
| 330 | return (_featureFlags & feat.getMask()) != 0; |
| 331 | } |
| 332 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 333 | /** |
Cowtowncoder | 23e52ab | 2015-05-19 17:53:26 -0700 | [diff] [blame] | 334 | * Bulk access method for getting the bit mask of all {@link DeserializationFeature}s |
| 335 | * that are enabled. |
| 336 | * |
| 337 | * @since 2.6 |
| 338 | */ |
| 339 | public final int getDeserializationFeatures() { |
| 340 | return _featureFlags; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Bulk access method for checking that all features specified by |
Tatu Saloranta | 0ac36ba | 2013-08-21 18:08:51 -0700 | [diff] [blame] | 345 | * mask are enabled. |
| 346 | * |
| 347 | * @since 2.3 |
| 348 | */ |
| 349 | public final boolean hasDeserializationFeatures(int featureMask) { |
Cowtowncoder | 23e52ab | 2015-05-19 17:53:26 -0700 | [diff] [blame] | 350 | return (_featureFlags & featureMask) == featureMask; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Bulk access method for checking that at least one of features specified by |
| 355 | * mask is enabled. |
| 356 | * |
| 357 | * @since 2.6 |
| 358 | */ |
| 359 | public final boolean hasSomeOfFeatures(int featureMask) { |
| 360 | return (_featureFlags & featureMask) != 0; |
Tatu Saloranta | 0ac36ba | 2013-08-21 18:08:51 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 364 | * Method for accessing the currently active parser. |
| 365 | * May be different from the outermost parser |
| 366 | * when content is buffered. |
| 367 | *<p> |
| 368 | * Use of this method is discouraged: if code has direct access |
| 369 | * to the active parser, that should be used instead. |
| 370 | */ |
| 371 | public final JsonParser getParser() { return _parser; } |
| 372 | |
| 373 | public final Object findInjectableValue(Object valueId, |
| 374 | BeanProperty forProperty, Object beanInstance) |
Tatu Saloranta | 6844037 | 2016-10-29 17:06:05 -0700 | [diff] [blame] | 375 | throws JsonMappingException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 376 | { |
| 377 | if (_injectableValues == null) { |
Tatu Saloranta | 6844037 | 2016-10-29 17:06:05 -0700 | [diff] [blame] | 378 | reportBadDefinition(ClassUtil.classOf(valueId), String.format( |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 379 | "No 'injectableValues' configured, cannot inject value with id [%s]", valueId)); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 380 | } |
| 381 | return _injectableValues.findInjectableValue(valueId, this, forProperty, beanInstance); |
| 382 | } |
Tatu Saloranta | b2d3c7d | 2012-01-27 21:41:48 -0800 | [diff] [blame] | 383 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 384 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 385 | * Convenience method for accessing the default Base64 encoding |
| 386 | * used for decoding base64 encoded binary content. |
| 387 | * Same as calling: |
| 388 | *<pre> |
| 389 | * getConfig().getBase64Variant(); |
| 390 | *</pre> |
| 391 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 392 | public final Base64Variant getBase64Variant() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 393 | return _config.getBase64Variant(); |
| 394 | } |
| 395 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 396 | /** |
| 397 | * Convenience method, functionally equivalent to: |
| 398 | *<pre> |
| 399 | * getConfig().getNodeFactory(); |
| 400 | * </pre> |
| 401 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 402 | public final JsonNodeFactory getNodeFactory() { |
| 403 | return _config.getNodeFactory(); |
| 404 | } |
| 405 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 406 | /* |
| 407 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 408 | /* Public API, pass-through to DeserializerCache |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 409 | /********************************************************** |
| 410 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 411 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 412 | /** |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 413 | * Method for checking whether we could find a deserializer |
| 414 | * for given type. |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 415 | * |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 416 | * @param type |
| 417 | * @since 2.3 |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 418 | */ |
Tatu Saloranta | af263c3 | 2013-10-24 16:18:50 -0700 | [diff] [blame] | 419 | public boolean hasValueDeserializerFor(JavaType type, AtomicReference<Throwable> cause) { |
| 420 | try { |
| 421 | return _cache.hasValueDeserializerFor(this, _factory, type); |
| 422 | } catch (JsonMappingException e) { |
| 423 | if (cause != null) { |
| 424 | cause.set(e); |
| 425 | } |
| 426 | } catch (RuntimeException e) { |
| 427 | if (cause == null) { // earlier behavior |
| 428 | throw e; |
| 429 | } |
| 430 | cause.set(e); |
| 431 | } |
| 432 | return false; |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 435 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 436 | * Method for finding a value deserializer, and creating a contextual |
| 437 | * version if necessary, for value reached via specified property. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 438 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 439 | @SuppressWarnings("unchecked") |
| 440 | public final JsonDeserializer<Object> findContextualValueDeserializer(JavaType type, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 441 | BeanProperty prop) throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 442 | { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 443 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 444 | if (deser != null) { |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 445 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, prop, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 446 | } |
| 447 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 448 | } |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 449 | |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 450 | /** |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 451 | * Variant that will try to locate deserializer for current type, but without |
| 452 | * performing any contextualization (unlike {@link #findContextualValueDeserializer}) |
| 453 | * or checking for need to create a {@link TypeDeserializer} (unlike |
| 454 | * {@link #findRootValueDeserializer(JavaType)}. |
| 455 | * This method is usually called from within {@link ResolvableDeserializer#resolve}, |
| 456 | * and expectation is that caller then calls either |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 457 | * {@link #handlePrimaryContextualization(JsonDeserializer, BeanProperty, JavaType)} or |
| 458 | * {@link #handleSecondaryContextualization(JsonDeserializer, BeanProperty, JavaType)} at a |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 459 | * later point, as necessary. |
| 460 | * |
| 461 | * @since 2.5 |
| 462 | */ |
| 463 | public final JsonDeserializer<Object> findNonContextualValueDeserializer(JavaType type) |
| 464 | throws JsonMappingException |
| 465 | { |
| 466 | return _cache.findValueDeserializer(this, _factory, type); |
| 467 | } |
| 468 | |
| 469 | /** |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 470 | * Method for finding a deserializer for root-level value. |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 471 | */ |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 472 | @SuppressWarnings("unchecked") |
Tatu Saloranta | 9b9d043 | 2012-01-30 09:20:26 -0800 | [diff] [blame] | 473 | public final JsonDeserializer<Object> findRootValueDeserializer(JavaType type) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 474 | throws JsonMappingException |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 475 | { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 476 | JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, |
| 477 | _factory, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 478 | if (deser == null) { // can this occur? |
| 479 | return null; |
| 480 | } |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 481 | deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, null, type); |
Tatu | c3a73d0 | 2012-01-31 12:45:49 -0800 | [diff] [blame] | 482 | TypeDeserializer typeDeser = _factory.findTypeDeserializer(_config, type); |
| 483 | if (typeDeser != null) { |
| 484 | // important: contextualize to indicate this is for root value |
| 485 | typeDeser = typeDeser.forProperty(null); |
| 486 | return new TypeWrappedDeserializer(typeDeser, deser); |
| 487 | } |
| 488 | return deser; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 489 | } |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 490 | |
| 491 | /** |
| 492 | * Convenience method, functionally same as: |
| 493 | *<pre> |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 494 | * getDeserializerProvider().findKeyDeserializer(getConfig(), prop.getType(), prop); |
Tatu Saloranta | e8dc603 | 2012-01-23 22:43:26 -0800 | [diff] [blame] | 495 | *</pre> |
| 496 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 497 | public final KeyDeserializer findKeyDeserializer(JavaType keyType, |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 498 | BeanProperty prop) throws JsonMappingException { |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 499 | KeyDeserializer kd = _cache.findKeyDeserializer(this, |
| 500 | _factory, keyType); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 501 | // Second: contextualize? |
| 502 | if (kd instanceof ContextualKeyDeserializer) { |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 503 | kd = ((ContextualKeyDeserializer) kd).createContextual(this, prop); |
Tatu | d0bb315 | 2012-01-31 13:04:06 -0800 | [diff] [blame] | 504 | } |
| 505 | return kd; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Tatu | b37ff33 | 2012-01-24 16:19:36 -0800 | [diff] [blame] | 508 | /* |
| 509 | /********************************************************** |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 510 | /* Public API, ObjectId handling |
| 511 | /********************************************************** |
| 512 | */ |
| 513 | |
| 514 | /** |
| 515 | * Method called to find and return entry corresponding to given |
| 516 | * Object Id: will add an entry if necessary, and never returns null |
| 517 | */ |
Pascal GĂ©linas | 184cae3 | 2014-02-05 17:35:56 -0500 | [diff] [blame] | 518 | public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> generator, ObjectIdResolver resolver); |
| 519 | |
Pascal GĂ©linas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 520 | /** |
| 521 | * Method called to ensure that every object id encounter during processing |
| 522 | * are resolved. |
| 523 | * |
| 524 | * @throws UnresolvedForwardReference |
| 525 | */ |
| 526 | public abstract void checkUnresolvedObjectId() |
| 527 | throws UnresolvedForwardReference; |
| 528 | |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 529 | /* |
| 530 | /********************************************************** |
| 531 | /* Public API, type handling |
| 532 | /********************************************************** |
| 533 | */ |
| 534 | |
| 535 | /** |
| 536 | * Convenience method, functionally equivalent to: |
| 537 | *<pre> |
| 538 | * getConfig().constructType(cls); |
| 539 | * </pre> |
| 540 | */ |
| 541 | public final JavaType constructType(Class<?> cls) { |
Tatu Saloranta | 816bbed | 2016-10-05 21:13:12 -0700 | [diff] [blame] | 542 | return (cls == null) ? null : _config.constructType(cls); |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
Cowtowncoder | a5bd0a2 | 2015-06-25 15:27:07 -0700 | [diff] [blame] | 546 | * Helper method that is to be used when resolving basic class name into |
| 547 | * Class instance, the reason being that it may be necessary to work around |
| 548 | * various ClassLoader limitations, as well as to handle primitive type |
| 549 | * signatures. |
| 550 | * |
| 551 | * @since 2.6 |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 552 | */ |
| 553 | public Class<?> findClass(String className) throws ClassNotFoundException |
| 554 | { |
| 555 | // By default, delegate to ClassUtil: can be overridden with custom handling |
Cowtowncoder | a5bd0a2 | 2015-06-25 15:27:07 -0700 | [diff] [blame] | 556 | return getTypeFactory().findClass(className); |
Tatu Saloranta | bf5e0fa | 2012-03-08 21:45:30 -0800 | [diff] [blame] | 557 | } |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 558 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 559 | /* |
| 560 | /********************************************************** |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 561 | /* Public API, helper object recycling |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 562 | /********************************************************** |
| 563 | */ |
| 564 | |
| 565 | /** |
| 566 | * Method that can be used to get access to a reusable ObjectBuffer, |
| 567 | * useful for efficiently constructing Object arrays and Lists. |
| 568 | * Note that leased buffers should be returned once deserializer |
| 569 | * is done, to allow for reuse during same round of deserialization. |
| 570 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 571 | public final ObjectBuffer leaseObjectBuffer() |
| 572 | { |
| 573 | ObjectBuffer buf = _objectBuffer; |
| 574 | if (buf == null) { |
| 575 | buf = new ObjectBuffer(); |
| 576 | } else { |
| 577 | _objectBuffer = null; |
| 578 | } |
| 579 | return buf; |
| 580 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * Method to call to return object buffer previously leased with |
| 584 | * {@link #leaseObjectBuffer}. |
| 585 | * |
| 586 | * @param buf Returned object buffer |
| 587 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 588 | public final void returnObjectBuffer(ObjectBuffer buf) |
| 589 | { |
| 590 | /* Already have a reusable buffer? Let's retain bigger one |
| 591 | * (or if equal, favor newer one, shorter life-cycle) |
| 592 | */ |
| 593 | if (_objectBuffer == null |
| 594 | || buf.initialCapacity() >= _objectBuffer.initialCapacity()) { |
| 595 | _objectBuffer = buf; |
| 596 | } |
| 597 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 598 | |
| 599 | /** |
| 600 | * Method for accessing object useful for building arrays of |
| 601 | * primitive types (such as int[]). |
| 602 | */ |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 603 | public final ArrayBuilders getArrayBuilders() |
| 604 | { |
| 605 | if (_arrayBuilders == null) { |
| 606 | _arrayBuilders = new ArrayBuilders(); |
| 607 | } |
| 608 | return _arrayBuilders; |
| 609 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | /********************************************************** |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 613 | /* Extended API: handler instantiation |
| 614 | /********************************************************** |
| 615 | */ |
| 616 | |
| 617 | public abstract JsonDeserializer<Object> deserializerInstance(Annotated annotated, |
| 618 | Object deserDef) |
| 619 | throws JsonMappingException; |
| 620 | |
| 621 | public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated, |
| 622 | Object deserDef) |
| 623 | throws JsonMappingException; |
| 624 | |
| 625 | /* |
| 626 | /********************************************************** |
| 627 | /* Extended API: resolving contextual deserializers; called |
| 628 | /* by structured deserializers for their value/component |
| 629 | /* deserializers |
| 630 | /********************************************************** |
| 631 | */ |
| 632 | |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 633 | /** |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 634 | * Method called for primary property deserializers (ones |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 635 | * directly created to deserialize values of a POJO property), |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 636 | * to handle details of resolving |
| 637 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 638 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 639 | * @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] | 640 | * |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 641 | * @since 2.5 |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 642 | */ |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 643 | public JsonDeserializer<?> handlePrimaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 644 | BeanProperty prop, JavaType type) |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 645 | throws JsonMappingException |
| 646 | { |
Cowtowncoder | a2e2720 | 2014-11-25 17:07:52 -0800 | [diff] [blame] | 647 | if (deser instanceof ContextualDeserializer) { |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 648 | _currentType = new LinkedNode<JavaType>(type, _currentType); |
| 649 | try { |
| 650 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
| 651 | } finally { |
| 652 | _currentType = _currentType.next(); |
| 653 | } |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 654 | } |
| 655 | return deser; |
| 656 | } |
Tatu Saloranta | b530c4d | 2013-09-04 22:20:13 -0700 | [diff] [blame] | 657 | |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 658 | /** |
| 659 | * Method called for secondary property deserializers (ones |
| 660 | * NOT directly created to deal with an annotatable POJO property, |
| 661 | * but instead created as a component -- such as value deserializers |
| 662 | * for structured types, or deserializers for root values) |
| 663 | * to handle details of resolving |
| 664 | * {@link ContextualDeserializer} with given property context. |
Tatu Saloranta | 59fe29c | 2013-09-05 22:37:06 -0700 | [diff] [blame] | 665 | * Given that these deserializers are not directly related to given property |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 666 | * (or, in case of root value property, to any property), annotations |
| 667 | * accessible may or may not be relevant. |
| 668 | * |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 669 | * @param prop Property for which deserializer is used, if any; null |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 670 | * when deserializing root values |
| 671 | * |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 672 | * @since 2.5 |
Tatu Saloranta | 4f90dbc | 2013-09-05 21:45:53 -0700 | [diff] [blame] | 673 | */ |
| 674 | public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser, |
Tatu Saloranta | c4b6c61 | 2014-12-26 22:07:54 -0800 | [diff] [blame] | 675 | BeanProperty prop, JavaType type) |
| 676 | throws JsonMappingException |
| 677 | { |
| 678 | if (deser instanceof ContextualDeserializer) { |
| 679 | _currentType = new LinkedNode<JavaType>(type, _currentType); |
| 680 | try { |
| 681 | deser = ((ContextualDeserializer) deser).createContextual(this, prop); |
| 682 | } finally { |
| 683 | _currentType = _currentType.next(); |
| 684 | } |
| 685 | } |
| 686 | return deser; |
| 687 | } |
| 688 | |
Tatu Saloranta | f72fe21 | 2013-09-04 22:07:32 -0700 | [diff] [blame] | 689 | /* |
| 690 | /********************************************************** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 691 | /* Parsing methods that may use reusable/-cyclable objects |
| 692 | /********************************************************** |
| 693 | */ |
| 694 | |
| 695 | /** |
| 696 | * Convenience method for parsing a Date from given String, using |
| 697 | * currently configured date format (accessed using |
| 698 | * {@link DeserializationConfig#getDateFormat()}). |
| 699 | *<p> |
| 700 | * Implementation will handle thread-safety issues related to |
| 701 | * date formats such that first time this method is called, |
| 702 | * date format is cloned, and cloned instance will be retained |
| 703 | * for use during this deserialization round. |
| 704 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 705 | public Date parseDate(String dateStr) throws IllegalArgumentException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 706 | { |
| 707 | try { |
Tatu Saloranta | ef00ac6 | 2013-11-03 09:56:39 -0800 | [diff] [blame] | 708 | DateFormat df = getDateFormat(); |
| 709 | return df.parse(dateStr); |
Tatu Saloranta | aa51274 | 2012-02-22 22:51:33 -0800 | [diff] [blame] | 710 | } catch (ParseException e) { |
Tatu Saloranta | 8e9d4b2 | 2015-05-01 21:26:32 -0700 | [diff] [blame] | 711 | throw new IllegalArgumentException(String.format( |
Tatu Saloranta | f6cf181 | 2018-09-05 23:17:05 -0700 | [diff] [blame] | 712 | "Failed to parse Date value '%s': %s", dateStr, |
| 713 | ClassUtil.exceptionMessage(e))); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 716 | |
| 717 | /** |
| 718 | * Convenience method for constructing Calendar instance set |
| 719 | * to specified time, to be modified and used by caller. |
| 720 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 721 | public Calendar constructCalendar(Date d) { |
| 722 | // 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] | 723 | Calendar c = Calendar.getInstance(getTimeZone()); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 724 | c.setTime(d); |
| 725 | return c; |
| 726 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 727 | |
| 728 | /* |
| 729 | /********************************************************** |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 730 | /* Convenience methods for reading parsed values |
| 731 | /********************************************************** |
| 732 | */ |
| 733 | |
| 734 | /** |
| 735 | * Convenience method that may be used by composite or container deserializers, |
| 736 | * for reading one-off values contained (for sequences, it is more efficient |
| 737 | * to actually fetch deserializer once for the whole collection). |
| 738 | *<p> |
| 739 | * NOTE: when deserializing values of properties contained in composite types, |
| 740 | * rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)}; |
| 741 | * this method does not allow use of contextual annotations. |
| 742 | * |
| 743 | * @since 2.4 |
| 744 | */ |
| 745 | public <T> T readValue(JsonParser p, Class<T> type) throws IOException { |
| 746 | return readValue(p, getTypeFactory().constructType(type)); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * @since 2.4 |
| 751 | */ |
| 752 | @SuppressWarnings("unchecked") |
| 753 | public <T> T readValue(JsonParser p, JavaType type) throws IOException { |
| 754 | JsonDeserializer<Object> deser = findRootValueDeserializer(type); |
| 755 | if (deser == null) { |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 756 | reportBadDefinition(type, |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 757 | "Could not find JsonDeserializer for type "+ClassUtil.getTypeDescription(type)); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 758 | } |
| 759 | return (T) deser.deserialize(p, this); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Convenience method that may be used by composite or container deserializers, |
| 764 | * for reading one-off values for the composite type, taking into account |
| 765 | * annotations that the property (passed to this method -- usually property that |
| 766 | * has custom serializer that called this method) has. |
| 767 | * |
| 768 | * @since 2.4 |
| 769 | */ |
| 770 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, Class<T> type) throws IOException { |
| 771 | return readPropertyValue(p, prop, getTypeFactory().constructType(type)); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * @since 2.4 |
| 776 | */ |
| 777 | @SuppressWarnings("unchecked") |
| 778 | public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException { |
| 779 | JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop); |
| 780 | if (deser == null) { |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 781 | return reportBadDefinition(type, String.format( |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 782 | "Could not find JsonDeserializer for type %s (via property %s)", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 783 | ClassUtil.getTypeDescription(type), ClassUtil.nameOf(prop))); |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 784 | } |
| 785 | return (T) deser.deserialize(p, this); |
| 786 | } |
Tatu Saloranta | 5ae6e4b | 2015-10-05 21:49:01 -0700 | [diff] [blame] | 787 | |
Tatu Saloranta | 7904f92 | 2019-09-11 15:03:50 -0700 | [diff] [blame] | 788 | /** |
| 789 | * @since 2.10 |
| 790 | */ |
| 791 | public JsonNode readTree(JsonParser p) throws IOException { |
| 792 | JsonToken t = p.currentToken(); |
| 793 | if (t == null) { |
| 794 | t = p.nextToken(); |
| 795 | if (t == null) { |
| 796 | return getNodeFactory().missingNode(); |
| 797 | } |
| 798 | } |
| 799 | if (t == JsonToken.VALUE_NULL) { |
| 800 | return getNodeFactory().nullNode(); |
| 801 | } |
| 802 | return (JsonNode) findRootValueDeserializer(_config.constructType(JsonNode.class)) |
| 803 | .deserialize(p, this); |
| 804 | } |
| 805 | |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 806 | /* |
| 807 | /********************************************************** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 808 | /* Methods for problem handling |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 809 | /********************************************************** |
| 810 | */ |
| 811 | |
| 812 | /** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 813 | * Method that deserializers should call if they encounter an unrecognized |
| 814 | * property (and once that is not explicitly designed as ignorable), to |
| 815 | * inform possibly configured {@link DeserializationProblemHandler}s and |
| 816 | * let it handle the problem. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 817 | * |
| 818 | * @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] | 819 | * problem |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 820 | */ |
Tatu Saloranta | 478d4a4 | 2014-03-16 21:53:33 -0700 | [diff] [blame] | 821 | public boolean handleUnknownProperty(JsonParser p, JsonDeserializer<?> deser, |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 822 | Object instanceOrClass, String propName) |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 823 | throws IOException |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 824 | { |
| 825 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 826 | while (h != null) { |
| 827 | // Can bail out if it's handled |
| 828 | if (h.value().handleUnknownProperty(this, p, deser, instanceOrClass, propName)) { |
| 829 | return true; |
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 | h = h.next(); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 832 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 833 | // Nope, not handled. Potentially that's a problem... |
| 834 | if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { |
| 835 | p.skipChildren(); |
| 836 | return true; |
| 837 | } |
| 838 | // Do we know properties that are expected instead? |
| 839 | Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); |
| 840 | throw UnrecognizedPropertyException.from(_parser, |
| 841 | instanceOrClass, propName, propIds); |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 842 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 843 | |
| 844 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 845 | * Method that deserializers should call if they encounter a String value |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 846 | * that cannot be converted to expected key of a {@link java.util.Map} |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 847 | * valued property. |
| 848 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue} |
| 849 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 850 | * succeed, will throw {@link InvalidFormatException} with given message. |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 851 | * |
| 852 | * @param keyClass Expected type for key |
| 853 | * @param keyValue String value from which to deserialize key |
| 854 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 855 | * @param msgArgs Optional arguments to use for message, if any |
| 856 | * |
| 857 | * @return Key value to use |
| 858 | * |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 859 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 860 | * |
| 861 | * @since 2.8 |
| 862 | */ |
| 863 | public Object handleWeirdKey(Class<?> keyClass, String keyValue, |
| 864 | String msg, Object... msgArgs) |
| 865 | throws IOException |
| 866 | { |
| 867 | // but if not handled, just throw exception |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 868 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 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 | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 874 | // Sanity check for broken handlers, otherwise nasty to debug: |
| 875 | if ((key == null) || keyClass.isInstance(key)) { |
| 876 | return key; |
| 877 | } |
| 878 | throw weirdStringException(keyValue, keyClass, String.format( |
| 879 | "DeserializationProblemHandler.handleWeirdStringValue() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 880 | ClassUtil.getClassDescription(keyClass), |
| 881 | ClassUtil.getClassDescription(key) |
| 882 | )); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 883 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 884 | h = h.next(); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 885 | } |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 886 | throw weirdKeyException(keyClass, keyValue, msg); |
| 887 | } |
| 888 | |
| 889 | /** |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 890 | * Method that deserializers should call if they encounter a String value |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 891 | * that cannot be converted to target property type, in cases where some |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 892 | * String values could be acceptable (either with different settings, |
| 893 | * or different value). |
| 894 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdStringValue} |
| 895 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 896 | * succeed, will throw {@link InvalidFormatException} with given message. |
| 897 | * |
| 898 | * @param targetClass Type of property into which incoming number should be converted |
| 899 | * @param value String value from which to deserialize property value |
| 900 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 901 | * @param msgArgs Optional arguments to use for message, if any |
| 902 | * |
| 903 | * @return Property value to use |
| 904 | * |
| 905 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
| 906 | * |
| 907 | * @since 2.8 |
| 908 | */ |
| 909 | public Object handleWeirdStringValue(Class<?> targetClass, String value, |
| 910 | String msg, Object... msgArgs) |
| 911 | throws IOException |
| 912 | { |
| 913 | // but if not handled, just throw exception |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 914 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 915 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 916 | while (h != null) { |
| 917 | // Can bail out if it's handled |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 918 | Object instance = h.value().handleWeirdStringValue(this, targetClass, value, msg); |
| 919 | if (instance != DeserializationProblemHandler.NOT_HANDLED) { |
| 920 | // Sanity check for broken handlers, otherwise nasty to debug: |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 921 | if (_isCompatible(targetClass, instance)) { |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 922 | return instance; |
| 923 | } |
| 924 | throw weirdStringException(value, targetClass, String.format( |
| 925 | "DeserializationProblemHandler.handleWeirdStringValue() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 926 | ClassUtil.getClassDescription(targetClass), |
| 927 | ClassUtil.getClassDescription(instance) |
| 928 | )); |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 929 | } |
| 930 | h = h.next(); |
| 931 | } |
| 932 | throw weirdStringException(value, targetClass, msg); |
| 933 | } |
| 934 | |
| 935 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 936 | * Method that deserializers should call if they encounter a numeric value |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 937 | * that cannot be converted to target property type, in cases where some |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 938 | * numeric values could be acceptable (either with different settings, |
| 939 | * or different numeric value). |
| 940 | * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue} |
| 941 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 942 | * succeed, will throw {@link InvalidFormatException} with given message. |
| 943 | * |
| 944 | * @param targetClass Type of property into which incoming number should be converted |
| 945 | * @param value Number value from which to deserialize property value |
| 946 | * @param msg Error message template caller wants to use if exception is to be thrown |
| 947 | * @param msgArgs Optional arguments to use for message, if any |
| 948 | * |
| 949 | * @return Property value to use |
| 950 | * |
| 951 | * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code> |
| 952 | * |
| 953 | * @since 2.8 |
| 954 | */ |
| 955 | public Object handleWeirdNumberValue(Class<?> targetClass, Number value, |
| 956 | String msg, Object... msgArgs) |
| 957 | throws IOException |
| 958 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 959 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 960 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 961 | while (h != null) { |
| 962 | // Can bail out if it's handled |
| 963 | Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg); |
| 964 | if (key != DeserializationProblemHandler.NOT_HANDLED) { |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 965 | // Sanity check for broken handlers, otherwise nasty to debug: |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 966 | if (_isCompatible(targetClass, key)) { |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 967 | return key; |
| 968 | } |
Tatu Saloranta | 23a610d | 2017-07-26 16:57:29 -0700 | [diff] [blame] | 969 | throw weirdNumberException(value, targetClass, _format( |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 970 | "DeserializationProblemHandler.handleWeirdNumberValue() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 971 | ClassUtil.getClassDescription(targetClass), |
| 972 | ClassUtil.getClassDescription(key) |
| 973 | )); |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 974 | } |
| 975 | h = h.next(); |
| 976 | } |
| 977 | throw weirdNumberException(value, targetClass, msg); |
| 978 | } |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 979 | |
Tatu Saloranta | 23a610d | 2017-07-26 16:57:29 -0700 | [diff] [blame] | 980 | public Object handleWeirdNativeValue(JavaType targetType, Object badValue, |
| 981 | JsonParser p) |
| 982 | throws IOException |
| 983 | { |
| 984 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 985 | final Class<?> raw = targetType.getRawClass(); |
| 986 | for (; h != null; h = h.next()) { |
| 987 | // Can bail out if it's handled |
| 988 | Object goodValue = h.value().handleWeirdNativeValue(this, targetType, badValue, p); |
| 989 | if (goodValue != DeserializationProblemHandler.NOT_HANDLED) { |
| 990 | // Sanity check for broken handlers, otherwise nasty to debug: |
| 991 | if ((goodValue == null) || raw.isInstance(goodValue)) { |
| 992 | return goodValue; |
| 993 | } |
| 994 | throw JsonMappingException.from(p, _format( |
| 995 | "DeserializationProblemHandler.handleWeirdNativeValue() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 996 | ClassUtil.getClassDescription(targetType), |
| 997 | ClassUtil.getClassDescription(goodValue) |
| 998 | )); |
Tatu Saloranta | 23a610d | 2017-07-26 16:57:29 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | throw weirdNativeValueException(badValue, raw); |
| 1002 | } |
| 1003 | |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1004 | /** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1005 | * Method that deserializers should call if they fail to instantiate value |
| 1006 | * due to lack of viable instantiator (usually creator, that is, constructor |
| 1007 | * or static factory method). Method should be called at point where value |
| 1008 | * has not been decoded, so that handler has a chance to handle decoding |
| 1009 | * using alternate mechanism, and handle underlying content (possibly by |
| 1010 | * just skipping it) to keep input state valid |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1011 | * |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1012 | * @param instClass Type that was to be instantiated |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1013 | * @param valueInst (optional) Value instantiator to be used, if any; null if type does not |
| 1014 | * use one for instantiation (custom deserialiers don't; standard POJO deserializer does) |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1015 | * @param p Parser that points to the JSON value to decode |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1016 | * |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1017 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1018 | * |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1019 | * @since 2.9 (2.8 had alternate that did not take <code>ValueInstantiator</code>) |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1020 | */ |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1021 | @SuppressWarnings("resource") |
| 1022 | public Object handleMissingInstantiator(Class<?> instClass, ValueInstantiator valueInst, |
| 1023 | JsonParser p, String msg, Object... msgArgs) |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1024 | throws IOException |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1025 | { |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1026 | if (p == null) { |
| 1027 | p = getParser(); |
| 1028 | } |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1029 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1030 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1031 | while (h != null) { |
| 1032 | // Can bail out if it's handled |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1033 | Object instance = h.value().handleMissingInstantiator(this, |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1034 | instClass, valueInst, p, msg); |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1035 | if (instance != DeserializationProblemHandler.NOT_HANDLED) { |
| 1036 | // Sanity check for broken handlers, otherwise nasty to debug: |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 1037 | if (_isCompatible(instClass, instance)) { |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1038 | return instance; |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1039 | } |
Tatu Saloranta | 816bbed | 2016-10-05 21:13:12 -0700 | [diff] [blame] | 1040 | reportBadDefinition(constructType(instClass), String.format( |
| 1041 | "DeserializationProblemHandler.handleMissingInstantiator() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1042 | ClassUtil.getClassDescription(instClass), |
| 1043 | ClassUtil.getClassDescription((instance) |
| 1044 | ))); |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1045 | } |
| 1046 | h = h.next(); |
| 1047 | } |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1048 | |
| 1049 | // 16-Oct-2016, tatu: This is either a definition problem (if no applicable creator |
Tatu Saloranta | 581ba1f | 2017-01-25 21:55:11 -0800 | [diff] [blame] | 1050 | // exists), or input mismatch problem (otherwise) since none of existing creators |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1051 | // match with token. |
| 1052 | if ((valueInst != null) && !valueInst.canInstantiate()) { |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1053 | msg = String.format("Cannot construct instance of %s (no Creators, like default construct, exist): %s", |
Tatu Saloranta | 267abe1 | 2016-11-25 18:14:20 -0800 | [diff] [blame] | 1054 | ClassUtil.nameOf(instClass), msg); |
Tatu Saloranta | ed41686 | 2016-10-16 13:42:12 -0700 | [diff] [blame] | 1055 | return reportBadDefinition(constructType(instClass), msg); |
| 1056 | } |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1057 | msg = String.format("Cannot construct instance of %s (although at least one Creator exists): %s", |
Tatu Saloranta | 267abe1 | 2016-11-25 18:14:20 -0800 | [diff] [blame] | 1058 | ClassUtil.nameOf(instClass), msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1059 | return reportInputMismatch(instClass, msg); |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1062 | /** |
| 1063 | * Method that deserializers should call if they fail to instantiate value |
| 1064 | * due to an exception that was thrown by constructor (or other mechanism used |
| 1065 | * to create instances). |
| 1066 | * Default implementation will try to call {@link DeserializationProblemHandler#handleInstantiationProblem} |
| 1067 | * on configured handlers, if any, to allow for recovery; if recovery does not |
| 1068 | * succeed, will throw exception constructed with {@link #instantiationException}. |
| 1069 | * |
| 1070 | * @param instClass Type that was to be instantiated |
| 1071 | * @param argument (optional) Argument that was passed to constructor or equivalent |
| 1072 | * instantiator; often a {@link java.lang.String}. |
| 1073 | * @param t Exception that caused failure |
| 1074 | * |
| 1075 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1076 | * |
| 1077 | * @since 2.8 |
| 1078 | */ |
| 1079 | public Object handleInstantiationProblem(Class<?> instClass, Object argument, |
| 1080 | Throwable t) |
| 1081 | throws IOException |
| 1082 | { |
| 1083 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1084 | while (h != null) { |
| 1085 | // Can bail out if it's handled |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 1086 | Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t); |
| 1087 | if (instance != DeserializationProblemHandler.NOT_HANDLED) { |
| 1088 | // Sanity check for broken handlers, otherwise nasty to debug: |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 1089 | if (_isCompatible(instClass, instance)) { |
Tatu Saloranta | d01e3ab | 2016-05-19 09:31:38 -0700 | [diff] [blame] | 1090 | return instance; |
| 1091 | } |
Tatu Saloranta | 816bbed | 2016-10-05 21:13:12 -0700 | [diff] [blame] | 1092 | reportBadDefinition(constructType(instClass), String.format( |
| 1093 | "DeserializationProblemHandler.handleInstantiationProblem() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1094 | ClassUtil.getClassDescription(instClass), |
| 1095 | ClassUtil.classNameOf(instance) |
| 1096 | )); |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1097 | } |
| 1098 | h = h.next(); |
| 1099 | } |
| 1100 | // 18-May-2016, tatu: Only wrap if not already a valid type to throw |
Tatu Saloranta | 78c7229 | 2016-11-07 20:37:45 -0800 | [diff] [blame] | 1101 | ClassUtil.throwIfIOE(t); |
Tatu Saloranta | e3ec9dc | 2019-09-11 16:33:32 -0700 | [diff] [blame] | 1102 | // [databind#2164]: but see if wrapping is desired |
| 1103 | if (!isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)) { |
| 1104 | ClassUtil.throwIfRTE(t); |
| 1105 | } |
Tatu Saloranta | 609a0f8 | 2016-05-18 20:17:10 -0700 | [diff] [blame] | 1106 | throw instantiationException(instClass, t); |
| 1107 | } |
| 1108 | |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1109 | /** |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1110 | * Method that deserializers should call if the first token of the value to |
| 1111 | * deserialize is of unexpected type (that is, type of token that deserializer |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 1112 | * cannot handle). This could occur, for example, if a Number deserializer |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1113 | * encounter {@link JsonToken#START_ARRAY} instead of |
| 1114 | * {@link JsonToken#VALUE_NUMBER_INT} or {@link JsonToken#VALUE_NUMBER_FLOAT}. |
| 1115 | * |
| 1116 | * @param instClass Type that was to be instantiated |
| 1117 | * @param p Parser that points to the JSON value to decode |
| 1118 | * |
| 1119 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1120 | * |
| 1121 | * @since 2.8 |
| 1122 | */ |
| 1123 | public Object handleUnexpectedToken(Class<?> instClass, JsonParser p) |
| 1124 | throws IOException |
| 1125 | { |
Tatu Saloranta | 2599caf | 2019-09-15 16:17:48 -0700 | [diff] [blame] | 1126 | return handleUnexpectedToken(constructType(instClass), p.getCurrentToken(), p, null); |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1127 | } |
Tatu Saloranta | 43fbd71 | 2016-11-09 21:43:27 -0800 | [diff] [blame] | 1128 | |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1129 | /** |
| 1130 | * Method that deserializers should call if the first token of the value to |
| 1131 | * deserialize is of unexpected type (that is, type of token that deserializer |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 1132 | * cannot handle). This could occur, for example, if a Number deserializer |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1133 | * encounter {@link JsonToken#START_ARRAY} instead of |
| 1134 | * {@link JsonToken#VALUE_NUMBER_INT} or {@link JsonToken#VALUE_NUMBER_FLOAT}. |
| 1135 | * |
| 1136 | * @param instClass Type that was to be instantiated |
Tatu Saloranta | 2a87718 | 2016-10-15 18:18:22 -0700 | [diff] [blame] | 1137 | * @param t Token encountered that does match expected |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1138 | * @param p Parser that points to the JSON value to decode |
| 1139 | * |
| 1140 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1141 | * |
| 1142 | * @since 2.8 |
| 1143 | */ |
| 1144 | public Object handleUnexpectedToken(Class<?> instClass, JsonToken t, |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1145 | JsonParser p, String msg, Object... msgArgs) |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1146 | throws IOException |
| 1147 | { |
Semyon Levin | 28e775f | 2019-09-16 02:01:56 +0300 | [diff] [blame] | 1148 | return handleUnexpectedToken(constructType(instClass), t, p, msg, msgArgs); |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Method that deserializers should call if the first token of the value to |
| 1153 | * deserialize is of unexpected type (that is, type of token that deserializer |
| 1154 | * cannot handle). This could occur, for example, if a Number deserializer |
| 1155 | * encounter {@link JsonToken#START_ARRAY} instead of |
| 1156 | * {@link JsonToken#VALUE_NUMBER_INT} or {@link JsonToken#VALUE_NUMBER_FLOAT}. |
| 1157 | * |
| 1158 | * @param targetType Type that was to be instantiated |
| 1159 | * @param p Parser that points to the JSON value to decode |
| 1160 | * |
| 1161 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1162 | * |
| 1163 | * @since 2.10 |
| 1164 | */ |
| 1165 | public Object handleUnexpectedToken(JavaType targetType, JsonParser p) |
| 1166 | throws IOException |
| 1167 | { |
| 1168 | return handleUnexpectedToken(targetType, p.getCurrentToken(), p, null); |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Method that deserializers should call if the first token of the value to |
| 1173 | * deserialize is of unexpected type (that is, type of token that deserializer |
| 1174 | * cannot handle). This could occur, for example, if a Number deserializer |
| 1175 | * encounter {@link JsonToken#START_ARRAY} instead of |
| 1176 | * {@link JsonToken#VALUE_NUMBER_INT} or {@link JsonToken#VALUE_NUMBER_FLOAT}. |
| 1177 | * |
| 1178 | * @param targetType Type that was to be instantiated |
| 1179 | * @param t Token encountered that does match expected |
| 1180 | * @param p Parser that points to the JSON value to decode |
| 1181 | * |
| 1182 | * @return Object that should be constructed, if any; has to be of type <code>instClass</code> |
| 1183 | * |
| 1184 | * @since 2.10 |
| 1185 | */ |
| 1186 | public Object handleUnexpectedToken(JavaType targetType, JsonToken t, |
| 1187 | JsonParser p, String msg, Object... msgArgs) |
| 1188 | throws IOException |
| 1189 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1190 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1191 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1192 | while (h != null) { |
| 1193 | Object instance = h.value().handleUnexpectedToken(this, |
Semyon Levin | 28e775f | 2019-09-16 02:01:56 +0300 | [diff] [blame] | 1194 | targetType, t, p, msg); |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1195 | if (instance != DeserializationProblemHandler.NOT_HANDLED) { |
Semyon Levin | 28e775f | 2019-09-16 02:01:56 +0300 | [diff] [blame] | 1196 | if (_isCompatible(targetType.getRawClass(), instance)) { |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1197 | return instance; |
| 1198 | } |
Semyon Levin | 28e775f | 2019-09-16 02:01:56 +0300 | [diff] [blame] | 1199 | reportBadDefinition(targetType, String.format( |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1200 | "DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1201 | ClassUtil.getClassDescription(targetType), |
| 1202 | ClassUtil.classNameOf(instance) |
| 1203 | )); |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1204 | } |
| 1205 | h = h.next(); |
| 1206 | } |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1207 | if (msg == null) { |
Tatu Saloranta | 73359ae | 2016-06-06 08:44:54 -0700 | [diff] [blame] | 1208 | if (t == null) { |
| 1209 | msg = String.format("Unexpected end-of-input when binding data into %s", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1210 | ClassUtil.getTypeDescription(targetType)); |
Tatu Saloranta | 73359ae | 2016-06-06 08:44:54 -0700 | [diff] [blame] | 1211 | } else { |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1212 | msg = String.format("Cannot deserialize instance of %s out of %s token", |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1213 | ClassUtil.getTypeDescription(targetType), t); |
Tatu Saloranta | 73359ae | 2016-06-06 08:44:54 -0700 | [diff] [blame] | 1214 | } |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1215 | } |
Semyon Levin | 28e775f | 2019-09-16 02:01:56 +0300 | [diff] [blame] | 1216 | reportInputMismatch(targetType, msg); |
Tatu Saloranta | acc533a | 2016-05-22 21:47:18 -0700 | [diff] [blame] | 1217 | return null; // never gets here |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1221 | * Method that deserializers should call if they encounter a type id |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 1222 | * (for polymorphic deserialization) that cannot be resolved to an |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1223 | * actual type; usually since there is no mapping defined. |
| 1224 | * Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId} |
| 1225 | * on configured handlers, if any, to allow for recovery; if recovery does not |
Tatu Saloranta | 3ee81a6 | 2017-05-02 10:01:48 -0700 | [diff] [blame] | 1226 | * succeed, will throw exception constructed with {@link #invalidTypeIdException}. |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1227 | * |
| 1228 | * @param baseType Base type from which resolution starts |
| 1229 | * @param id Type id that could not be converted |
| 1230 | * @param extraDesc Additional problem description to add to default exception message, |
| 1231 | * if resolution fails. |
| 1232 | * |
| 1233 | * @return {@link JavaType} that id resolves to |
| 1234 | * |
Oliver Kopp | 5e3bf08 | 2017-06-27 14:12:19 +0300 | [diff] [blame] | 1235 | * @throws IOException To indicate unrecoverable problem, if resolution cannot |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1236 | * be made to work |
| 1237 | * |
| 1238 | * @since 2.8 |
| 1239 | */ |
| 1240 | public JavaType handleUnknownTypeId(JavaType baseType, String id, |
Tatu Saloranta | d6230fd | 2016-05-30 21:11:19 -0700 | [diff] [blame] | 1241 | TypeIdResolver idResolver, String extraDesc) throws IOException |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1242 | { |
| 1243 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1244 | while (h != null) { |
| 1245 | // Can bail out if it's handled |
Tatu Saloranta | d6230fd | 2016-05-30 21:11:19 -0700 | [diff] [blame] | 1246 | JavaType type = h.value().handleUnknownTypeId(this, baseType, id, idResolver, extraDesc); |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1247 | if (type != null) { |
| 1248 | if (type.hasRawClass(Void.class)) { |
| 1249 | return null; |
| 1250 | } |
| 1251 | // But ensure there's type compatibility |
| 1252 | if (type.isTypeOrSubTypeOf(baseType.getRawClass())) { |
| 1253 | return type; |
| 1254 | } |
Tatu Saloranta | 5223856 | 2017-04-28 10:52:23 -0700 | [diff] [blame] | 1255 | throw invalidTypeIdException(baseType, id, |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1256 | "problem handler tried to resolve into non-subtype: "+ |
| 1257 | ClassUtil.getTypeDescription(type)); |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1258 | } |
| 1259 | h = h.next(); |
| 1260 | } |
Tatu Saloranta | 811ec17 | 2016-05-24 23:39:04 -0700 | [diff] [blame] | 1261 | // 24-May-2016, tatu: Actually we may still not want to fail quite yet |
| 1262 | if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) { |
| 1263 | return null; |
| 1264 | } |
Tatu Saloranta | 5223856 | 2017-04-28 10:52:23 -0700 | [diff] [blame] | 1265 | throw invalidTypeIdException(baseType, id, extraDesc); |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Tatu Saloranta | 5b8f0d9 | 2017-03-09 10:18:47 -0800 | [diff] [blame] | 1268 | /** |
| 1269 | * @since 2.9 |
| 1270 | */ |
| 1271 | public JavaType handleMissingTypeId(JavaType baseType, |
| 1272 | TypeIdResolver idResolver, String extraDesc) throws IOException |
| 1273 | { |
| 1274 | LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); |
| 1275 | while (h != null) { |
| 1276 | // Can bail out if it's handled |
| 1277 | JavaType type = h.value().handleMissingTypeId(this, baseType, idResolver, extraDesc); |
| 1278 | if (type != null) { |
| 1279 | if (type.hasRawClass(Void.class)) { |
| 1280 | return null; |
| 1281 | } |
| 1282 | // But ensure there's type compatibility |
| 1283 | if (type.isTypeOrSubTypeOf(baseType.getRawClass())) { |
| 1284 | return type; |
| 1285 | } |
Tatu Saloranta | 5223856 | 2017-04-28 10:52:23 -0700 | [diff] [blame] | 1286 | throw invalidTypeIdException(baseType, null, |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1287 | "problem handler tried to resolve into non-subtype: "+ |
| 1288 | ClassUtil.getTypeDescription(type)); |
Tatu Saloranta | 5b8f0d9 | 2017-03-09 10:18:47 -0800 | [diff] [blame] | 1289 | } |
| 1290 | h = h.next(); |
| 1291 | } |
| 1292 | // 09-Mar-2017, tatu: We may want to consider yet another feature at some |
| 1293 | // point to allow returning `null`... but that seems bit risky for now |
| 1294 | // if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) { |
| 1295 | // return null; |
| 1296 | // } |
| 1297 | throw missingTypeIdException(baseType, extraDesc); |
| 1298 | } |
| 1299 | |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 1300 | /** |
Tatu Saloranta | 9051c79 | 2019-08-20 20:12:34 -0700 | [diff] [blame] | 1301 | * Method that deserializer may call if it is called to do an update ("merge") |
| 1302 | * but deserializer operates on a non-mergeable type. Although this should |
| 1303 | * usually be caught earlier, sometimes it may only be caught during operation |
| 1304 | * and if so this is the method to call. |
| 1305 | * Note that if {@link MapperFeature#IGNORE_MERGE_FOR_UNMERGEABLE} is enabled, |
| 1306 | * this method will simply return null; otherwise {@link InvalidDefinitionException} |
| 1307 | * will be thrown. |
| 1308 | * |
| 1309 | * @since 2.10 |
| 1310 | */ |
| 1311 | public void handleBadMerge(JsonDeserializer<?> deser) throws JsonMappingException |
| 1312 | { |
| 1313 | if (!isEnabled(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)) { |
| 1314 | JavaType type = constructType(deser.handledType()); |
Tatu Saloranta | a49faf9 | 2019-09-16 17:21:51 -0700 | [diff] [blame] | 1315 | String msg = String.format("Invalid configuration: values of type %s cannot be merged", |
| 1316 | ClassUtil.getTypeDescription(type)); |
Tatu Saloranta | 9051c79 | 2019-08-20 20:12:34 -0700 | [diff] [blame] | 1317 | throw InvalidDefinitionException.from(getParser(), msg, type); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | /** |
Tatu Saloranta | dcfb0cc | 2017-09-18 21:56:07 -0700 | [diff] [blame] | 1322 | * @since 2.9.2 |
| 1323 | */ |
| 1324 | protected boolean _isCompatible(Class<?> target, Object value) |
| 1325 | { |
| 1326 | if ((value == null) || target.isInstance(value)) { |
| 1327 | return true; |
| 1328 | } |
| 1329 | // [databind#1767]: Make sure to allow wrappers for primitive fields |
| 1330 | return target.isPrimitive() |
| 1331 | && ClassUtil.wrapperType(target).isInstance(value); |
| 1332 | } |
| 1333 | |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1334 | /* |
| 1335 | /********************************************************** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1336 | /* Methods for problem reporting, in cases where recovery |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1337 | /* is not considered possible: input problem |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1338 | /********************************************************** |
| 1339 | */ |
| 1340 | |
| 1341 | /** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1342 | * Method for deserializers to call |
| 1343 | * when the token encountered was of type different than what <b>should</b> |
| 1344 | * be seen at that position, usually within a sequence of expected tokens. |
| 1345 | * Note that this method will throw a {@link JsonMappingException} and no |
| 1346 | * recovery is attempted (via {@link DeserializationProblemHandler}, as |
| 1347 | * problem is considered to be difficult to recover from, in general. |
| 1348 | * |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1349 | * @since 2.9 |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1350 | */ |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1351 | public void reportWrongTokenException(JsonDeserializer<?> deser, |
| 1352 | JsonToken expToken, String msg, Object... msgArgs) |
| 1353 | throws JsonMappingException |
| 1354 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1355 | msg = _format(msg, msgArgs); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1356 | throw wrongTokenException(getParser(), deser.handledType(), expToken, msg); |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Method for deserializers to call |
| 1361 | * when the token encountered was of type different than what <b>should</b> |
| 1362 | * be seen at that position, usually within a sequence of expected tokens. |
| 1363 | * Note that this method will throw a {@link JsonMappingException} and no |
| 1364 | * recovery is attempted (via {@link DeserializationProblemHandler}, as |
| 1365 | * problem is considered to be difficult to recover from, in general. |
| 1366 | * |
| 1367 | * @since 2.9 |
| 1368 | */ |
| 1369 | public void reportWrongTokenException(JavaType targetType, |
| 1370 | JsonToken expToken, String msg, Object... msgArgs) |
| 1371 | throws JsonMappingException |
| 1372 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1373 | msg = _format(msg, msgArgs); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1374 | throw wrongTokenException(getParser(), targetType, expToken, msg); |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * Method for deserializers to call |
| 1379 | * when the token encountered was of type different than what <b>should</b> |
| 1380 | * be seen at that position, usually within a sequence of expected tokens. |
| 1381 | * Note that this method will throw a {@link JsonMappingException} and no |
| 1382 | * recovery is attempted (via {@link DeserializationProblemHandler}, as |
| 1383 | * problem is considered to be difficult to recover from, in general. |
| 1384 | * |
| 1385 | * @since 2.9 |
| 1386 | */ |
| 1387 | public void reportWrongTokenException(Class<?> targetType, |
| 1388 | JsonToken expToken, String msg, Object... msgArgs) |
| 1389 | throws JsonMappingException |
| 1390 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1391 | msg = _format(msg, msgArgs); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1392 | throw wrongTokenException(getParser(), targetType, expToken, msg); |
| 1393 | } |
Tatu Saloranta | 9d1fb75 | 2016-06-07 22:47:04 -0700 | [diff] [blame] | 1394 | |
| 1395 | /** |
| 1396 | * @since 2.8 |
| 1397 | */ |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1398 | public <T> T reportUnresolvedObjectId(ObjectIdReader oidReader, Object bean) |
Tatu Saloranta | 9d1fb75 | 2016-06-07 22:47:04 -0700 | [diff] [blame] | 1399 | throws JsonMappingException |
| 1400 | { |
| 1401 | String msg = String.format("No Object Id found for an instance of %s, to assign to property '%s'", |
Tatu Saloranta | 267abe1 | 2016-11-25 18:14:20 -0800 | [diff] [blame] | 1402 | ClassUtil.classNameOf(bean), oidReader.propertyName); |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1403 | return reportInputMismatch(oidReader.idProperty, msg); |
Tatu Saloranta | 9d1fb75 | 2016-06-07 22:47:04 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Tatu Saloranta | bb06aa0 | 2016-09-08 22:38:23 -0700 | [diff] [blame] | 1406 | /** |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1407 | * Helper method used to indicate a problem with input in cases where more |
| 1408 | * specific <code>reportXxx()</code> method was not available. |
| 1409 | * |
| 1410 | * @since 2.9 |
| 1411 | */ |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1412 | public <T> T reportInputMismatch(JsonDeserializer<?> src, |
| 1413 | String msg, Object... msgArgs) throws JsonMappingException |
| 1414 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1415 | msg = _format(msg, msgArgs); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1416 | throw MismatchedInputException.from(getParser(), src.handledType(), msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /** |
| 1420 | * Helper method used to indicate a problem with input in cases where more |
| 1421 | * specific <code>reportXxx()</code> method was not available. |
| 1422 | * |
| 1423 | * @since 2.9 |
| 1424 | */ |
| 1425 | public <T> T reportInputMismatch(Class<?> targetType, |
| 1426 | String msg, Object... msgArgs) throws JsonMappingException |
| 1427 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1428 | msg = _format(msg, msgArgs); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1429 | throw MismatchedInputException.from(getParser(), targetType, msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Helper method used to indicate a problem with input in cases where more |
| 1434 | * specific <code>reportXxx()</code> method was not available. |
| 1435 | * |
| 1436 | * @since 2.9 |
| 1437 | */ |
| 1438 | public <T> T reportInputMismatch(JavaType targetType, |
| 1439 | String msg, Object... msgArgs) throws JsonMappingException |
| 1440 | { |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1441 | msg = _format(msg, msgArgs); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1442 | throw MismatchedInputException.from(getParser(), targetType, msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1443 | } |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1444 | |
Tatu Saloranta | b3e4194 | 2019-09-11 14:18:02 -0700 | [diff] [blame] | 1445 | /** |
| 1446 | * Helper method used to indicate a problem with input in cases where more |
| 1447 | * specific <code>reportXxx()</code> method was not available. |
| 1448 | * |
| 1449 | * @since 2.9 |
| 1450 | */ |
| 1451 | public <T> T reportInputMismatch(BeanProperty prop, |
| 1452 | String msg, Object... msgArgs) throws JsonMappingException |
| 1453 | { |
| 1454 | msg = _format(msg, msgArgs); |
| 1455 | JavaType type = (prop == null) ? null : prop.getType(); |
| 1456 | final MismatchedInputException e = MismatchedInputException.from(getParser(), type, msg); |
| 1457 | // [databind#2357]: Include property name, if we have it |
| 1458 | if (prop != null) { |
| 1459 | AnnotatedMember member = prop.getMember(); |
| 1460 | if (member != null) { |
| 1461 | e.prependPath(member.getDeclaringClass(), prop.getName()); |
| 1462 | } |
| 1463 | } |
| 1464 | throw e; |
| 1465 | } |
| 1466 | |
| 1467 | /** |
| 1468 | * Helper method used to indicate a problem with input in cases where more |
| 1469 | * specific <code>reportXxx()</code> method was not available. |
| 1470 | * |
| 1471 | * @since 2.10 |
| 1472 | */ |
| 1473 | public <T> T reportPropertyInputMismatch(Class<?> targetType, String propertyName, |
| 1474 | String msg, Object... msgArgs) throws JsonMappingException |
| 1475 | { |
| 1476 | msg = _format(msg, msgArgs); |
| 1477 | MismatchedInputException e = MismatchedInputException.from(getParser(), targetType, msg); |
| 1478 | if (propertyName != null) { |
| 1479 | e.prependPath(targetType, propertyName); |
| 1480 | } |
| 1481 | throw e; |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * Helper method used to indicate a problem with input in cases where more |
| 1486 | * specific <code>reportXxx()</code> method was not available. |
| 1487 | * |
| 1488 | * @since 2.10 |
| 1489 | */ |
| 1490 | public <T> T reportPropertyInputMismatch(JavaType targetType, String propertyName, |
| 1491 | String msg, Object... msgArgs) throws JsonMappingException |
| 1492 | { |
| 1493 | return reportPropertyInputMismatch(targetType.getRawClass(), propertyName, msg, msgArgs); |
| 1494 | } |
| 1495 | |
Tatu Saloranta | 8f9226b | 2017-07-18 17:18:08 -0700 | [diff] [blame] | 1496 | public <T> T reportTrailingTokens(Class<?> targetType, |
| 1497 | JsonParser p, JsonToken trailingToken) throws JsonMappingException |
| 1498 | { |
| 1499 | throw MismatchedInputException.from(p, targetType, String.format( |
| 1500 | "Trailing token (of type %s) found after value (bound as %s): not allowed as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`", |
| 1501 | trailingToken, ClassUtil.nameOf(targetType) |
| 1502 | )); |
| 1503 | } |
| 1504 | |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1505 | @Deprecated // since 2.9 |
| 1506 | public void reportWrongTokenException(JsonParser p, |
| 1507 | JsonToken expToken, String msg, Object... msgArgs) |
| 1508 | throws JsonMappingException |
| 1509 | { |
| 1510 | msg = _format(msg, msgArgs); |
| 1511 | throw wrongTokenException(p, expToken, msg); |
| 1512 | } |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1513 | |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1514 | /** |
| 1515 | * Helper method for reporting a problem with unhandled unknown property. |
| 1516 | * |
| 1517 | * @param instanceOrClass Either value being populated (if one has been |
| 1518 | * instantiated), or Class that indicates type that would be (or |
| 1519 | * have been) instantiated |
| 1520 | * @param deser Deserializer that had the problem, if called by deserializer |
| 1521 | * (or on behalf of one) |
| 1522 | * |
| 1523 | * @deprecated Since 2.8 call {@link #handleUnknownProperty} instead |
| 1524 | */ |
| 1525 | @Deprecated |
| 1526 | public void reportUnknownProperty(Object instanceOrClass, String fieldName, |
| 1527 | JsonDeserializer<?> deser) |
| 1528 | throws JsonMappingException |
| 1529 | { |
| 1530 | if (isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { |
| 1531 | // Do we know properties that are expected instead? |
| 1532 | Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); |
| 1533 | throw UnrecognizedPropertyException.from(_parser, |
| 1534 | instanceOrClass, fieldName, propIds); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * @since 2.8 |
| 1540 | * |
| 1541 | * @deprecated Since 2.9: not clear this ever occurs |
| 1542 | */ |
| 1543 | @Deprecated // since 2.9 |
| 1544 | public void reportMissingContent(String msg, Object... msgArgs) throws JsonMappingException { |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1545 | throw MismatchedInputException.from(getParser(), (JavaType) null, "No content to map due to end-of-input"); |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | /********************************************************** |
| 1550 | /* Methods for problem reporting, in cases where recovery |
| 1551 | /* is not considered possible: POJO definition problems |
| 1552 | /********************************************************** |
| 1553 | */ |
| 1554 | |
| 1555 | /** |
Tatu Saloranta | bb06aa0 | 2016-09-08 22:38:23 -0700 | [diff] [blame] | 1556 | * Helper method called to indicate problem in POJO (serialization) definitions or settings |
| 1557 | * regarding specific Java type, unrelated to actual JSON content to map. |
| 1558 | * Default behavior is to construct and throw a {@link JsonMappingException}. |
| 1559 | * |
| 1560 | * @since 2.9 |
| 1561 | */ |
| 1562 | public <T> T reportBadTypeDefinition(BeanDescription bean, |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1563 | String msg, Object... msgArgs) throws JsonMappingException { |
| 1564 | msg = _format(msg, msgArgs); |
Tatu Saloranta | e088920 | 2017-03-30 15:43:20 -0700 | [diff] [blame] | 1565 | String beanDesc = ClassUtil.nameOf(bean.getBeanClass()); |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1566 | msg = String.format("Invalid type definition for type %s: %s", beanDesc, msg); |
| 1567 | throw InvalidDefinitionException.from(_parser, msg, bean, null); |
Tatu Saloranta | bb06aa0 | 2016-09-08 22:38:23 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * Helper method called to indicate problem in POJO (serialization) definitions or settings |
| 1572 | * regarding specific property (of a type), unrelated to actual JSON content to map. |
| 1573 | * Default behavior is to construct and throw a {@link JsonMappingException}. |
| 1574 | * |
| 1575 | * @since 2.9 |
| 1576 | */ |
| 1577 | public <T> T reportBadPropertyDefinition(BeanDescription bean, BeanPropertyDefinition prop, |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1578 | String msg, Object... msgArgs) throws JsonMappingException { |
| 1579 | msg = _format(msg, msgArgs); |
Tatu Saloranta | 43fbd71 | 2016-11-09 21:43:27 -0800 | [diff] [blame] | 1580 | String propName = ClassUtil.nameOf(prop); |
Tatu Saloranta | e088920 | 2017-03-30 15:43:20 -0700 | [diff] [blame] | 1581 | String beanDesc = ClassUtil.nameOf(bean.getBeanClass()); |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1582 | msg = String.format("Invalid definition for property %s (of type %s): %s", |
| 1583 | propName, beanDesc, msg); |
| 1584 | throw InvalidDefinitionException.from(_parser, msg, bean, prop); |
Tatu Saloranta | bb06aa0 | 2016-09-08 22:38:23 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1587 | @Override |
| 1588 | public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException { |
| 1589 | throw InvalidDefinitionException.from(_parser, msg, type); |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1592 | /** |
Tatu Saloranta | 9051c79 | 2019-08-20 20:12:34 -0700 | [diff] [blame] | 1593 | * @deprecated Since 2.10 use {@link #handleBadMerge} instead |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1594 | */ |
Tatu Saloranta | 9051c79 | 2019-08-20 20:12:34 -0700 | [diff] [blame] | 1595 | @Deprecated // since 2.10 |
| 1596 | public <T> T reportBadMerge(JsonDeserializer<?> deser) throws JsonMappingException { |
| 1597 | handleBadMerge(deser); |
| 1598 | return null; |
Tatu Saloranta | b8f6029 | 2016-10-25 22:50:06 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1601 | /* |
| 1602 | /********************************************************** |
| 1603 | /* Methods for constructing semantic exceptions; usually not |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1604 | /* to be called directly, call `handleXxx()` instead |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1605 | /********************************************************** |
| 1606 | */ |
| 1607 | |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1608 | /** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1609 | * Helper method for constructing {@link JsonMappingException} to indicate |
Tatu Saloranta | 98dd091 | 2016-05-15 23:13:44 -0700 | [diff] [blame] | 1610 | * that the token encountered was of type different than what <b>should</b> |
| 1611 | * be seen at that position, usually within a sequence of expected tokens. |
| 1612 | * Note that most of the time this method should NOT be directly called; |
| 1613 | * instead, {@link #reportWrongTokenException} should be called and will |
| 1614 | * call this method as necessary. |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1615 | * |
| 1616 | * @since 2.9 |
Tatu Saloranta | 98dd091 | 2016-05-15 23:13:44 -0700 | [diff] [blame] | 1617 | */ |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1618 | public JsonMappingException wrongTokenException(JsonParser p, JavaType targetType, |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1619 | JsonToken expToken, String extra) |
Tatu Saloranta | 98dd091 | 2016-05-15 23:13:44 -0700 | [diff] [blame] | 1620 | { |
| 1621 | String msg = String.format("Unexpected token (%s), expected %s", |
| 1622 | p.getCurrentToken(), expToken); |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1623 | msg = _colonConcat(msg, extra); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1624 | return MismatchedInputException.from(p, targetType, msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | public JsonMappingException wrongTokenException(JsonParser p, Class<?> targetType, |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1628 | JsonToken expToken, String extra) |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1629 | { |
| 1630 | String msg = String.format("Unexpected token (%s), expected %s", |
| 1631 | p.getCurrentToken(), expToken); |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1632 | msg = _colonConcat(msg, extra); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1633 | return MismatchedInputException.from(p, targetType, msg); |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | @Deprecated // since 2.9 |
| 1637 | public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken, |
| 1638 | String msg) |
| 1639 | { |
| 1640 | return wrongTokenException(p, (JavaType) null, expToken, msg); |
Tatu Saloranta | 98dd091 | 2016-05-15 23:13:44 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /** |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1644 | * Helper method for constructing exception to indicate that given JSON |
| 1645 | * Object field name was not in format to be able to deserialize specified |
| 1646 | * key type. |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1647 | * Note that most of the time this method should NOT be called; instead, |
| 1648 | * {@link #handleWeirdKey} should be called which will call this method |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1649 | * if necessary. |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1650 | */ |
| 1651 | public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, |
| 1652 | String msg) { |
| 1653 | return InvalidFormatException.from(_parser, |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1654 | String.format("Cannot deserialize Map key of type %s from String %s: %s", |
Tatu Saloranta | 267abe1 | 2016-11-25 18:14:20 -0800 | [diff] [blame] | 1655 | ClassUtil.nameOf(keyClass), _quotedString(keyValue), msg), |
Tatu Saloranta | 1e5fd12 | 2016-05-09 23:26:30 -0700 | [diff] [blame] | 1656 | keyValue, keyClass); |
| 1657 | } |
| 1658 | |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1659 | /** |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1660 | * Helper method for constructing exception to indicate that input JSON |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1661 | * String was not suitable for deserializing into given target type. |
| 1662 | * Note that most of the time this method should NOT be called; instead, |
| 1663 | * {@link #handleWeirdStringValue} should be called which will call this method |
| 1664 | * if necessary. |
| 1665 | * |
| 1666 | * @param value String value from input being deserialized |
| 1667 | * @param instClass Type that String should be deserialized into |
Tatu Saloranta | 94c56eb | 2019-01-18 20:45:25 -0800 | [diff] [blame] | 1668 | * @param msgBase Message that describes specific problem |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1669 | * |
| 1670 | * @since 2.1 |
| 1671 | */ |
| 1672 | public JsonMappingException weirdStringException(String value, Class<?> instClass, |
Tatu Saloranta | 94c56eb | 2019-01-18 20:45:25 -0800 | [diff] [blame] | 1673 | String msgBase) { |
| 1674 | final String msg = String.format("Cannot deserialize value of type %s from String %s: %s", |
| 1675 | ClassUtil.nameOf(instClass), _quotedString(value), msgBase); |
| 1676 | return InvalidFormatException.from(_parser, msg, value, instClass); |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * Helper method for constructing exception to indicate that input JSON |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1681 | * Number was not suitable for deserializing into given target type. |
Tatu Saloranta | 0b6cb9b | 2016-05-15 22:59:17 -0700 | [diff] [blame] | 1682 | * Note that most of the time this method should NOT be called; instead, |
| 1683 | * {@link #handleWeirdNumberValue} should be called which will call this method |
| 1684 | * if necessary. |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1685 | */ |
| 1686 | public JsonMappingException weirdNumberException(Number value, Class<?> instClass, |
| 1687 | String msg) { |
| 1688 | return InvalidFormatException.from(_parser, |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1689 | String.format("Cannot deserialize value of type %s from number %s: %s", |
Tatu Saloranta | 267abe1 | 2016-11-25 18:14:20 -0800 | [diff] [blame] | 1690 | ClassUtil.nameOf(instClass), String.valueOf(value), msg), |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1691 | value, instClass); |
| 1692 | } |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame] | 1693 | |
| 1694 | /** |
Tatu Saloranta | 23a610d | 2017-07-26 16:57:29 -0700 | [diff] [blame] | 1695 | * Helper method for constructing exception to indicate that input JSON |
| 1696 | * token of type "native value" (see {@link JsonToken#VALUE_EMBEDDED_OBJECT}) |
| 1697 | * is of incompatible type (and there is no delegating creator or such to use) |
| 1698 | * and can not be used to construct value of specified type (usually POJO). |
| 1699 | * Note that most of the time this method should NOT be called; instead, |
| 1700 | * {@link #handleWeirdNativeValue} should be called which will call this method |
| 1701 | * |
| 1702 | * @since 2.9 |
| 1703 | */ |
| 1704 | public JsonMappingException weirdNativeValueException(Object value, Class<?> instClass) |
| 1705 | { |
| 1706 | return InvalidFormatException.from(_parser, String.format( |
| 1707 | "Cannot deserialize value of type %s from native value (`JsonToken.VALUE_EMBEDDED_OBJECT`) of type %s: incompatible types", |
| 1708 | ClassUtil.nameOf(instClass), ClassUtil.classNameOf(value)), |
| 1709 | value, instClass); |
| 1710 | } |
| 1711 | |
| 1712 | /** |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame] | 1713 | * Helper method for constructing instantiation exception for specified type, |
| 1714 | * to indicate problem with physically constructing instance of |
| 1715 | * specified class (missing constructor, exception from constructor) |
| 1716 | *<p> |
Tatu Saloranta | 555cbeb | 2018-08-28 22:19:01 -0700 | [diff] [blame] | 1717 | * Note that most of the time this method should NOT be called directly; instead, |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame] | 1718 | * {@link #handleInstantiationProblem} should be called which will call this method |
| 1719 | * if necessary. |
| 1720 | */ |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1721 | public JsonMappingException instantiationException(Class<?> instClass, Throwable cause) { |
Tatu Saloranta | 555cbeb | 2018-08-28 22:19:01 -0700 | [diff] [blame] | 1722 | String excMsg; |
| 1723 | if (cause == null) { |
| 1724 | excMsg = "N/A"; |
Tatu Saloranta | f6cf181 | 2018-09-05 23:17:05 -0700 | [diff] [blame] | 1725 | } else if ((excMsg = ClassUtil.exceptionMessage(cause)) == null) { |
Tatu Saloranta | 555cbeb | 2018-08-28 22:19:01 -0700 | [diff] [blame] | 1726 | excMsg = ClassUtil.nameOf(cause.getClass()); |
| 1727 | } |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1728 | String msg = String.format("Cannot construct instance of %s, problem: %s", |
Tatu Saloranta | 555cbeb | 2018-08-28 22:19:01 -0700 | [diff] [blame] | 1729 | ClassUtil.nameOf(instClass), excMsg); |
Tatu Saloranta | 39294f9 | 2018-09-04 22:25:50 -0700 | [diff] [blame] | 1730 | // [databind#2162]: use specific exception type as we don't know if it's |
| 1731 | // due to type definition, input, or neither |
| 1732 | return ValueInstantiationException.from(_parser, msg, constructType(instClass), cause); |
Tatu Saloranta | b1041d0 | 2016-05-19 08:22:50 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Tatu Saloranta | 888c6b9 | 2016-05-15 22:16:40 -0700 | [diff] [blame] | 1735 | /** |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1736 | * Helper method for constructing instantiation exception for specified type, |
| 1737 | * to indicate that instantiation failed due to missing instantiator |
| 1738 | * (creator; constructor or factory method). |
| 1739 | *<p> |
| 1740 | * Note that most of the time this method should NOT be called; instead, |
| 1741 | * {@link #handleMissingInstantiator} should be called which will call this method |
| 1742 | * if necessary. |
| 1743 | */ |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1744 | public JsonMappingException instantiationException(Class<?> instClass, String msg0) { |
Tatu Saloranta | 39294f9 | 2018-09-04 22:25:50 -0700 | [diff] [blame] | 1745 | // [databind#2162]: use specific exception type as we don't know if it's |
| 1746 | // due to type definition, input, or neither |
| 1747 | return ValueInstantiationException.from(_parser, |
| 1748 | String.format("Cannot construct instance of %s: %s", |
| 1749 | ClassUtil.nameOf(instClass), msg0), |
| 1750 | constructType(instClass)); |
Tatu Saloranta | 6824544 | 2016-05-21 18:08:41 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Tatu Saloranta | 3e75d54 | 2017-05-02 22:45:54 -0700 | [diff] [blame] | 1753 | @Override |
Tatu Saloranta | 5223856 | 2017-04-28 10:52:23 -0700 | [diff] [blame] | 1754 | public JsonMappingException invalidTypeIdException(JavaType baseType, String typeId, |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1755 | String extraDesc) { |
Tatu Saloranta | 5223856 | 2017-04-28 10:52:23 -0700 | [diff] [blame] | 1756 | String msg = String.format("Could not resolve type id '%s' as a subtype of %s", |
Tatu Saloranta | 5f1bb5a | 2019-11-04 22:05:23 -0800 | [diff] [blame] | 1757 | typeId, ClassUtil.getTypeDescription(baseType)); |
Tatu Saloranta | 125ac18 | 2017-03-09 11:10:02 -0800 | [diff] [blame] | 1758 | return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, typeId); |
Tatu Saloranta | 2d318f6 | 2016-05-10 21:58:00 -0700 | [diff] [blame] | 1759 | } |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1760 | |
Tatu Saloranta | 5b8f0d9 | 2017-03-09 10:18:47 -0800 | [diff] [blame] | 1761 | /** |
| 1762 | * @since 2.9 |
| 1763 | */ |
| 1764 | public JsonMappingException missingTypeIdException(JavaType baseType, |
| 1765 | String extraDesc) { |
| 1766 | String msg = String.format("Missing type id when trying to resolve subtype of %s", |
| 1767 | baseType); |
Tatu Saloranta | 125ac18 | 2017-03-09 11:10:02 -0800 | [diff] [blame] | 1768 | return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, null); |
Tatu Saloranta | 5b8f0d9 | 2017-03-09 10:18:47 -0800 | [diff] [blame] | 1769 | } |
| 1770 | |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1771 | /* |
| 1772 | /********************************************************** |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1773 | /* Deprecated exception factory methods |
Tatu Saloranta | 34eaa4f | 2016-05-05 13:11:44 -0700 | [diff] [blame] | 1774 | /********************************************************** |
| 1775 | */ |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1776 | |
Tatu Saloranta | 7a4193f | 2016-05-05 12:55:05 -0700 | [diff] [blame] | 1777 | /** |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1778 | * @since 2.5 |
Tatu Saloranta | 2ffca93 | 2016-05-05 12:23:14 -0700 | [diff] [blame] | 1779 | * |
Tatu Saloranta | 33ba4ad | 2016-05-10 20:03:26 -0700 | [diff] [blame] | 1780 | * @deprecated Since 2.8 use {@link #handleUnknownTypeId} instead |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1781 | */ |
Tatu Saloranta | 2ffca93 | 2016-05-05 12:23:14 -0700 | [diff] [blame] | 1782 | @Deprecated |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1783 | public JsonMappingException unknownTypeException(JavaType type, String id, |
Tatu Saloranta | 43fbd71 | 2016-11-09 21:43:27 -0800 | [diff] [blame] | 1784 | String extraDesc) |
| 1785 | { |
Tatu Saloranta | 8c3eb03 | 2016-04-20 22:52:38 -0700 | [diff] [blame] | 1786 | String msg = String.format("Could not resolve type id '%s' into a subtype of %s", |
Tatu Saloranta | 5f1bb5a | 2019-11-04 22:05:23 -0800 | [diff] [blame] | 1787 | id, ClassUtil.getTypeDescription(type)); |
Tatu Saloranta | 7912486 | 2016-10-19 22:13:29 -0700 | [diff] [blame] | 1788 | msg = _colonConcat(msg, extraDesc); |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1789 | return MismatchedInputException.from(_parser, type, msg); |
Tatu Saloranta | 7fc3238 | 2014-12-26 17:19:09 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1792 | /** |
| 1793 | * Helper method for constructing exception to indicate that end-of-input was |
| 1794 | * reached while still expecting more tokens to deserialize value of specified type. |
| 1795 | * |
| 1796 | * @deprecated Since 2.8; currently no way to catch EOF at databind level |
| 1797 | */ |
| 1798 | @Deprecated |
| 1799 | public JsonMappingException endOfInputException(Class<?> instClass) { |
Tatu Saloranta | a0bd159 | 2016-11-25 18:49:52 -0800 | [diff] [blame] | 1800 | return MismatchedInputException.from(_parser, instClass, |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1801 | "Unexpected end-of-input when trying to deserialize a "+instClass.getName()); |
Tatu Saloranta | 1ecc6b5 | 2016-05-10 22:18:53 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1804 | /* |
| 1805 | /********************************************************** |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1806 | /* Deprecated methods for constructing, throwing non-specific |
| 1807 | /* JsonMappingExceptions: as of 2.9, should use more specific |
| 1808 | /* ones. |
| 1809 | /********************************************************** |
| 1810 | */ |
| 1811 | |
| 1812 | /** |
| 1813 | * Fallback method that may be called if no other <code>reportXxx</code> |
| 1814 | * is applicable -- but only in that case. |
| 1815 | * |
| 1816 | * @since 2.8 |
| 1817 | * |
Tatu Saloranta | d9b0cae | 2017-04-24 22:49:03 -0700 | [diff] [blame] | 1818 | * @deprecated Since 2.9: use a more specific method, or {@link #reportBadDefinition(JavaType, String)}, |
Tatu Saloranta | fbc1fa2 | 2016-10-06 15:07:52 -0700 | [diff] [blame] | 1819 | * or {@link #reportInputMismatch} instead |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1820 | */ |
| 1821 | @Deprecated // since 2.9 |
| 1822 | public void reportMappingException(String msg, Object... msgArgs) |
| 1823 | throws JsonMappingException |
| 1824 | { |
Tatu Saloranta | e27ec2d | 2016-10-24 22:44:16 -0700 | [diff] [blame] | 1825 | throw JsonMappingException.from(getParser(), _format(msg, msgArgs)); |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * Helper method for constructing generic mapping exception with specified |
| 1830 | * message and current location information. |
| 1831 | * Note that application code should almost always call |
| 1832 | * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)} |
| 1833 | * instead. |
| 1834 | * |
| 1835 | * @since 2.6 |
| 1836 | * |
| 1837 | * @deprecated Since 2.9 use more specific error reporting methods instead |
| 1838 | */ |
| 1839 | @Deprecated |
| 1840 | public JsonMappingException mappingException(String message) { |
| 1841 | return JsonMappingException.from(getParser(), message); |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * Helper method for constructing generic mapping exception with specified |
| 1846 | * message and current location information |
| 1847 | * Note that application code should almost always call |
| 1848 | * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)} |
| 1849 | * instead. |
| 1850 | * |
| 1851 | * @since 2.6 |
| 1852 | * |
| 1853 | * @deprecated Since 2.9 use more specific error reporting methods instead |
| 1854 | */ |
| 1855 | @Deprecated |
Tatu Saloranta | 4fc0fb3 | 2016-10-20 23:04:31 -0700 | [diff] [blame] | 1856 | public JsonMappingException mappingException(String msg, Object... msgArgs) { |
| 1857 | return JsonMappingException.from(getParser(), _format(msg, msgArgs)); |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * Helper method for constructing generic mapping exception for specified type |
| 1862 | * |
| 1863 | * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead |
| 1864 | */ |
| 1865 | @Deprecated |
| 1866 | public JsonMappingException mappingException(Class<?> targetClass) { |
| 1867 | return mappingException(targetClass, _parser.getCurrentToken()); |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead |
| 1872 | */ |
| 1873 | @Deprecated |
| 1874 | public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) { |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1875 | return JsonMappingException.from(_parser, |
Oliver Kopp | 6847680 | 2017-06-27 22:26:41 +0300 | [diff] [blame] | 1876 | String.format("Cannot deserialize instance of %s out of %s token", |
Tatu Saloranta | 9a81cf1 | 2017-09-18 22:04:55 -0700 | [diff] [blame] | 1877 | ClassUtil.nameOf(targetClass), token)); |
Tatu Saloranta | c1478ac | 2016-09-29 08:17:03 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | /********************************************************** |
Tatu Saloranta | 851092c | 2016-05-21 20:08:40 -0700 | [diff] [blame] | 1882 | /* Other internal methods |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1883 | /********************************************************** |
| 1884 | */ |
| 1885 | |
| 1886 | protected DateFormat getDateFormat() |
| 1887 | { |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1888 | if (_dateFormat != null) { |
| 1889 | return _dateFormat; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1890 | } |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1891 | /* 24-Feb-2012, tatu: At this point, all timezone configuration |
Cowtowncoder | 8d0d6e4 | 2015-01-22 15:36:13 -0800 | [diff] [blame] | 1892 | * should have occurred, with respect to default dateformat |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1893 | * and timezone configuration. But we still better clone |
| 1894 | * an instance as formatters may be stateful. |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1895 | */ |
| 1896 | DateFormat df = _config.getDateFormat(); |
Tatu Saloranta | 3f0ca2e | 2012-02-25 15:54:08 -0800 | [diff] [blame] | 1897 | _dateFormat = df = (DateFormat) df.clone(); |
Tatu Saloranta | 3b88a6b | 2012-02-24 09:53:59 -0800 | [diff] [blame] | 1898 | return df; |
Tatu Saloranta | 06c20b1 | 2012-01-29 21:36:52 -0800 | [diff] [blame] | 1899 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1900 | } |