43
43
*/
44
44
public class Jackson2JsonRedisSerializer <T > implements RedisSerializer <T > {
45
45
46
+ /**
47
+ * @deprecated since 3.0 for removal.
48
+ */
49
+ @ Deprecated (since = "3.0" , forRemoval = true ) //
46
50
public static final Charset DEFAULT_CHARSET = StandardCharsets .UTF_8 ;
47
51
48
52
private final JavaType javaType ;
49
53
50
- private ObjectMapper objectMapper = new ObjectMapper () ;
54
+ private ObjectMapper mapper ;
51
55
52
- private JacksonObjectReader reader = JacksonObjectReader . create () ;
56
+ private final JacksonObjectReader reader ;
53
57
54
- private JacksonObjectWriter writer = JacksonObjectWriter . create () ;
58
+ private final JacksonObjectWriter writer ;
55
59
56
60
/**
57
61
* Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link Class}.
58
62
*
59
- * @param type
63
+ * @param type must not be {@literal null}.
60
64
*/
61
65
public Jackson2JsonRedisSerializer (Class <T > type ) {
62
- this . javaType = getJavaType ( type );
66
+ this ( new ObjectMapper (), type );
63
67
}
64
68
65
69
/**
66
70
* Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
67
71
*
68
- * @param javaType
72
+ * @param javaType must not be {@literal null}.
69
73
*/
70
74
public Jackson2JsonRedisSerializer (JavaType javaType ) {
75
+ this (new ObjectMapper (), javaType );
76
+ }
77
+
78
+ /**
79
+ * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link Class}.
80
+ *
81
+ * @param mapper must not be {@literal null}.
82
+ * @param type must not be {@literal null}.
83
+ * @since 3.0
84
+ */
85
+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , Class <T > type ) {
86
+
87
+ Assert .notNull (mapper , "ObjectMapper must not be null" );
88
+ Assert .notNull (type , "Java type must not be null" );
89
+
90
+ this .javaType = getJavaType (type );
91
+ this .mapper = mapper ;
92
+ this .reader = JacksonObjectReader .create ();
93
+ this .writer = JacksonObjectWriter .create ();
94
+ }
95
+
96
+ /**
97
+ * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
98
+ *
99
+ * @param mapper must not be {@literal null}.
100
+ * @param javaType must not be {@literal null}.
101
+ * @since 3.0
102
+ */
103
+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , JavaType javaType ) {
104
+ this (mapper , javaType , JacksonObjectReader .create (), JacksonObjectWriter .create ());
105
+ }
106
+
107
+ /**
108
+ * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
109
+ *
110
+ * @param mapper must not be {@literal null}.
111
+ * @param javaType must not be {@literal null}.
112
+ * @param reader the {@link JacksonObjectReader} function to read objects using {@link ObjectMapper}.
113
+ * @param writer the {@link JacksonObjectWriter} function to write objects using {@link ObjectMapper}.
114
+ * @since 3.0
115
+ */
116
+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , JavaType javaType , JacksonObjectReader reader ,
117
+ JacksonObjectWriter writer ) {
118
+
119
+ Assert .notNull (mapper , "ObjectMapper must not be null!" );
120
+ Assert .notNull (reader , "Reader must not be null!" );
121
+ Assert .notNull (writer , "Writer must not be null!" );
122
+
123
+ this .mapper = mapper ;
124
+ this .reader = reader ;
125
+ this .writer = writer ;
71
126
this .javaType = javaType ;
72
127
}
73
128
@@ -78,7 +133,7 @@ public T deserialize(@Nullable byte[] bytes) throws SerializationException {
78
133
return null ;
79
134
}
80
135
try {
81
- return (T ) this .reader .read (this .objectMapper , bytes , javaType );
136
+ return (T ) this .reader .read (this .mapper , bytes , javaType );
82
137
} catch (Exception ex ) {
83
138
throw new SerializationException ("Could not read JSON: " + ex .getMessage (), ex );
84
139
}
@@ -91,7 +146,7 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
91
146
return SerializationUtils .EMPTY_ARRAY ;
92
147
}
93
148
try {
94
- return this .writer .write (this .objectMapper , t );
149
+ return this .writer .write (this .mapper , t );
95
150
} catch (Exception ex ) {
96
151
throw new SerializationException ("Could not write JSON: " + ex .getMessage (), ex );
97
152
}
@@ -105,31 +160,15 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
105
160
* process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for
106
161
* specific types. The other option for refining the serialization process is to use Jackson's provided annotations on
107
162
* the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
108
- */
109
- public void setObjectMapper (ObjectMapper objectMapper ) {
110
-
111
- Assert .notNull (objectMapper , "'objectMapper' must not be null" );
112
- this .objectMapper = objectMapper ;
113
- }
114
-
115
- /**
116
- * Sets the {@link JacksonObjectReader} for this serializer. Setting the reader allows customization of the JSON
117
- * deserialization.
118
163
*
119
- * @since 3.0
164
+ * @deprecated since 3.0, use {@link #Jackson2JsonRedisSerializer(ObjectMapper, Class) constructor creation} to
165
+ * configure the object mapper.
120
166
*/
121
- public void setReader (JacksonObjectReader reader ) {
122
- this .reader = reader ;
123
- }
167
+ @ Deprecated (since = "3.0" , forRemoval = true )
168
+ public void setObjectMapper (ObjectMapper mapper ) {
124
169
125
- /**
126
- * Sets the {@link JacksonObjectWriter} for this serializer. Setting the reader allows customization of the JSON
127
- * serialization.
128
- *
129
- * @since 3.0
130
- */
131
- public void setWriter (JacksonObjectWriter writer ) {
132
- this .writer = writer ;
170
+ Assert .notNull (mapper , "'objectMapper' must not be null" );
171
+ this .mapper = mapper ;
133
172
}
134
173
135
174
/**
0 commit comments