@@ -219,16 +219,10 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider ctxt,
219
219
// And this is where this class comes in: if type is not a
220
220
// known "primary JDK type", perhaps it's a bean? We can still
221
221
// get a null, if we can't find a single suitable bean property.
222
- ser = findBeanSerializer (ctxt , beanDesc , type , formatOverrides );
222
+ ser = constructBeanOrAddOnSerializer (ctxt , type , beanDesc , formatOverrides , staticTyping );
223
223
// Finally: maybe we can still deal with it as an implementation of some basic JDK interface?
224
224
if (ser == null ) {
225
- ser = findSerializerByAddonType (ctxt , type , beanDesc , formatOverrides , staticTyping );
226
- // 18-Sep-2014, tatu: Actually, as per [jackson-databind#539], need to get
227
- // 'unknown' serializer assigned earlier, here, so that it gets properly
228
- // post-processed
229
- if (ser == null ) {
230
- ser = ctxt .getUnknownTypeSerializer (beanDesc .getBeanClass ());
231
- }
225
+ ser = ctxt .getUnknownTypeSerializer (beanDesc .getBeanClass ());
232
226
}
233
227
}
234
228
}
@@ -242,43 +236,31 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider ctxt,
242
236
return ser ;
243
237
}
244
238
245
- /*
246
- /**********************************************************************
247
- /* Other public methods that are not part of `SerializerFactory` API
248
- /**********************************************************************
249
- */
250
-
251
- /**
252
- * Method that will try to construct a {@link BeanSerializer} for
253
- * given class. Returns null if no properties are found.
254
- */
255
- public JsonSerializer <Object > findBeanSerializer (SerializerProvider ctxt , BeanDescription beanDesc ,
256
- JavaType type , JsonFormat .Value format )
257
- throws JsonMappingException
258
- {
259
- // First things first: we know some types are not beans...
260
- if (!isPotentialBeanType (type .getRawClass ())) {
261
- // Except we do need to allow serializers for Enums, if shape dictates (which it does
262
- // if we end up here)
263
- if (!type .isEnumType ()) {
264
- return null ;
265
- }
266
- }
267
- return constructBeanSerializer (ctxt , beanDesc );
268
- }
269
-
270
239
/*
271
240
/**********************************************************************
272
241
/* Overridable non-public factory methods
273
242
/**********************************************************************
274
243
*/
275
244
276
245
/**
277
- * Method called to construct serializer for serializing specified bean type.
246
+ * Method called to construct serializer based on checking which condition is matched:
247
+ * <ol>
248
+ * <li>Nominal type is {@code java.lang.Object}: if so, return special "no type known" serializer
249
+ * </li>
250
+ * <li>If a known "not-POJO" type (like JDK {@code Proxy}), return {@code null}
251
+ * </li>
252
+ * <li>If at least one logical property found, build actual {@code BeanSerializer}
253
+ * </li>
254
+ * <li>If add-on type (like {@link java.lang.Iterable}) found, create appropriate serializer
255
+ * </li>
256
+ * <li>If one of Jackson's "well-known" annotations found, create bogus "empty Object" Serializer
257
+ * </li>
258
+ * </ol>
259
+ * or, if none matched, return {@code null}.
278
260
*/
279
261
@ SuppressWarnings ("unchecked" )
280
- protected JsonSerializer <Object > constructBeanSerializer (SerializerProvider ctxt ,
281
- BeanDescription beanDesc )
262
+ protected JsonSerializer <Object > constructBeanOrAddOnSerializer (SerializerProvider ctxt ,
263
+ JavaType type , BeanDescription beanDesc , JsonFormat . Value format , boolean staticTyping )
282
264
throws JsonMappingException
283
265
{
284
266
// 13-Oct-2010, tatu: quick sanity check: never try to create bean serializer for plain Object
@@ -287,6 +269,16 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider ctxt
287
269
return ctxt .getUnknownTypeSerializer (Object .class );
288
270
// throw new IllegalArgumentException("Cannot create bean serializer for Object.class");
289
271
}
272
+
273
+ // We also know some types are not beans...
274
+ if (!isPotentialBeanType (type .getRawClass ())) {
275
+ // Except we do need to allow serializers for Enums, if shape dictates (which it does
276
+ // if we end up here)
277
+ if (!type .isEnumType ()) {
278
+ return null ;
279
+ }
280
+ }
281
+
290
282
final SerializationConfig config = ctxt .getConfig ();
291
283
BeanSerializerBuilder builder = constructBeanSerializerBuilder (beanDesc );
292
284
builder .setConfig (config );
@@ -329,18 +321,18 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider ctxt
329
321
330
322
AnnotatedMember anyGetter = beanDesc .findAnyGetter ();
331
323
if (anyGetter != null ) {
332
- JavaType type = anyGetter .getType ();
324
+ JavaType anyType = anyGetter .getType ();
333
325
// copied from BasicSerializerFactory.buildMapSerializer():
334
- boolean staticTyping = config .isEnabled (MapperFeature .USE_STATIC_TYPING );
335
- JavaType valueType = type .getContentType ();
326
+ JavaType valueType = anyType .getContentType ();
336
327
TypeSerializer typeSer = ctxt .findTypeSerializer (valueType );
337
328
// last 2 nulls; don't know key, value serializers (yet)
338
329
// 23-Feb-2015, tatu: As per [databind#705], need to support custom serializers
339
330
JsonSerializer <?> anySer = findSerializerFromAnnotation (ctxt , anyGetter );
340
331
if (anySer == null ) {
341
332
// TODO: support '@JsonIgnoreProperties' with any setter?
342
333
anySer = MapSerializer .construct (/* ignored props*/ (Set <String >) null ,
343
- type , staticTyping , typeSer , null , null , /*filterId*/ null );
334
+ anyType , config .isEnabled (MapperFeature .USE_STATIC_TYPING ),
335
+ typeSer , null , null , /*filterId*/ null );
344
336
}
345
337
// TODO: can we find full PropertyName?
346
338
PropertyName name = PropertyName .construct (anyGetter .getName ());
@@ -366,11 +358,15 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider ctxt
366
358
beanDesc .getType (), e .getClass ().getName (), e .getMessage ());
367
359
}
368
360
if (ser == null ) {
369
- // If we get this far, there were no properties found, so no regular BeanSerializer
370
- // would be constructed. But, couple of exceptions.
371
- // First: if there are known annotations, just create 'empty bean' serializer
372
- if (beanDesc .hasKnownClassAnnotations ()) {
373
- return builder .createDummy ();
361
+ // [databind#2390]: Need to consider add-ons before fallback "empty" serializer
362
+ ser = (JsonSerializer <Object >) findSerializerByAddonType (ctxt , type , beanDesc , format , staticTyping );
363
+ if (ser == null ) {
364
+ // If we get this far, there were no properties found, so no regular BeanSerializer
365
+ // would be constructed. But, couple of exceptions.
366
+ // First: if there are known annotations, just create 'empty bean' serializer
367
+ if (beanDesc .hasKnownClassAnnotations ()) {
368
+ return builder .createDummy ();
369
+ }
374
370
}
375
371
}
376
372
return ser ;
0 commit comments