Skip to content

Commit 2a6755f

Browse files
Lookup jackson config to figure out what to do with certain types (eg. java.time)
Related: #1566 Original Pull Request: #2253
1 parent 2fc29d9 commit 2a6755f

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java

+35-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
5454
import com.fasterxml.jackson.databind.SerializationFeature;
5555
import com.fasterxml.jackson.databind.SerializerProvider;
56+
import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
57+
import com.fasterxml.jackson.databind.deser.Deserializers;
5658
import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
5759
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
5860
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
@@ -169,17 +171,30 @@ public Jackson2HashMapper(boolean flatten) {
169171
@Override
170172
protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyping applicability,
171173
PolymorphicTypeValidator ptv) {
174+
172175
return new DefaultTypeResolverBuilder(applicability, ptv) {
176+
177+
Map<Class, Boolean> serializerPresentCache = new HashMap<>();
178+
173179
public boolean useForType(JavaType t) {
174180

175181
if (t.isPrimitive()) {
176182
return false;
177183
}
178184

179185
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)) {
183198
return false;
184199
}
185200

@@ -188,6 +203,23 @@ public boolean useForType(JavaType t) {
188203

189204
return super.useForType(t);
190205
}
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+
}
191223
};
192224
}
193225
}.findAndRegisterModules(), flatten);

0 commit comments

Comments
 (0)