1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .http .converter .json ;
18
18
19
+ import java .io .IOException ;
19
20
import java .io .UnsupportedEncodingException ;
20
21
import java .text .SimpleDateFormat ;
21
22
import java .util .ArrayList ;
22
- import java .util .Arrays ;
23
23
import java .util .Collections ;
24
24
import java .util .Date ;
25
25
import java .util .HashMap ;
41
41
import com .fasterxml .jackson .databind .ObjectMapper ;
42
42
import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
43
43
import com .fasterxml .jackson .databind .SerializationFeature ;
44
+ import com .fasterxml .jackson .databind .SerializerProvider ;
44
45
import com .fasterxml .jackson .databind .cfg .DeserializerFactoryConfig ;
45
46
import com .fasterxml .jackson .databind .cfg .SerializerFactoryConfig ;
46
47
import com .fasterxml .jackson .databind .deser .BasicDeserializerFactory ;
55
56
import com .fasterxml .jackson .databind .ser .std .NumberSerializer ;
56
57
import com .fasterxml .jackson .databind .type .SimpleType ;
57
58
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 ;
59
+ import static org .hamcrest .Matchers .containsString ;
60
60
import org .joda .time .DateTime ;
61
61
import org .joda .time .DateTimeZone ;
62
- import org .joda .time .format .DateTimeFormat ;
63
62
import org .junit .Test ;
64
63
65
64
import org .springframework .beans .FatalBeanException ;
@@ -212,15 +211,29 @@ public void wrongTimeZoneStringSetter() {
212
211
}
213
212
214
213
@ Test
215
- public void setModules () {
214
+ public void modules () {
216
215
NumberSerializer serializer1 = new NumberSerializer ();
217
216
SimpleModule module = new SimpleModule ();
218
217
module .addSerializer (Integer .class , serializer1 );
219
- ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().modules (Arrays . asList ( new Module []{ module }) ).build ();
218
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().modules (module ).build ();
220
219
Serializers serializers = getSerializerFactoryConfig (objectMapper ).serializers ().iterator ().next ();
221
220
assertTrue (serializers .findSerializer (null , SimpleType .construct (Integer .class ), null ) == serializer1 );
222
221
}
223
222
223
+ @ Test
224
+ public void modulesToInstallByClass () {
225
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().modulesToInstall (CustomIntegerModule .class ).build ();
226
+ Serializers serializers = getSerializerFactoryConfig (objectMapper ).serializers ().iterator ().next ();
227
+ assertTrue (serializers .findSerializer (null , SimpleType .construct (Integer .class ), null ).getClass () == CustomIntegerSerializer .class );
228
+ }
229
+
230
+ @ Test
231
+ public void modulesToInstallByInstance () {
232
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().modulesToInstall (new CustomIntegerModule ()).build ();
233
+ Serializers serializers = getSerializerFactoryConfig (objectMapper ).serializers ().iterator ().next ();
234
+ assertTrue (serializers .findSerializer (null , SimpleType .construct (Integer .class ), null ).getClass () == CustomIntegerSerializer .class );
235
+ }
236
+
224
237
@ Test
225
238
public void defaultModules () throws JsonProcessingException , UnsupportedEncodingException {
226
239
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().build ();
@@ -230,21 +243,29 @@ public void defaultModules() throws JsonProcessingException, UnsupportedEncoding
230
243
}
231
244
232
245
@ Test // SPR-12634
233
- public void customizeDefaultModules () throws JsonProcessingException , UnsupportedEncodingException {
246
+ public void customizeDefaultModulesWithModule () throws JsonProcessingException , UnsupportedEncodingException {
234
247
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
235
- .featuresToDisable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS )
236
- .modulesToInstall (CustomModule .class ).build ();
248
+ .modulesToInstall (new CustomIntegerModule ()).build ();
237
249
DateTime dateTime = new DateTime (1322903730000L , DateTimeZone .UTC );
238
- assertEquals ("\" 2011-12-03\" " , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
250
+ assertEquals ("1322903730000" , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
251
+ assertThat (new String (objectMapper .writeValueAsBytes (new Integer (4 )), "UTF-8" ), containsString ("customid" ));
252
+ }
253
+
254
+ @ Test // SPR-12634
255
+ public void customizeDefaultModulesWithModuleClass () throws JsonProcessingException , UnsupportedEncodingException {
256
+ ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ().modulesToInstall (CustomIntegerModule .class ).build ();
257
+ DateTime dateTime = new DateTime (1322903730000L , DateTimeZone .UTC );
258
+ assertEquals ("1322903730000" , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
259
+ assertThat (new String (objectMapper .writeValueAsBytes (new Integer (4 )), "UTF-8" ), containsString ("customid" ));
239
260
}
240
261
241
262
@ Test // SPR-12634
242
263
public void customizeDefaultModulesWithSerializer () throws JsonProcessingException , UnsupportedEncodingException {
243
264
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder .json ()
244
- .serializerByType (DateTime .class , new DateTimeSerializer (new JacksonJodaDateFormat (DateTimeFormat .forPattern ("YYYY-MM-dd" ).withZoneUTC ())))
245
- .featuresToDisable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS ).build ();
265
+ .serializerByType (Integer .class , new CustomIntegerSerializer ()).build ();
246
266
DateTime dateTime = new DateTime (1322903730000L , DateTimeZone .UTC );
247
- assertEquals ("\" 2011-12-03\" " , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
267
+ assertEquals ("1322903730000" , new String (objectMapper .writeValueAsBytes (dateTime ), "UTF-8" ));
268
+ assertThat (new String (objectMapper .writeValueAsBytes (new Integer (4 )), "UTF-8" ), containsString ("customid" ));
248
269
}
249
270
250
271
@@ -387,7 +408,7 @@ public void createXmlMapper() {
387
408
}
388
409
389
410
390
- public static class CustomModule extends Module {
411
+ public static class CustomIntegerModule extends Module {
391
412
392
413
@ Override
393
414
public String getModuleName () {
@@ -402,9 +423,19 @@ public Version version() {
402
423
@ Override
403
424
public void setupModule (SetupContext context ) {
404
425
SimpleSerializers serializers = new SimpleSerializers ();
405
- serializers .addSerializer (DateTime .class , new DateTimeSerializer ( new JacksonJodaDateFormat ( DateTimeFormat . forPattern ( "YYYY-MM-dd" ). withZoneUTC ()) ));
426
+ serializers .addSerializer (Integer .class , new CustomIntegerSerializer ( ));
406
427
context .addSerializers (serializers );
407
428
}
408
429
}
409
430
431
+ public static class CustomIntegerSerializer extends JsonSerializer <Integer > {
432
+
433
+ @ Override
434
+ public void serialize (Integer value , JsonGenerator gen , SerializerProvider serializers ) throws IOException , JsonProcessingException {
435
+ gen .writeStartObject ();
436
+ gen .writeNumberField ("customid" , value );
437
+ gen .writeEndObject ();
438
+ }
439
+ }
440
+
410
441
}
0 commit comments