20
20
import org .springframework .core .serializer .DefaultSerializer ;
21
21
import org .springframework .core .serializer .support .DeserializingConverter ;
22
22
import org .springframework .core .serializer .support .SerializingConverter ;
23
+ import org .springframework .data .redis .util .RedisAssertions ;
23
24
import org .springframework .lang .Nullable ;
24
- import org .springframework .util .Assert ;
25
25
26
26
/**
27
- * Java Serialization Redis serializer. Delegates to the default (Java based) {@link DefaultSerializer serializer} and
28
- * {@link DefaultDeserializer}. This {@link RedisSerializer serializer} can be constructed with either custom
29
- * {@link ClassLoader} or own {@link Converter converters}.
27
+ * Java Serialization {@link RedisSerializer}.
28
+ * <p>
29
+ * Delegates to the default (Java-based) {@link DefaultSerializer serializer}
30
+ * and {@link DefaultDeserializer deserializer}.
31
+ * <p>
32
+ * This {@link RedisSerializer serializer} can be constructed with either a custom {@link ClassLoader}
33
+ * or custom {@link Converter converters}.
30
34
*
31
35
* @author Mark Pollack
32
36
* @author Costin Leau
33
37
* @author Mark Paluch
34
38
* @author Christoph Strobl
39
+ * @author John Blum
35
40
*/
36
41
public class JdkSerializationRedisSerializer implements RedisSerializer <Object > {
37
42
38
43
private final Converter <Object , byte []> serializer ;
39
44
private final Converter <byte [], Object > deserializer ;
40
45
41
46
/**
42
- * Creates a new {@link JdkSerializationRedisSerializer} using the default class loader .
47
+ * Creates a new {@link JdkSerializationRedisSerializer} using the default {@link ClassLoader} .
43
48
*/
44
49
public JdkSerializationRedisSerializer () {
45
50
this (new SerializingConverter (), new DeserializingConverter ());
46
51
}
47
52
48
53
/**
49
- * Creates a new {@link JdkSerializationRedisSerializer} using a {@link ClassLoader}.
54
+ * Creates a new {@link JdkSerializationRedisSerializer} with the given {@link ClassLoader} used to
55
+ * resolve {@link Class types} during deserialization.
50
56
*
51
- * @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}.
57
+ * @param classLoader {@link ClassLoader} used to resolve {@link Class types} for deserialization;
58
+ * can be {@literal null}.
52
59
* @since 1.7
53
60
*/
54
61
public JdkSerializationRedisSerializer (@ Nullable ClassLoader classLoader ) {
55
62
this (new SerializingConverter (), new DeserializingConverter (classLoader ));
56
63
}
57
64
58
65
/**
59
- * Creates a new {@link JdkSerializationRedisSerializer} using a {@link Converter converters} to serialize and
60
- * deserialize objects.
66
+ * Creates a new {@link JdkSerializationRedisSerializer} using {@link Converter converters} to serialize and
67
+ * deserialize {@link Object objects} .
61
68
*
62
- * @param serializer must not be {@literal null}
63
- * @param deserializer must not be {@literal null}
69
+ * @param serializer {@link Converter} used to serialize an {@link Object} to a byte array;
70
+ * must not be {@literal null}.
71
+ * @param deserializer {@link Converter} used to deserialize and convert a byte arra into an {@link Object};
72
+ * must not be {@literal null}
73
+ * @throws IllegalArgumentException if either the given {@code serializer} or {@code deserializer}
74
+ * are {@literal null}.
64
75
* @since 1.7
65
76
*/
66
- public JdkSerializationRedisSerializer (Converter <Object , byte []> serializer , Converter <byte [], Object > deserializer ) {
77
+ public JdkSerializationRedisSerializer (Converter <Object , byte []> serializer ,
78
+ Converter <byte [], Object > deserializer ) {
67
79
68
- Assert .notNull (serializer , "Serializer must not be null" );
69
- Assert .notNull (deserializer , "Deserializer must not be null" );
70
-
71
- this .serializer = serializer ;
72
- this .deserializer = deserializer ;
80
+ this .serializer = RedisAssertions .requireObject (serializer , "Serializer must not be null" );
81
+ this .deserializer = RedisAssertions .requireObject (deserializer , "Deserializer must not be null" );
73
82
}
74
83
84
+ @ Override
75
85
public Object deserialize (@ Nullable byte [] bytes ) {
76
86
77
87
if (SerializationUtils .isEmpty (bytes )) {
@@ -80,20 +90,22 @@ public Object deserialize(@Nullable byte[] bytes) {
80
90
81
91
try {
82
92
return deserializer .convert (bytes );
83
- } catch (Exception ex ) {
84
- throw new SerializationException ("Cannot deserialize" , ex );
93
+ } catch (Exception cause ) {
94
+ throw new SerializationException ("Cannot deserialize" , cause );
85
95
}
86
96
}
87
97
88
98
@ Override
89
99
public byte [] serialize (@ Nullable Object object ) {
100
+
90
101
if (object == null ) {
91
102
return SerializationUtils .EMPTY_ARRAY ;
92
103
}
104
+
93
105
try {
94
106
return serializer .convert (object );
95
- } catch (Exception ex ) {
96
- throw new SerializationException ("Cannot serialize" , ex );
107
+ } catch (Exception cause ) {
108
+ throw new SerializationException ("Cannot serialize" , cause );
97
109
}
98
110
}
99
111
}
0 commit comments