blob: af2f24678a9a5bbf703b74b53d67c64978879cb8 [file] [log] [blame]
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08001package com.fasterxml.jackson.databind;
2
3import java.io.IOException;
4import java.util.*;
5
6import com.fasterxml.jackson.core.*;
Tatub37ff332012-01-24 16:19:36 -08007
8import com.fasterxml.jackson.databind.cfg.MapperConfig;
Tatu Salorantad92c1ed2011-12-23 18:23:31 -08009import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
Tatub37ff332012-01-24 16:19:36 -080010import com.fasterxml.jackson.databind.introspect.Annotated;
Tatu Saloranta2f823442011-12-23 20:25:27 -080011import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080012import com.fasterxml.jackson.databind.type.TypeFactory;
13import com.fasterxml.jackson.databind.util.ArrayBuilders;
14import com.fasterxml.jackson.databind.util.ObjectBuffer;
15
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080016/**
17 * Context for deserialization process. Used to allow passing in configuration
18 * settings and reusable temporary objects (scrap arrays, containers).
19 */
20public abstract class DeserializationContext
21{
22 protected final DeserializationConfig _config;
23
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080024 protected final int _featureFlags;
Tatu Salorantab2d3c7d2012-01-27 21:41:48 -080025
26 protected final Class<?> _view;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080027
28 /*
29 /**********************************************************
30 /* Life-cycle
31 /**********************************************************
32 */
33
34 protected DeserializationContext(DeserializationConfig config)
35 {
36 _config = config;
Tatu Salorantac733a7f2012-01-18 20:25:27 -080037 _featureFlags = config.getFeatureFlags();
Tatu Salorantab2d3c7d2012-01-27 21:41:48 -080038 _view = config.getActiveView();
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080039 }
40
41 /*
42 /**********************************************************
43 /* Configuration methods
44 /**********************************************************
45 */
46
47 /**
48 * Method for accessing configuration setting object for
49 * currently active deserialization.
50 */
51 public DeserializationConfig getConfig() { return _config; }
52
Tatu Salorantabb802d22012-01-23 22:33:09 -080053 public final AnnotationIntrospector getAnnotationIntrospector() {
Tatu Saloranta78dddb92012-01-15 10:00:17 -080054 return _config.getAnnotationIntrospector();
55 }
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080056
57 /**
58 * Convenience method for checking whether specified on/off
59 * feature is enabled
60 */
Tatu Salorantabb802d22012-01-23 22:33:09 -080061 public final boolean isEnabled(DeserializationConfig.Feature feat) {
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080062 /* 03-Dec-2010, tatu: minor shortcut; since this is called quite often,
63 * let's use a local copy of feature settings:
64 */
65 return (_featureFlags & feat.getMask()) != 0;
66 }
67
Tatub37ff332012-01-24 16:19:36 -080068 public final boolean isEnabled(MapperConfig.Feature feat) {
69 return _config.isEnabled(feat);
70 }
Tatu Salorantab2d3c7d2012-01-27 21:41:48 -080071
72 public final Class<?> getActiveView() {
73 return _view;
74 }
Tatub37ff332012-01-24 16:19:36 -080075
76 public final boolean canOverrideAccessModifiers() {
77 return _config.canOverrideAccessModifiers();
78 }
79
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080080 /**
81 * Convenience method for accessing the default Base64 encoding
82 * used for decoding base64 encoded binary content.
83 * Same as calling:
84 *<pre>
85 * getConfig().getBase64Variant();
86 *</pre>
87 */
88 public Base64Variant getBase64Variant() {
89 return _config.getBase64Variant();
90 }
91
92 /**
93 * Accessor for getting access to the underlying JSON parser used
94 * for deserialization.
95 */
96 public abstract JsonParser getParser();
97
98 public final JsonNodeFactory getNodeFactory() {
99 return _config.getNodeFactory();
100 }
101
Tatu Salorantabb802d22012-01-23 22:33:09 -0800102 public final JavaType constructType(Class<?> cls) {
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800103 return _config.constructType(cls);
104 }
105
Tatu Salorantabb802d22012-01-23 22:33:09 -0800106 public final TypeFactory getTypeFactory() {
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800107 return _config.getTypeFactory();
108 }
109
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800110 public abstract Object findInjectableValue(Object valueId,
111 BeanProperty forProperty, Object beanInstance);
Tatu Salorantae8dc6032012-01-23 22:43:26 -0800112
113 /*
114 /**********************************************************
Tatu9e504222012-01-24 11:23:59 -0800115 /* By-pass methods to DeserializerCache
Tatu Salorantae8dc6032012-01-23 22:43:26 -0800116 /**********************************************************
117 */
118
119 /**
120 * Convenience method, functionally same as:
121 *<pre>
122 * getDeserializerProvider().findValueDeserializer(getConfig(), propertyType, property);
123 *</pre>
124 */
125 public abstract JsonDeserializer<Object> findValueDeserializer(JavaType propertyType,
126 BeanProperty property)
127 throws JsonMappingException;
128
129 /**
130 * Convenience method, functionally same as:
131 *<pre>
132 * getDeserializerProvider().findTypedValueDeserializer(getConfig(), propertyType, property);
133 *</pre>
134 */
135 public abstract JsonDeserializer<Object> findTypedValueDeserializer(JavaType type,
136 BeanProperty property)
137 throws JsonMappingException;
138
139 /**
140 * Convenience method, functionally same as:
141 *<pre>
142 * getDeserializerProvider().findKeyDeserializer(getConfig(), propertyType, property);
143 *</pre>
144 */
145 public abstract KeyDeserializer findKeyDeserializer(JavaType keyType,
146 BeanProperty property)
147 throws JsonMappingException;
Tatub37ff332012-01-24 16:19:36 -0800148
149 /*
150 /**********************************************************
151 /* Extended API: handler instantiation
152 /**********************************************************
153 */
154
155 public abstract JsonDeserializer<Object> deserializerInstance(Annotated annotated,
156 BeanProperty property, Object deserDef)
157 throws JsonMappingException;
158
159 public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated,
160 BeanProperty property, Object keyDeserClass)
161 throws JsonMappingException;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800162
163 /*
164 /**********************************************************
165 /* Methods for accessing reusable/recyclable helper objects
166 /**********************************************************
167 */
168
169 /**
170 * Method that can be used to get access to a reusable ObjectBuffer,
171 * useful for efficiently constructing Object arrays and Lists.
172 * Note that leased buffers should be returned once deserializer
173 * is done, to allow for reuse during same round of deserialization.
174 */
175 public abstract ObjectBuffer leaseObjectBuffer();
176
177 /**
178 * Method to call to return object buffer previously leased with
179 * {@link #leaseObjectBuffer}.
180 *
181 * @param buf Returned object buffer
182 */
183 public abstract void returnObjectBuffer(ObjectBuffer buf);
184
185 /**
186 * Method for accessing object useful for building arrays of
187 * primitive types (such as int[]).
188 */
189 public abstract ArrayBuilders getArrayBuilders();
190
191 /*
192 /**********************************************************
193 /* Parsing methods that may use reusable/-cyclable objects
194 /**********************************************************
195 */
196
197 /**
198 * Convenience method for parsing a Date from given String, using
199 * currently configured date format (accessed using
200 * {@link DeserializationConfig#getDateFormat()}).
201 *<p>
202 * Implementation will handle thread-safety issues related to
203 * date formats such that first time this method is called,
204 * date format is cloned, and cloned instance will be retained
205 * for use during this deserialization round.
206 */
207 public abstract java.util.Date parseDate(String dateStr)
208 throws IllegalArgumentException;
209
210 /**
211 * Convenience method for constructing Calendar instance set
212 * to specified time, to be modified and used by caller.
213 */
214 public abstract Calendar constructCalendar(Date d);
215
216 /*
217 /**********************************************************
218 /* Methods for problem handling, reporting
219 /**********************************************************
220 */
221
222 /**
223 * Method deserializers can call to inform configured {@link DeserializationProblemHandler}s
224 * of an unrecognized property.
225 *
226 * @return True if there was a configured problem handler that was able to handle the
Tatu Saloranta0e3b3832012-01-22 22:08:20 -0800227 * problem
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800228 */
229 public abstract boolean handleUnknownProperty(JsonParser jp, JsonDeserializer<?> deser, Object instanceOrClass, String propName)
230 throws IOException, JsonProcessingException;
231
232 /**
233 * Helper method for constructing generic mapping exception for specified type
234 */
235 public abstract JsonMappingException mappingException(Class<?> targetClass);
236
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800237 public abstract JsonMappingException mappingException(Class<?> targetClass, JsonToken t);
238
239 /**
240 * Helper method for constructing generic mapping exception with specified
241 * message and current location information
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800242 */
243 public JsonMappingException mappingException(String message)
244 {
245 return JsonMappingException.from(getParser(), message);
246 }
247
248 /**
249 * Helper method for constructing instantiation exception for specified type,
250 * to indicate problem with physically constructing instance of
251 * specified class (missing constructor, exception from constructor)
252 */
253 public abstract JsonMappingException instantiationException(Class<?> instClass, Throwable t);
254
255 public abstract JsonMappingException instantiationException(Class<?> instClass, String msg);
256
257 /**
258 * Helper method for constructing exception to indicate that input JSON
259 * String was not in recognized format for deserializing into given type.
260 */
261 public abstract JsonMappingException weirdStringException(Class<?> instClass, String msg);
262
263 /**
264 * Helper method for constructing exception to indicate that input JSON
265 * Number was not suitable for deserializing into given type.
266 */
267 public abstract JsonMappingException weirdNumberException(Class<?> instClass, String msg);
268
269 /**
270 * Helper method for constructing exception to indicate that given JSON
271 * Object field name was not in format to be able to deserialize specified
272 * key type.
273 */
274 public abstract JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg);
275
276 /**
277 * Helper method for indicating that the current token was expected to be another
278 * token.
279 */
280 public abstract JsonMappingException wrongTokenException(JsonParser jp, JsonToken expToken, String msg);
281
282 /**
283 * Helper method for constructing exception to indicate that JSON Object
284 * field name did not map to a known property of type being
285 * deserialized.
286 *
287 * @param instanceOrClass Either value being populated (if one has been
288 * instantiated), or Class that indicates type that would be (or
289 * have been) instantiated
290 */
291 public abstract JsonMappingException unknownFieldException(Object instanceOrClass, String fieldName);
292
293 /**
294 * Helper method for constructing exception to indicate that given
295 * type id (parsed from JSON) could not be converted to a Java type.
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800296 */
297 public abstract JsonMappingException unknownTypeException(JavaType baseType, String id);
298}