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