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
+ */
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 ;
71
124
this .javaType = javaType ;
72
125
}
73
126
@@ -78,7 +131,7 @@ public T deserialize(@Nullable byte[] bytes) throws SerializationException {
78
131
return null ;
79
132
}
80
133
try {
81
- return (T ) this .reader .read (this .objectMapper , bytes , javaType );
134
+ return (T ) this .reader .read (this .mapper , bytes , javaType );
82
135
} catch (Exception ex ) {
83
136
throw new SerializationException ("Could not read JSON: " + ex .getMessage (), ex );
84
137
}
@@ -91,7 +144,7 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
91
144
return SerializationUtils .EMPTY_ARRAY ;
92
145
}
93
146
try {
94
- return this .writer .write (this .objectMapper , t );
147
+ return this .writer .write (this .mapper , t );
95
148
} catch (Exception ex ) {
96
149
throw new SerializationException ("Could not write JSON: " + ex .getMessage (), ex );
97
150
}
@@ -105,31 +158,15 @@ public byte[] serialize(@Nullable Object t) throws SerializationException {
105
158
* process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for
106
159
* specific types. The other option for refining the serialization process is to use Jackson's provided annotations on
107
160
* 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
161
*
119
- * @since 3.0
162
+ * @deprecated since 3.0, use {@link #Jackson2JsonRedisSerializer(ObjectMapper, Class) constructor creation} to
163
+ * configure the object mapper.
120
164
*/
121
- public void setReader (JacksonObjectReader reader ) {
122
- this .reader = reader ;
123
- }
165
+ @ Deprecated (since = "3.0" , forRemoval = true )
166
+ public void setObjectMapper (ObjectMapper mapper ) {
124
167
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 ;
133
170
}
134
171
135
172
/**
0 commit comments