53
53
import com .fasterxml .jackson .databind .ObjectMapper .DefaultTyping ;
54
54
import com .fasterxml .jackson .databind .SerializationFeature ;
55
55
import com .fasterxml .jackson .databind .SerializerProvider ;
56
+ import com .fasterxml .jackson .databind .deser .BeanDeserializerFactory ;
57
+ import com .fasterxml .jackson .databind .deser .Deserializers ;
56
58
import com .fasterxml .jackson .databind .deser .std .UntypedObjectDeserializer ;
57
59
import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
58
60
import com .fasterxml .jackson .databind .jsontype .TypeDeserializer ;
@@ -169,17 +171,30 @@ public Jackson2HashMapper(boolean flatten) {
169
171
@ Override
170
172
protected TypeResolverBuilder <?> _constructDefaultTypeResolverBuilder (DefaultTyping applicability ,
171
173
PolymorphicTypeValidator ptv ) {
174
+
172
175
return new DefaultTypeResolverBuilder (applicability , ptv ) {
176
+
177
+ Map <Class , Boolean > serializerPresentCache = new HashMap <>();
178
+
173
179
public boolean useForType (JavaType t ) {
174
180
175
181
if (t .isPrimitive ()) {
176
182
return false ;
177
183
}
178
184
179
185
if (EVERYTHING .equals (_appliesFor )) {
180
- // yuck! Isn't there a better way to distinguish whether there's a registered serializer so that we don't
181
- // use type builders?
182
- if (t .getRawClass ().getPackage ().getName ().startsWith ("java.time" )) {
186
+
187
+ while (t .isArrayType ()) {
188
+ t = t .getContentType ();
189
+ }
190
+ while (t .isReferenceType ()) {
191
+ t = t .getReferencedType ();
192
+ }
193
+
194
+ /*
195
+ * check for registered serializers and make uses of those.
196
+ */
197
+ if (serializerPresentCache .computeIfAbsent (t .getRawClass (), this ::hasConfiguredSerializer )) {
183
198
return false ;
184
199
}
185
200
@@ -188,6 +203,23 @@ public boolean useForType(JavaType t) {
188
203
189
204
return super .useForType (t );
190
205
}
206
+
207
+ private Boolean hasConfiguredSerializer (Class key ) {
208
+
209
+ if (!(_deserializationContext .getFactory () instanceof BeanDeserializerFactory )) {
210
+ return false ;
211
+ }
212
+
213
+ Iterator <Deserializers > deserializers = ((BeanDeserializerFactory ) _deserializationContext .getFactory ())
214
+ .getFactoryConfig ().deserializers ().iterator ();
215
+ while (deserializers .hasNext ()) {
216
+ Deserializers next = deserializers .next ();
217
+ if (next .hasDeserializerFor (_deserializationConfig , key )) {
218
+ return true ;
219
+ }
220
+ }
221
+ return false ;
222
+ }
191
223
};
192
224
}
193
225
}.findAndRegisterModules (), flatten );
0 commit comments