16
16
17
17
package org .springframework .http .converter .json ;
18
18
19
+ import java .io .UnsupportedEncodingException ;
19
20
import java .text .SimpleDateFormat ;
21
+ import java .util .ArrayList ;
20
22
import java .util .Arrays ;
21
23
import java .util .Collections ;
22
24
import java .util .Date ;
28
30
import com .fasterxml .jackson .annotation .JsonInclude ;
29
31
import com .fasterxml .jackson .core .JsonGenerator ;
30
32
import com .fasterxml .jackson .core .JsonParser ;
33
+ import com .fasterxml .jackson .core .JsonProcessingException ;
34
+ import com .fasterxml .jackson .core .Version ;
31
35
import com .fasterxml .jackson .databind .DeserializationFeature ;
32
36
import com .fasterxml .jackson .databind .JsonDeserializer ;
33
37
import com .fasterxml .jackson .databind .JsonMappingException ;
44
48
import com .fasterxml .jackson .databind .deser .std .DateDeserializers ;
45
49
import com .fasterxml .jackson .databind .introspect .NopAnnotationIntrospector ;
46
50
import com .fasterxml .jackson .databind .module .SimpleModule ;
51
+ import com .fasterxml .jackson .databind .module .SimpleSerializers ;
47
52
import com .fasterxml .jackson .databind .ser .BasicSerializerFactory ;
48
53
import com .fasterxml .jackson .databind .ser .Serializers ;
49
54
import com .fasterxml .jackson .databind .ser .std .ClassSerializer ;
50
55
import com .fasterxml .jackson .databind .ser .std .NumberSerializer ;
51
56
import com .fasterxml .jackson .databind .type .SimpleType ;
52
57
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
58
+ import com .fasterxml .jackson .datatype .joda .cfg .JacksonJodaDateFormat ;
59
+ import com .fasterxml .jackson .datatype .joda .ser .DateTimeSerializer ;
60
+ import org .joda .time .DateTime ;
61
+ import org .joda .time .format .DateTimeFormat ;
53
62
import org .junit .Test ;
54
63
55
64
import org .springframework .beans .FatalBeanException ;
@@ -211,6 +220,32 @@ public void setModules() {
211
220
assertTrue (serializers .findSerializer (null , SimpleType .construct (Integer .class ), null ) == serializer1 );
212
221
}
213
222
223
+ @ Test
224
+ public void defaultModules () throws JsonProcessingException , UnsupportedEncodingException {
225
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().build ();
226
+ DateTime dateTime = DateTime .parse ("2011-12-03T10:15:30" );
227
+ assertEquals ("1322903730000" , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
228
+ }
229
+
230
+ @ Test // SPR-12634
231
+ public void customizeDefaultModules () throws JsonProcessingException , UnsupportedEncodingException {
232
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
233
+ .featuresToDisable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS )
234
+ .modulesToInstall (CustomModule .class ).build ();
235
+ DateTime dateTime = DateTime .parse ("2011-12-03T10:15:30" );
236
+ assertEquals ("\" 2011-12-03\" " , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
237
+ }
238
+
239
+ @ Test // SPR-12634
240
+ public void customizeDefaultModulesWithSerializer () throws JsonProcessingException , UnsupportedEncodingException {
241
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
242
+ .serializerByType (DateTime .class , new DateTimeSerializer (new JacksonJodaDateFormat (DateTimeFormat .forPattern ("YYYY-MM-dd" ).withZoneUTC ())))
243
+ .featuresToDisable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS ).build ();
244
+ DateTime dateTime = DateTime .parse ("2011-12-03T10:15:30" );
245
+ assertEquals ("\" 2011-12-03\" " , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
246
+ }
247
+
248
+
214
249
private static SerializerFactoryConfig getSerializerFactoryConfig (ObjectMapper objectMapper ) {
215
250
return ((BasicSerializerFactory ) objectMapper .getSerializerFactory ()).getFactoryConfig ();
216
251
}
@@ -231,6 +266,7 @@ public void propertyNamingStrategy() {
231
266
public void serializerByType () {
232
267
JsonSerializer <Number > serializer = new NumberSerializer ();
233
268
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
269
+ .modules (new ArrayList <>()) // Disable well-known modules detection
234
270
.serializerByType (Boolean .class , serializer ).build ();
235
271
assertTrue (getSerializerFactoryConfig (objectMapper ).hasSerializers ());
236
272
Serializers serializers = getSerializerFactoryConfig (objectMapper ).serializers ().iterator ().next ();
@@ -241,6 +277,7 @@ public void serializerByType() {
241
277
public void deserializerByType () throws JsonMappingException {
242
278
JsonDeserializer <Date > deserializer = new DateDeserializers .DateDeserializer ();
243
279
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
280
+ .modules (new ArrayList <>()) // Disable well-known modules detection
244
281
.deserializerByType (Date .class , deserializer ).build ();
245
282
assertTrue (getDeserializerFactoryConfig (objectMapper ).hasDeserializers ());
246
283
Deserializers deserializers = getDeserializerFactoryConfig (objectMapper ).deserializers ().iterator ().next ();
@@ -284,6 +321,7 @@ public void completeSetup() throws JsonMappingException {
284
321
JsonSerializer <Number > serializer2 = new NumberSerializer ();
285
322
286
323
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder .json ()
324
+ .modules (new ArrayList <>()) // Disable well-known modules detection
287
325
.serializers (serializer1 )
288
326
.serializersByType (Collections .<Class <?>, JsonSerializer <?>>singletonMap (Boolean .class , serializer2 ))
289
327
.deserializersByType (deserializerMap )
@@ -346,4 +384,25 @@ public void createXmlMapper() {
346
384
assertTrue (xmlObjectMapper .getClass ().isAssignableFrom (XmlMapper .class ));
347
385
}
348
386
387
+
388
+ public static class CustomModule extends Module {
389
+
390
+ @ Override
391
+ public String getModuleName () {
392
+ return this .getClass ().getSimpleName ();
393
+ }
394
+
395
+ @ Override
396
+ public Version version () {
397
+ return Version .unknownVersion ();
398
+ }
399
+
400
+ @ Override
401
+ public void setupModule (SetupContext context ) {
402
+ SimpleSerializers serializers = new SimpleSerializers ();
403
+ serializers .addSerializer (DateTime .class , new DateTimeSerializer (new JacksonJodaDateFormat (DateTimeFormat .forPattern ("YYYY-MM-dd" ).withZoneUTC ())));
404
+ context .addSerializers (serializers );
405
+ }
406
+ }
407
+
349
408
}
0 commit comments