49
49
import com .fasterxml .jackson .databind .type .TypeFactory ;
50
50
51
51
/**
52
- * Generic Jackson 2-based {@link RedisSerializer} that maps {@link Object objects} to and from {@literal JSON}
53
- * using dynamic typing.
52
+ * Generic Jackson 2-based {@link RedisSerializer} that maps {@link Object objects} to and from {@literal JSON} using
53
+ * dynamic typing.
54
54
* <p>
55
- * {@literal JSON} reading and writing can be customized by configuring a {@link JacksonObjectReader}
56
- * and {@link JacksonObjectWriter}.
55
+ * {@literal JSON} reading and writing can be customized by configuring a {@link JacksonObjectReader} and
56
+ * {@link JacksonObjectWriter}.
57
57
*
58
58
* @author Christoph Strobl
59
59
* @author Mark Paluch
66
66
*/
67
67
public class GenericJackson2JsonRedisSerializer implements RedisSerializer <Object > {
68
68
69
-
70
69
private final JacksonObjectReader reader ;
71
70
72
71
private final JacksonObjectWriter writer ;
@@ -78,22 +77,22 @@ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Objec
78
77
private final TypeResolver typeResolver ;
79
78
80
79
/**
81
- * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
82
- * default typing.
80
+ * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for default
81
+ * typing.
83
82
*/
84
83
public GenericJackson2JsonRedisSerializer () {
85
84
this ((String ) null );
86
85
}
87
86
88
87
/**
89
- * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
90
- * default typing using the given {@link String name}.
88
+ * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for default
89
+ * typing using the given {@link String name}.
91
90
* <p>
92
- * In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS}
93
- * will be used.
91
+ * In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS} will be
92
+ * used.
94
93
*
95
- * @param classPropertyTypeName {@link String name} of the JSON property holding type information;
96
- * can be {@literal null}.
94
+ * @param classPropertyTypeName {@link String name} of the JSON property holding type information; can be
95
+ * {@literal null}.
97
96
* @see ObjectMapper#activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
98
97
* @see ObjectMapper#activateDefaultTyping(PolymorphicTypeValidator, DefaultTyping, As)
99
98
*/
@@ -102,15 +101,15 @@ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName
102
101
}
103
102
104
103
/**
105
- * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
106
- * default typing using the given {@link String name} along with the given, required {@link JacksonObjectReader}
107
- * and {@link JacksonObjectWriter} used to read/write {@link Object Objects} de/serialized as JSON.
104
+ * Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for default
105
+ * typing using the given {@link String name} along with the given, required {@link JacksonObjectReader} and
106
+ * {@link JacksonObjectWriter} used to read/write {@link Object Objects} de/serialized as JSON.
108
107
* <p>
109
- * In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS}
110
- * will be used.
108
+ * In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS} will be
109
+ * used.
111
110
*
112
- * @param classPropertyTypeName {@link String name} of the JSON property holding type information;
113
- * can be {@literal null}.
111
+ * @param classPropertyTypeName {@link String name} of the JSON property holding type information; can be
112
+ * {@literal null}.
114
113
* @param reader {@link JacksonObjectReader} function to read objects using {@link ObjectMapper}.
115
114
* @param writer {@link JacksonObjectWriter} function to write objects using {@link ObjectMapper}.
116
115
* @see ObjectMapper#activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
@@ -124,8 +123,7 @@ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName
124
123
125
124
registerNullValueSerializer (this .mapper , classPropertyTypeName );
126
125
127
- StdTypeResolverBuilder typer = TypeResolverBuilder .forEverything (this .mapper )
128
- .init (JsonTypeInfo .Id .CLASS , null )
126
+ StdTypeResolverBuilder typer = TypeResolverBuilder .forEverything (this .mapper ).init (JsonTypeInfo .Id .CLASS , null )
129
127
.inclusion (JsonTypeInfo .As .PROPERTY );
130
128
131
129
if (StringUtils .hasText (classPropertyTypeName )) {
@@ -169,8 +167,7 @@ private GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectRea
169
167
this .reader = RedisAssertions .requireNonNull (reader , "Reader must not be null" );
170
168
this .writer = RedisAssertions .requireNonNull (writer , "Writer must not be null" );
171
169
172
- this .defaultTypingEnabled = Lazy .of (() -> mapper .getSerializationConfig ()
173
- .getDefaultTyper (null ) != null );
170
+ this .defaultTypingEnabled = Lazy .of (() -> mapper .getSerializationConfig ().getDefaultTyper (null ) != null );
174
171
175
172
this .typeResolver = new TypeResolver (Lazy .of (mapper ::getTypeFactory ),
176
173
newTypeHintPropertyNameSupplier (mapper , typeHintPropertyName , this .defaultTypingEnabled ));
@@ -180,12 +177,13 @@ private Supplier<String> newTypeHintPropertyNameSupplier(ObjectMapper mapper, @N
180
177
Lazy <Boolean > defaultTypingEnabled ) {
181
178
182
179
return typeHintPropertyName != null ? () -> typeHintPropertyName
183
- : Lazy .of (() -> defaultTypingEnabled .get () ? null
184
- : mapper .getDeserializationConfig ().getDefaultTyper (null )
185
- .buildTypeDeserializer (mapper .getDeserializationConfig (),
186
- mapper .getTypeFactory ().constructType (Object .class ), Collections .emptyList ())
187
- .getPropertyName ())
188
- .or ("@class" );
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" );
189
187
}
190
188
191
189
/**
@@ -205,8 +203,8 @@ public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nulla
205
203
}
206
204
207
205
/**
208
- * Gets the configured {@link ObjectMapper} used internally by this {@link GenericJackson2JsonRedisSerializer}
209
- * to de/serialize {@link Object objects} as {@literal JSON}.
206
+ * Gets the configured {@link ObjectMapper} used internally by this {@link GenericJackson2JsonRedisSerializer} to
207
+ * de/serialize {@link Object objects} as {@literal JSON}.
210
208
*
211
209
* @return the configured {@link ObjectMapper}.
212
210
*/
@@ -235,17 +233,17 @@ public Object deserialize(@Nullable byte[] source) throws SerializationException
235
233
}
236
234
237
235
/**
238
- * Deserialized the array of bytes containing {@literal JSON} as an {@link Object} of the given,
239
- * required {@link Class type}.
236
+ * Deserialized the array of bytes containing {@literal JSON} as an {@link Object} of the given, required {@link Class
237
+ * type}.
240
238
*
241
239
* @param source array of bytes containing the {@literal JSON} to deserialize; can be {@literal null}.
242
- * @param type {@link Class type} of {@link Object} from which the {@literal JSON} will be deserialized;
243
- * must not be {@literal null}.
244
- * @return {@literal null} for an empty source, or an {@link Object} of the given {@link Class type}
245
- * deserialized from the array of bytes containing {@literal JSON}.
240
+ * @param type {@link Class type} of {@link Object} from which the {@literal JSON} will be deserialized; must not be
241
+ * {@literal null}.
242
+ * @return {@literal null} for an empty source, or an {@link Object} of the given {@link Class type} deserialized from
243
+ * the array of bytes containing {@literal JSON}.
246
244
* @throws IllegalArgumentException if the given {@link Class type} is {@literal null}.
247
- * @throws SerializationException if the array of bytes cannot be deserialized as an instance of
248
- * the given {@link Class type}
245
+ * @throws SerializationException if the array of bytes cannot be deserialized as an instance of the given
246
+ * {@link Class type}
249
247
*/
250
248
@ Nullable
251
249
@ SuppressWarnings ("unchecked" )
@@ -267,20 +265,19 @@ public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws Serializ
267
265
}
268
266
269
267
/**
270
- * Builder method used to configure and customize the internal Jackson {@link ObjectMapper} created by
271
- * this {@link GenericJackson2JsonRedisSerializer} and used to de/serialize {@link Object objects}
272
- * as {@literal JSON}.
268
+ * Builder method used to configure and customize the internal Jackson {@link ObjectMapper} created by this
269
+ * {@link GenericJackson2JsonRedisSerializer} and used to de/serialize {@link Object objects} as {@literal JSON}.
273
270
*
274
271
* @param objectMapperConfigurer {@link Consumer} used to configure and customize the internal {@link ObjectMapper};
275
- * must not be {@literal null}.
272
+ * must not be {@literal null}.
276
273
* @return this {@link GenericJackson2JsonRedisSerializer}.
277
- * @throws IllegalArgumentException if the {@link Consumer} used to configure and customize
278
- * the internal {@link ObjectMapper} is {@literal null}.
274
+ * @throws IllegalArgumentException if the {@link Consumer} used to configure and customize the internal
275
+ * {@link ObjectMapper} is {@literal null}.
276
+ * @since 3.1.5
279
277
*/
280
278
public GenericJackson2JsonRedisSerializer configure (Consumer <ObjectMapper > objectMapperConfigurer ) {
281
279
282
- Assert .notNull (objectMapperConfigurer ,
283
- "Consumer used to configure and customize ObjectMapper must not be null" );
280
+ Assert .notNull (objectMapperConfigurer , "Consumer used to configure and customize ObjectMapper must not be null" );
284
281
285
282
objectMapperConfigurer .accept (getObjectMapper ());
286
283
@@ -339,8 +336,7 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException
339
336
*/
340
337
private static class NullValueSerializer extends StdSerializer <NullValue > {
341
338
342
- @ Serial
343
- private static final long serialVersionUID = 1999052150548658808L ;
339
+ @ Serial private static final long serialVersionUID = 1999052150548658808L ;
344
340
345
341
private final String classIdentifier ;
346
342
0 commit comments