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