4343 */
4444public class Jackson2JsonRedisSerializer <T > implements RedisSerializer <T > {
4545
46+ /**
47+ * @deprecated since 3.0 for removal.
48+ */
49+ @ Deprecated (since = "3.0" , forRemoval = true ) //
4650 public static final Charset DEFAULT_CHARSET = StandardCharsets .UTF_8 ;
4751
4852 private final JavaType javaType ;
4953
50- private ObjectMapper objectMapper = new ObjectMapper () ;
54+ private ObjectMapper mapper ;
5155
52- private JacksonObjectReader reader = JacksonObjectReader . create () ;
56+ private final JacksonObjectReader reader ;
5357
54- private JacksonObjectWriter writer = JacksonObjectWriter . create () ;
58+ private final JacksonObjectWriter writer ;
5559
5660 /**
5761 * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link Class}.
5862 *
59- * @param type
63+ * @param type must not be {@literal null}.
6064 */
6165 public Jackson2JsonRedisSerializer (Class <T > type ) {
62- this . javaType = getJavaType ( type );
66+ this ( new ObjectMapper (), type );
6367 }
6468
6569 /**
6670 * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
6771 *
68- * @param javaType
72+ * @param javaType must not be {@literal null}.
6973 */
7074 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+ */
84+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , Class <T > type ) {
85+
86+ Assert .notNull (mapper , "ObjectMapper must not be null" );
87+ Assert .notNull (type , "Java type must not be null" );
88+
89+ this .javaType = getJavaType (type );
90+ this .mapper = mapper ;
91+ this .reader = JacksonObjectReader .create ();
92+ this .writer = JacksonObjectWriter .create ();
93+ }
94+
95+ /**
96+ * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
97+ *
98+ * @param mapper must not be {@literal null}.
99+ * @param javaType must not be {@literal null}.
100+ */
101+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , JavaType javaType ) {
102+ this (mapper , javaType , JacksonObjectReader .create (), JacksonObjectWriter .create ());
103+ }
104+
105+ /**
106+ * Creates a new {@link Jackson2JsonRedisSerializer} for the given target {@link JavaType}.
107+ *
108+ * @param mapper must not be {@literal null}.
109+ * @param javaType must not be {@literal null}.
110+ * @param reader the {@link JacksonObjectReader} function to read objects using {@link ObjectMapper}.
111+ * @param writer the {@link JacksonObjectWriter} function to write objects using {@link ObjectMapper}.
112+ * @since 3.0
113+ */
114+ public Jackson2JsonRedisSerializer (ObjectMapper mapper , JavaType javaType , JacksonObjectReader reader ,
115+ JacksonObjectWriter writer ) {
116+
117+ Assert .notNull (mapper , "ObjectMapper must not be null!" );
118+ Assert .notNull (reader , "Reader must not be null!" );
119+ Assert .notNull (writer , "Writer must not be null!" );
120+
121+ this .mapper = mapper ;
122+ this .reader = reader ;
123+ this .writer = writer ;
71124 this .javaType = javaType ;
72125 }
73126
@@ -78,7 +131,7 @@ public T deserialize(@Nullable byte[] bytes) throws SerializationException {
78131 return null ;
79132 }
80133 try {
81- return (T ) this .reader .read (this .objectMapper , bytes , javaType );
134+ return (T ) this .reader .read (this .mapper , bytes , javaType );
82135 } catch (Exception ex ) {
83136 throw new SerializationException ("Could not read JSON: " + ex .getMessage (), ex );
84137 }
@@ -91,7 +144,7 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
91144 return SerializationUtils .EMPTY_ARRAY ;
92145 }
93146 try {
94- return this .writer .write (this .objectMapper , t );
147+ return this .writer .write (this .mapper , t );
95148 } catch (Exception ex ) {
96149 throw new SerializationException ("Could not write JSON: " + ex .getMessage (), ex );
97150 }
@@ -105,31 +158,15 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
105158 * process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for
106159 * specific types. The other option for refining the serialization process is to use Jackson's provided annotations on
107160 * 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.
118161 *
119- * @since 3.0
162+ * @deprecated since 3.0, use {@link #Jackson2JsonRedisSerializer(ObjectMapper, Class) constructor creation} to
163+ * configure the object mapper.
120164 */
121- public void setReader (JacksonObjectReader reader ) {
122- this .reader = reader ;
123- }
165+ @ Deprecated (since = "3.0" , forRemoval = true )
166+ public void setObjectMapper (ObjectMapper mapper ) {
124167
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 ;
168+ Assert .notNull (mapper , "'objectMapper' must not be null" );
169+ this .mapper = mapper ;
133170 }
134171
135172 /**
0 commit comments