|
23 | 23 |
|
24 | 24 | import org.springframework.cache.support.NullValue; |
25 | 25 | import org.springframework.core.KotlinDetector; |
26 | | -import org.springframework.data.redis.util.RedisAssertions; |
27 | 26 | import org.springframework.data.util.Lazy; |
28 | 27 | import org.springframework.lang.Nullable; |
29 | 28 | import org.springframework.util.Assert; |
|
34 | 33 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As; |
35 | 34 | import com.fasterxml.jackson.core.JsonGenerator; |
36 | 35 | import com.fasterxml.jackson.core.TreeNode; |
| 36 | +import com.fasterxml.jackson.databind.DeserializationConfig; |
37 | 37 | import com.fasterxml.jackson.databind.JavaType; |
38 | 38 | import com.fasterxml.jackson.databind.JsonNode; |
39 | 39 | import com.fasterxml.jackson.databind.ObjectMapper; |
40 | 40 | import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; |
41 | 41 | import com.fasterxml.jackson.databind.SerializerProvider; |
42 | 42 | import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; |
| 43 | +import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
43 | 44 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; |
44 | 45 | import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder; |
45 | 46 | import com.fasterxml.jackson.databind.module.SimpleModule; |
@@ -163,27 +164,55 @@ public GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectRead |
163 | 164 | private GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader, |
164 | 165 | JacksonObjectWriter writer, @Nullable String typeHintPropertyName) { |
165 | 166 |
|
166 | | - this.mapper = RedisAssertions.requireNonNull(mapper, "ObjectMapper must not be null"); |
167 | | - this.reader = RedisAssertions.requireNonNull(reader, "Reader must not be null"); |
168 | | - this.writer = RedisAssertions.requireNonNull(writer, "Writer must not be null"); |
| 167 | + Assert.notNull(mapper, "ObjectMapper must not be null"); |
| 168 | + Assert.notNull(reader, "Reader must not be null"); |
| 169 | + Assert.notNull(writer, "Writer must not be null"); |
| 170 | + |
| 171 | + this.mapper = mapper; |
| 172 | + this.reader = reader; |
| 173 | + this.writer = writer; |
169 | 174 |
|
170 | 175 | this.defaultTypingEnabled = Lazy.of(() -> mapper.getSerializationConfig().getDefaultTyper(null) != null); |
171 | 176 |
|
172 | | - this.typeResolver = new TypeResolver(Lazy.of(mapper::getTypeFactory), |
173 | | - newTypeHintPropertyNameSupplier(mapper, typeHintPropertyName, this.defaultTypingEnabled)); |
| 177 | + this.typeResolver = newTypeResolver(mapper, typeHintPropertyName, this.defaultTypingEnabled); |
174 | 178 | } |
175 | 179 |
|
176 | | - private Supplier<String> newTypeHintPropertyNameSupplier(ObjectMapper mapper, @Nullable String typeHintPropertyName, |
| 180 | + private TypeResolver newTypeResolver(ObjectMapper mapper, @Nullable String typeHintPropertyName, |
177 | 181 | Lazy<Boolean> defaultTypingEnabled) { |
178 | 182 |
|
179 | | - return typeHintPropertyName != null ? () -> typeHintPropertyName |
180 | | - : Lazy |
181 | | - .of(() -> defaultTypingEnabled.get() ? null |
182 | | - : mapper.getDeserializationConfig().getDefaultTyper(null) |
183 | | - .buildTypeDeserializer(mapper.getDeserializationConfig(), |
184 | | - mapper.getTypeFactory().constructType(Object.class), Collections.emptyList()) |
185 | | - .getPropertyName()) |
186 | | - .or("@class"); |
| 183 | + Lazy<TypeFactory> lazyTypeFactory = Lazy.of(mapper::getTypeFactory); |
| 184 | + |
| 185 | + Lazy<String> lazyTypeHintPropertyName = typeHintPropertyName != null ? Lazy.of(typeHintPropertyName) |
| 186 | + : newLazyTypeHintPropertyName(mapper, defaultTypingEnabled); |
| 187 | + |
| 188 | + return new TypeResolver(lazyTypeFactory, lazyTypeHintPropertyName); |
| 189 | + } |
| 190 | + |
| 191 | + private Lazy<String> newLazyTypeHintPropertyName(ObjectMapper mapper, Lazy<Boolean> defaultTypingEnabled) { |
| 192 | + |
| 193 | + Lazy<String> configuredTypeDeserializationPropertyName = getConfiguredTypeDeserializationPropertyName(mapper); |
| 194 | + |
| 195 | + Lazy<String> resolvedLazyTypeHintPropertyName = Lazy.of(() -> defaultTypingEnabled.get() ? null |
| 196 | + : configuredTypeDeserializationPropertyName.get()); |
| 197 | + |
| 198 | + resolvedLazyTypeHintPropertyName = resolvedLazyTypeHintPropertyName.or("@class"); |
| 199 | + |
| 200 | + return resolvedLazyTypeHintPropertyName; |
| 201 | + } |
| 202 | + |
| 203 | + private Lazy<String> getConfiguredTypeDeserializationPropertyName(ObjectMapper mapper) { |
| 204 | + |
| 205 | + return Lazy.of(() -> { |
| 206 | + |
| 207 | + DeserializationConfig deserializationConfig = mapper.getDeserializationConfig(); |
| 208 | + |
| 209 | + JavaType objectType = mapper.getTypeFactory().constructType(Object.class); |
| 210 | + |
| 211 | + TypeDeserializer typeDeserializer = deserializationConfig.getDefaultTyper(null) |
| 212 | + .buildTypeDeserializer(deserializationConfig, objectType, Collections.emptyList()); |
| 213 | + |
| 214 | + return typeDeserializer.getPropertyName(); |
| 215 | + }); |
187 | 216 | } |
188 | 217 |
|
189 | 218 | /** |
@@ -336,7 +365,8 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException |
336 | 365 | */ |
337 | 366 | private static class NullValueSerializer extends StdSerializer<NullValue> { |
338 | 367 |
|
339 | | - @Serial private static final long serialVersionUID = 1999052150548658808L; |
| 368 | + @Serial |
| 369 | + private static final long serialVersionUID = 1999052150548658808L; |
340 | 370 |
|
341 | 371 | private final String classIdentifier; |
342 | 372 |
|
|
0 commit comments