18
18
import com .fasterxml .jackson .databind .JavaType ;
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
20
import com .fasterxml .jackson .databind .ObjectMapper ;
21
+ import com .fasterxml .jackson .databind .SerializationFeature ;
22
+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
21
23
import com .google .protobuf .MessageLite ;
22
24
import io .dapr .client .domain .CloudEvent ;
23
25
import io .dapr .utils .TypeRef ;
24
26
25
27
import java .io .IOException ;
26
28
import java .lang .reflect .Method ;
29
+ import java .util .Objects ;
27
30
28
31
/**
29
32
* Serializes and deserializes an internal object.
@@ -35,16 +38,29 @@ public class ObjectSerializer {
35
38
*/
36
39
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ()
37
40
.configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
41
+ .registerModule (new JavaTimeModule ())
42
+ .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS )
38
43
.setSerializationInclusion (JsonInclude .Include .NON_NULL );
39
44
45
+ protected ObjectMapper objectMapper ;
46
+
40
47
/**
41
48
* Default constructor to avoid class from being instantiated outside package but still inherited.
42
49
*/
43
50
protected ObjectSerializer () {
51
+ this (OBJECT_MAPPER );
52
+ }
53
+
54
+ protected ObjectSerializer (final ObjectMapper objectMapper ) {
55
+ this .objectMapper = Objects .requireNonNullElse (objectMapper , OBJECT_MAPPER );
56
+ }
57
+
58
+ public static ObjectSerializer withObjectMapper (final ObjectMapper objectMapper ) {
59
+ return new ObjectSerializer (objectMapper );
44
60
}
45
61
46
62
/**
47
- * Serializes a given state object into byte array.
63
+ * Serializes a given state object into a byte array.
48
64
*
49
65
* @param state State object to be serialized.
50
66
* @return Array of bytes[] with the serialized content.
@@ -70,7 +86,7 @@ public byte[] serialize(Object state) throws IOException {
70
86
}
71
87
72
88
// Not string, not primitive, so it is a complex type: we use JSON for that.
73
- return OBJECT_MAPPER .writeValueAsBytes (state );
89
+ return objectMapper .writeValueAsBytes (state );
74
90
}
75
91
76
92
/**
@@ -83,7 +99,7 @@ public byte[] serialize(Object state) throws IOException {
83
99
* @throws IOException In case content cannot be deserialized.
84
100
*/
85
101
public <T > T deserialize (byte [] content , TypeRef <T > type ) throws IOException {
86
- return deserialize (content , OBJECT_MAPPER .constructType (type .getType ()));
102
+ return deserialize (content , objectMapper .constructType (type .getType ()));
87
103
}
88
104
89
105
/**
@@ -96,7 +112,7 @@ public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
96
112
* @throws IOException In case content cannot be deserialized.
97
113
*/
98
114
public <T > T deserialize (byte [] content , Class <T > clazz ) throws IOException {
99
- return deserialize (content , OBJECT_MAPPER .constructType (clazz ));
115
+ return deserialize (content , objectMapper .constructType (clazz ));
100
116
}
101
117
102
118
private <T > T deserialize (byte [] content , JavaType javaType ) throws IOException {
@@ -138,7 +154,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
138
154
}
139
155
}
140
156
141
- return OBJECT_MAPPER .readValue (content , javaType );
157
+ return objectMapper .readValue (content , javaType );
142
158
}
143
159
144
160
/**
@@ -149,7 +165,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
149
165
* @throws IOException In case content cannot be parsed.
150
166
*/
151
167
public JsonNode parseNode (byte [] content ) throws IOException {
152
- return OBJECT_MAPPER .readTree (content );
168
+ return OBJECT_MAPPER .readTree (content );
153
169
}
154
170
155
171
/**
@@ -161,7 +177,7 @@ public JsonNode parseNode(byte[] content) throws IOException {
161
177
* @return Result as corresponding type.
162
178
* @throws IOException if cannot deserialize primitive time.
163
179
*/
164
- private static <T > T deserializePrimitives (byte [] content , JavaType javaType ) throws IOException {
180
+ public <T > T deserializePrimitives (byte [] content , JavaType javaType ) throws IOException {
165
181
if ((content == null ) || (content .length == 0 )) {
166
182
if (javaType .hasRawClass (boolean .class )) {
167
183
return (T ) Boolean .FALSE ;
@@ -198,6 +214,6 @@ private static <T> T deserializePrimitives(byte[] content, JavaType javaType) th
198
214
return null ;
199
215
}
200
216
201
- return OBJECT_MAPPER .readValue (content , javaType );
217
+ return objectMapper .readValue (content , javaType );
202
218
}
203
219
}
0 commit comments