|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.io.Serial;
|
20 | 20 | import java.util.Collections;
|
| 21 | +import java.util.function.Consumer; |
21 | 22 | import java.util.function.Supplier;
|
22 | 23 |
|
23 | 24 | import org.springframework.cache.support.NullValue;
|
@@ -196,12 +197,22 @@ private Supplier<String> newTypeHintPropertyNameSupplier(ObjectMapper mapper, @N
|
196 | 197 | return typeHintPropertyName != null ? () -> typeHintPropertyName
|
197 | 198 | : Lazy.of(() -> defaultTypingEnabled.get() ? null
|
198 | 199 | : mapper.getDeserializationConfig().getDefaultTyper(null)
|
199 |
| - .buildTypeDeserializer(mapper.getDeserializationConfig(), |
200 |
| - mapper.getTypeFactory().constructType(Object.class), Collections.emptyList()) |
| 200 | + .buildTypeDeserializer(mapper.getDeserializationConfig(), |
| 201 | + mapper.getTypeFactory().constructType(Object.class), Collections.emptyList()) |
201 | 202 | .getPropertyName())
|
202 | 203 | .or("@class");
|
203 | 204 | }
|
204 | 205 |
|
| 206 | + /** |
| 207 | + * Gets the configured {@link ObjectMapper} used internally by this {@link GenericJackson2JsonRedisSerializer} |
| 208 | + * to de/serialize {@link Object objects} as {@literal JSON}. |
| 209 | + * |
| 210 | + * @return the configured {@link ObjectMapper}. |
| 211 | + */ |
| 212 | + protected ObjectMapper getObjectMapper() { |
| 213 | + return this.mapper; |
| 214 | + } |
| 215 | + |
205 | 216 | @Override
|
206 | 217 | public byte[] serialize(@Nullable Object source) throws SerializationException {
|
207 | 218 |
|
@@ -248,11 +259,33 @@ public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws Serializ
|
248 | 259 |
|
249 | 260 | try {
|
250 | 261 | return (T) reader.read(mapper, source, resolveType(source, type));
|
251 |
| - } catch (Exception ex) { |
252 |
| - throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex); |
| 262 | + } catch (Exception cause) { |
| 263 | + String message = String.format("Could not read JSON:%s ", cause.getMessage()); |
| 264 | + throw new SerializationException(message, cause); |
253 | 265 | }
|
254 | 266 | }
|
255 | 267 |
|
| 268 | + /** |
| 269 | + * Builder method used to configure and customize the internal Jackson {@link ObjectMapper} created by |
| 270 | + * this {@link GenericJackson2JsonRedisSerializer} and used to de/serialize {@link Object objects} |
| 271 | + * as {@literal JSON}. |
| 272 | + * |
| 273 | + * @param objectMapperConfigurer {@link Consumer} used to configure and customize the internal {@link ObjectMapper}; |
| 274 | + * must not be {@literal null}. |
| 275 | + * @return this {@link GenericJackson2JsonRedisSerializer}. |
| 276 | + * @throws IllegalArgumentException if the {@link Consumer} used to configure and customize |
| 277 | + * the internal {@link ObjectMapper} is {@literal null}. |
| 278 | + */ |
| 279 | + public GenericJackson2JsonRedisSerializer configure(Consumer<ObjectMapper> objectMapperConfigurer) { |
| 280 | + |
| 281 | + Assert.notNull(objectMapperConfigurer, |
| 282 | + "Consumer used to configure and customize ObjectMapper must not be null"); |
| 283 | + |
| 284 | + objectMapperConfigurer.accept(getObjectMapper()); |
| 285 | + |
| 286 | + return this; |
| 287 | + } |
| 288 | + |
256 | 289 | protected JavaType resolveType(byte[] source, Class<?> type) throws IOException {
|
257 | 290 |
|
258 | 291 | if (!type.equals(Object.class) || !defaultTypingEnabled.get()) {
|
|
0 commit comments