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.
18
18
19
19
import java .text .DateFormat ;
20
20
import java .text .SimpleDateFormat ;
21
+ import java .util .Arrays ;
21
22
import java .util .HashMap ;
22
23
import java .util .LinkedHashMap ;
23
24
import java .util .LinkedList ;
24
25
import java .util .List ;
26
+ import java .util .Locale ;
25
27
import java .util .Map ;
28
+ import java .util .TimeZone ;
26
29
27
30
import com .fasterxml .jackson .annotation .JsonInclude ;
28
31
import com .fasterxml .jackson .core .JsonGenerator ;
46
49
import org .springframework .context .ApplicationContext ;
47
50
import org .springframework .util .Assert ;
48
51
import org .springframework .util .ClassUtils ;
52
+ import org .springframework .util .StringUtils ;
49
53
50
54
/**
51
55
* A builder used to create {@link ObjectMapper} instances with a fluent API.
@@ -75,6 +79,10 @@ public class Jackson2ObjectMapperBuilder {
75
79
76
80
private DateFormat dateFormat ;
77
81
82
+ private Locale locale ;
83
+
84
+ private TimeZone timeZone ;
85
+
78
86
private AnnotationIntrospector annotationIntrospector ;
79
87
80
88
private PropertyNamingStrategy propertyNamingStrategy ;
@@ -91,9 +99,11 @@ public class Jackson2ObjectMapperBuilder {
91
99
92
100
private List <Module > modules ;
93
101
94
- private Class <? extends Module >[] modulesToInstall ;
102
+ private Class <? extends Module >[] moduleClasses ;
103
+
104
+ private boolean findModulesViaServiceLoader = false ;
95
105
96
- private boolean findModulesViaServiceLoader ;
106
+ private boolean findWellKnownModules = true ;
97
107
98
108
private ClassLoader moduleClassLoader = getClass ().getClassLoader ();
99
109
@@ -134,6 +144,48 @@ public Jackson2ObjectMapperBuilder simpleDateFormat(String format) {
134
144
return this ;
135
145
}
136
146
147
+ /**
148
+ * Override the default {@link Locale} to use for formatting.
149
+ * Default value used is {@link Locale#getDefault()}.
150
+ * @since 4.1.5
151
+ */
152
+ public Jackson2ObjectMapperBuilder locale (Locale locale ) {
153
+ this .locale = locale ;
154
+ return this ;
155
+ }
156
+
157
+ /**
158
+ * Override the default {@link Locale} to use for formatting.
159
+ * Default value used is {@link Locale#getDefault()}.
160
+ * @param localeString the locale ID as a String representation
161
+ * @since 4.1.5
162
+ */
163
+ public Jackson2ObjectMapperBuilder locale (String localeString ) {
164
+ this .locale = StringUtils .parseLocaleString (localeString );
165
+ return this ;
166
+ }
167
+
168
+ /**
169
+ * Override the default {@link TimeZone} to use for formatting.
170
+ * Default value used is UTC (NOT local timezone).
171
+ * @since 4.1.5
172
+ */
173
+ public Jackson2ObjectMapperBuilder timeZone (TimeZone timeZone ) {
174
+ this .timeZone = timeZone ;
175
+ return this ;
176
+ }
177
+
178
+ /**
179
+ * Override the default {@link TimeZone} to use for formatting.
180
+ * Default value used is UTC (NOT local timezone).
181
+ * @param timeZoneString the zone ID as a String representation
182
+ * @since 4.1.5
183
+ */
184
+ public Jackson2ObjectMapperBuilder timeZone (String timeZoneString ) {
185
+ this .timeZone = StringUtils .parseTimeZoneString (timeZoneString );
186
+ return this ;
187
+ }
188
+
137
189
/**
138
190
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
139
191
*/
@@ -337,31 +389,68 @@ public Jackson2ObjectMapperBuilder featuresToDisable(Object... featuresToDisable
337
389
return this ;
338
390
}
339
391
392
+ /**
393
+ * Specify one or more modules to be registered with the {@link ObjectMapper}.
394
+ * <p>Note: If this is set, no finding of modules is going to happen - not by
395
+ * Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}).
396
+ * As a consequence, specifying an empty list here will suppress any kind of
397
+ * module detection.
398
+ * <p>Specify either this or {@link #modulesToInstall}, not both.
399
+ * @since 4.1.5
400
+ * @see #modules(List)
401
+ * @see com.fasterxml.jackson.databind.Module
402
+ */
403
+ public Jackson2ObjectMapperBuilder modules (Module ... modules ) {
404
+ return modules (Arrays .asList (modules ));
405
+ }
406
+
340
407
/**
341
408
* Set a complete list of modules to be registered with the {@link ObjectMapper}.
342
409
* <p>Note: If this is set, no finding of modules is going to happen - not by
343
410
* Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}).
344
411
* As a consequence, specifying an empty list here will suppress any kind of
345
412
* module detection.
346
413
* <p>Specify either this or {@link #modulesToInstall}, not both.
414
+ * @see #modules(Module...)
347
415
* @see com.fasterxml.jackson.databind.Module
348
416
*/
349
417
public Jackson2ObjectMapperBuilder modules (List <Module > modules ) {
350
418
this .modules = new LinkedList <Module >(modules );
419
+ this .findModulesViaServiceLoader = false ;
420
+ this .findWellKnownModules = false ;
421
+ return this ;
422
+ }
423
+
424
+ /**
425
+ * Specify one or more modules to be registered with the {@link ObjectMapper}.
426
+ * <p>Modules specified here will be registered after
427
+ * Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
428
+ * finding of modules (see {@link #findModulesViaServiceLoader}),
429
+ * allowing to eventually override their configuration.
430
+ * <p>Specify either this or {@link #modules}, not both.
431
+ * @since 4.1.5
432
+ * @see com.fasterxml.jackson.databind.Module
433
+ */
434
+ public Jackson2ObjectMapperBuilder modulesToInstall (Module ... modules ) {
435
+ this .modules = Arrays .asList (modules );
436
+ this .findWellKnownModules = true ;
351
437
return this ;
352
438
}
353
439
354
440
/**
355
- * Specify one or more modules by class (or class name in XML),
356
- * to be registered with the {@link ObjectMapper}.
357
- * <p>Modules specified here will be registered in combination with
441
+ * Specify one or more modules by class to be registered with
442
+ * the {@link ObjectMapper}.
443
+ * <p>Modules specified here will be registered after
358
444
* Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
359
- * finding of modules (see {@link #findModulesViaServiceLoader}).
445
+ * finding of modules (see {@link #findModulesViaServiceLoader}),
446
+ * allowing to eventually override their configuration.
360
447
* <p>Specify either this or {@link #modules}, not both.
448
+ * @see #modulesToInstall(Module...)
361
449
* @see com.fasterxml.jackson.databind.Module
362
450
*/
363
451
public Jackson2ObjectMapperBuilder modulesToInstall (Class <? extends Module >... modules ) {
364
- this .modulesToInstall = modules ;
452
+ this .moduleClasses = modules ;
453
+ this .findWellKnownModules = true ;
365
454
return this ;
366
455
}
367
456
@@ -444,9 +533,35 @@ public <T extends ObjectMapper> T build() {
444
533
public void configure (ObjectMapper objectMapper ) {
445
534
Assert .notNull (objectMapper , "ObjectMapper must not be null" );
446
535
536
+ if (this .findModulesViaServiceLoader ) {
537
+ // Jackson 2.2+
538
+ objectMapper .registerModules (ObjectMapper .findModules (this .moduleClassLoader ));
539
+ }
540
+ else if (this .findWellKnownModules ) {
541
+ registerWellKnownModulesIfAvailable (objectMapper );
542
+ }
543
+
544
+ if (this .modules != null ) {
545
+ for (Module module : this .modules ) {
546
+ // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
547
+ objectMapper .registerModule (module );
548
+ }
549
+ }
550
+ if (this .moduleClasses != null ) {
551
+ for (Class <? extends Module > module : this .moduleClasses ) {
552
+ objectMapper .registerModule (BeanUtils .instantiate (module ));
553
+ }
554
+ }
555
+
447
556
if (this .dateFormat != null ) {
448
557
objectMapper .setDateFormat (this .dateFormat );
449
558
}
559
+ if (this .locale != null ) {
560
+ objectMapper .setLocale (this .locale );
561
+ }
562
+ if (this .timeZone != null ) {
563
+ objectMapper .setTimeZone (this .timeZone );
564
+ }
450
565
451
566
if (this .annotationIntrospector != null ) {
452
567
objectMapper .setAnnotationIntrospector (this .annotationIntrospector );
@@ -468,29 +583,6 @@ public void configure(ObjectMapper objectMapper) {
468
583
configureFeature (objectMapper , feature , this .features .get (feature ));
469
584
}
470
585
471
- if (this .modules != null ) {
472
- // Complete list of modules given
473
- for (Module module : this .modules ) {
474
- // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
475
- objectMapper .registerModule (module );
476
- }
477
- }
478
- else {
479
- // Combination of modules by class names specified and class presence in the classpath
480
- if (this .modulesToInstall != null ) {
481
- for (Class <? extends Module > module : this .modulesToInstall ) {
482
- objectMapper .registerModule (BeanUtils .instantiate (module ));
483
- }
484
- }
485
- if (this .findModulesViaServiceLoader ) {
486
- // Jackson 2.2+
487
- objectMapper .registerModules (ObjectMapper .findModules (this .moduleClassLoader ));
488
- }
489
- else {
490
- registerWellKnownModulesIfAvailable (objectMapper );
491
- }
492
- }
493
-
494
586
if (this .propertyNamingStrategy != null ) {
495
587
objectMapper .setPropertyNamingStrategy (this .propertyNamingStrategy );
496
588
}
0 commit comments