@@ -228,16 +228,12 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider prov,
228
228
// And this is where this class comes in: if type is not a
229
229
// known "primary JDK type", perhaps it's a bean? We can still
230
230
// get a null, if we can't find a single suitable bean property.
231
- ser = findBeanSerializer (prov , type , beanDesc );
232
- // Finally: maybe we can still deal with it as an implementation of some basic JDK interface?
231
+ ser = findBeanOrAddOnSerializer (prov , type , beanDesc , staticTyping );
232
+ // 18-Sep-2014, tatu: Actually, as per [jackson-databind#539], need to get
233
+ // 'unknown' serializer assigned earlier, here, so that it gets properly
234
+ // post-processed
233
235
if (ser == null ) {
234
- ser = findSerializerByAddonType (config , type , beanDesc , staticTyping );
235
- // 18-Sep-2014, tatu: Actually, as per [jackson-databind#539], need to get
236
- // 'unknown' serializer assigned earlier, here, so that it gets properly
237
- // post-processed
238
- if (ser == null ) {
239
- ser = prov .getUnknownTypeSerializer (beanDesc .getBeanClass ());
240
- }
236
+ ser = prov .getUnknownTypeSerializer (beanDesc .getBeanClass ());
241
237
}
242
238
}
243
239
}
@@ -260,12 +256,23 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider prov,
260
256
/**********************************************************
261
257
*/
262
258
259
+ @ Deprecated // since 2.10
260
+ public JsonSerializer <Object > findBeanSerializer (SerializerProvider prov , JavaType type ,
261
+ BeanDescription beanDesc )
262
+ throws JsonMappingException
263
+ {
264
+ return findBeanOrAddOnSerializer (prov , type , beanDesc , prov .isEnabled (MapperFeature .USE_STATIC_TYPING ));
265
+ }
266
+
263
267
/**
264
268
* Method that will try to construct a {@link BeanSerializer} for
265
- * given class. Returns null if no properties are found.
269
+ * given class if at least one property is found, OR, if not,
270
+ * one of add-on types.
271
+ *<p>
272
+ * NOTE: behavior changed a bit
266
273
*/
267
- public JsonSerializer <Object > findBeanSerializer (SerializerProvider prov , JavaType type ,
268
- BeanDescription beanDesc )
274
+ public JsonSerializer <Object > findBeanOrAddOnSerializer (SerializerProvider prov , JavaType type ,
275
+ BeanDescription beanDesc , boolean staticTyping )
269
276
throws JsonMappingException
270
277
{
271
278
// First things first: we know some types are not beans...
@@ -276,7 +283,7 @@ public JsonSerializer<Object> findBeanSerializer(SerializerProvider prov, JavaTy
276
283
return null ;
277
284
}
278
285
}
279
- return constructBeanSerializer (prov , beanDesc );
286
+ return constructBeanOrAddOnSerializer (prov , type , beanDesc , staticTyping );
280
287
}
281
288
282
289
/**
@@ -344,14 +351,23 @@ public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType,
344
351
/**********************************************************
345
352
*/
346
353
354
+ @ Deprecated // since 2.10
355
+ protected JsonSerializer <Object > constructBeanSerializer (SerializerProvider prov ,
356
+ BeanDescription beanDesc )
357
+ throws JsonMappingException
358
+ {
359
+ return constructBeanOrAddOnSerializer (prov , beanDesc .getType (), beanDesc , prov .isEnabled (MapperFeature .USE_STATIC_TYPING ));
360
+ }
361
+
347
362
/**
348
- * Method called to construct serializer for serializing specified bean type.
363
+ * Method called to construct serializer for serializing specified bean type if
364
+ * (but only if, as of 2.10), at least one property is found.
349
365
*
350
- * @since 2.1
366
+ * @since 2.10
351
367
*/
352
368
@ SuppressWarnings ("unchecked" )
353
- protected JsonSerializer <Object > constructBeanSerializer (SerializerProvider prov ,
354
- BeanDescription beanDesc )
369
+ protected JsonSerializer <Object > constructBeanOrAddOnSerializer (SerializerProvider prov ,
370
+ JavaType type , BeanDescription beanDesc , boolean staticTyping )
355
371
throws JsonMappingException
356
372
{
357
373
// 13-Oct-2010, tatu: quick sanity check: never try to create bean serializer for plain Object
@@ -402,18 +418,18 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
402
418
403
419
AnnotatedMember anyGetter = beanDesc .findAnyGetter ();
404
420
if (anyGetter != null ) {
405
- JavaType type = anyGetter .getType ();
421
+ JavaType anyType = anyGetter .getType ();
406
422
// copied from BasicSerializerFactory.buildMapSerializer():
407
- boolean staticTyping = config .isEnabled (MapperFeature .USE_STATIC_TYPING );
408
- JavaType valueType = type .getContentType ();
423
+ JavaType valueType = anyType .getContentType ();
409
424
TypeSerializer typeSer = createTypeSerializer (config , valueType );
410
425
// last 2 nulls; don't know key, value serializers (yet)
411
426
// 23-Feb-2015, tatu: As per [databind#705], need to support custom serializers
412
427
JsonSerializer <?> anySer = findSerializerFromAnnotation (prov , anyGetter );
413
428
if (anySer == null ) {
414
429
// TODO: support '@JsonIgnoreProperties' with any setter?
415
430
anySer = MapSerializer .construct (/* ignored props*/ (Set <String >) null ,
416
- type , staticTyping , typeSer , null , null , /*filterId*/ null );
431
+ anyType , config .isEnabled (MapperFeature .USE_STATIC_TYPING ),
432
+ typeSer , null , null , /*filterId*/ null );
417
433
}
418
434
// TODO: can we find full PropertyName?
419
435
PropertyName name = PropertyName .construct (anyGetter .getName ());
@@ -435,15 +451,20 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
435
451
try {
436
452
ser = (JsonSerializer <Object >) builder .build ();
437
453
} catch (RuntimeException e ) {
438
- prov .reportBadTypeDefinition (beanDesc , "Failed to construct BeanSerializer for %s: (%s) %s" ,
454
+ return prov .reportBadTypeDefinition (beanDesc , "Failed to construct BeanSerializer for %s: (%s) %s" ,
439
455
beanDesc .getType (), e .getClass ().getName (), e .getMessage ());
440
456
}
441
457
if (ser == null ) {
442
- // If we get this far, there were no properties found, so no regular BeanSerializer
443
- // would be constructed. But, couple of exceptions.
444
- // First: if there are known annotations, just create 'empty bean' serializer
445
- if (beanDesc .hasKnownClassAnnotations ()) {
446
- return builder .createDummy ();
458
+ // 06-Aug-2019, tatu: As per [databind#2390], we need to check for add-ons here,
459
+ // before considering fallbacks
460
+ ser = (JsonSerializer <Object >) findSerializerByAddonType (config , type , beanDesc , staticTyping );
461
+ if (ser == null ) {
462
+ // If we get this far, there were no properties found, so no regular BeanSerializer
463
+ // would be constructed. But, couple of exceptions.
464
+ // First: if there are known annotations, just create 'empty bean' serializer
465
+ if (beanDesc .hasKnownClassAnnotations ()) {
466
+ return builder .createDummy ();
467
+ }
447
468
}
448
469
}
449
470
return ser ;
0 commit comments