|
23 | 23 | import org.springframework.data.util.Lazy;
|
24 | 24 | import org.springframework.lang.Nullable;
|
25 | 25 | import org.springframework.util.Assert;
|
| 26 | +import org.springframework.util.ClassUtils; |
26 | 27 | import org.springframework.util.StringUtils;
|
27 | 28 |
|
28 | 29 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
29 | 30 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
30 | 31 | import com.fasterxml.jackson.core.JsonGenerator;
|
| 32 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 33 | +import com.fasterxml.jackson.core.TreeNode; |
31 | 34 | import com.fasterxml.jackson.databind.JavaType;
|
32 | 35 | import com.fasterxml.jackson.databind.JsonNode;
|
33 | 36 | import com.fasterxml.jackson.databind.ObjectMapper;
|
34 | 37 | import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
35 | 38 | import com.fasterxml.jackson.databind.SerializerProvider;
|
36 | 39 | import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
37 | 40 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
| 41 | +import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder; |
38 | 42 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
39 | 43 | import com.fasterxml.jackson.databind.node.TextNode;
|
40 | 44 | import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
@@ -105,12 +109,15 @@ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName
|
105 | 109 | // the type hint embedded for deserialization using the default typing feature.
|
106 | 110 | registerNullValueSerializer(mapper, classPropertyTypeName);
|
107 | 111 |
|
| 112 | + StdTypeResolverBuilder typer = new TypeResolverBuilder(DefaultTyping.EVERYTHING, |
| 113 | + mapper.getPolymorphicTypeValidator()); |
| 114 | + typer = typer.init(JsonTypeInfo.Id.CLASS, null); |
| 115 | + typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); |
| 116 | + |
108 | 117 | if (StringUtils.hasText(classPropertyTypeName)) {
|
109 |
| - mapper.activateDefaultTypingAsProperty(mapper.getPolymorphicTypeValidator(), DefaultTyping.EVERYTHING, |
110 |
| - classPropertyTypeName); |
111 |
| - } else { |
112 |
| - mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), DefaultTyping.EVERYTHING, As.PROPERTY); |
| 118 | + typer = typer.typeProperty(classPropertyTypeName); |
113 | 119 | }
|
| 120 | + mapper.setDefaultTyping(typer); |
114 | 121 | }
|
115 | 122 |
|
116 | 123 | /**
|
@@ -311,4 +318,52 @@ public void serializeWithType(NullValue value, JsonGenerator gen, SerializerProv
|
311 | 318 | serialize(value, gen, serializers);
|
312 | 319 | }
|
313 | 320 | }
|
| 321 | + |
| 322 | + /** |
| 323 | + * Custom {@link StdTypeResolverBuilder} that considers typing for non-primitive types. Primitives, their wrappers and |
| 324 | + * primitive arrays do not require type hints. The default {@code DefaultTyping#EVERYTHING} typing does not satisfy |
| 325 | + * those requirements. |
| 326 | + * |
| 327 | + * @author Mark Paluch |
| 328 | + * @since 2.7.2 |
| 329 | + */ |
| 330 | + private static class TypeResolverBuilder extends ObjectMapper.DefaultTypeResolverBuilder { |
| 331 | + |
| 332 | + public TypeResolverBuilder(DefaultTyping t, PolymorphicTypeValidator ptv) { |
| 333 | + super(t, ptv); |
| 334 | + } |
| 335 | + |
| 336 | + @Override |
| 337 | + public ObjectMapper.DefaultTypeResolverBuilder withDefaultImpl(Class<?> defaultImpl) { |
| 338 | + return this; |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Method called to check if the default type handler should be used for given type. Note: "natural types" (String, |
| 343 | + * Boolean, Integer, Double) will never use typing; that is both due to them being concrete and final, and since |
| 344 | + * actual serializers and deserializers will also ignore any attempts to enforce typing. |
| 345 | + */ |
| 346 | + public boolean useForType(JavaType t) { |
| 347 | + |
| 348 | + if (t.isJavaLangObject()) { |
| 349 | + return true; |
| 350 | + } |
| 351 | + |
| 352 | + while (t.isArrayType()) { |
| 353 | + t = t.getContentType(); |
| 354 | + } |
| 355 | + |
| 356 | + if (ClassUtils.isPrimitiveOrWrapper(t.getRawClass())) { |
| 357 | + return false; |
| 358 | + } |
| 359 | + |
| 360 | + // 19-Apr-2016, tatu: ReferenceType like Optional also requires similar handling: |
| 361 | + while (t.isReferenceType()) { |
| 362 | + t = t.getReferencedType(); |
| 363 | + } |
| 364 | + |
| 365 | + // [databind#88] Should not apply to JSON tree models: |
| 366 | + return !TreeNode.class.isAssignableFrom(t.getRawClass()); |
| 367 | + } |
| 368 | + } |
314 | 369 | }
|
0 commit comments