public class Jackson2ObjectMapperBuilder
extends java.lang.Object
ObjectMapper
instances with a fluent API.
It customizes Jackson's default properties with the following ones:
MapperFeature#DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES
is disabledIt also automatically registers the following well-known modules if they are detected on the classpath:
Optional
Compatible with Jackson 2.6 and higher, as of Spring 4.3.
build()
,
configure(ObjectMapper)
,
Jackson2ObjectMapperFactoryBean
Modifier and Type | Class and Description |
---|---|
private static class |
Jackson2ObjectMapperBuilder.CborFactoryInitializer |
private static class |
Jackson2ObjectMapperBuilder.SmileFactoryInitializer |
private static class |
Jackson2ObjectMapperBuilder.XmlObjectMapperInitializer |
Modifier and Type | Field and Description |
---|---|
private AnnotationIntrospector |
annotationIntrospector |
private ApplicationContext |
applicationContext |
private boolean |
createXmlMapper |
private java.text.DateFormat |
dateFormat |
private <any> |
defaultTyping |
private java.lang.Boolean |
defaultUseWrapper |
private java.util.Map<java.lang.Class<?>,<any>> |
deserializers |
private JsonFactory |
factory |
private java.util.Map<java.lang.Object,java.lang.Boolean> |
features |
private FilterProvider |
filters |
private boolean |
findModulesViaServiceLoader |
private boolean |
findWellKnownModules |
private HandlerInstantiator |
handlerInstantiator |
private java.util.Locale |
locale |
private Log |
logger |
private java.util.Map<java.lang.Class<?>,java.lang.Class<?>> |
mixIns |
private java.lang.Class<? extends Module>[] |
moduleClasses |
private java.lang.ClassLoader |
moduleClassLoader |
private java.util.List<Module> |
modules |
private PropertyNamingStrategy |
propertyNamingStrategy |
private JsonInclude.Include |
serializationInclusion |
private java.util.Map<java.lang.Class<?>,<any>> |
serializers |
private java.util.TimeZone |
timeZone |
Constructor and Description |
---|
Jackson2ObjectMapperBuilder() |
Modifier and Type | Method and Description |
---|---|
private <T> void |
addDeserializers(SimpleModule module) |
private <T> void |
addSerializers(SimpleModule module) |
Jackson2ObjectMapperBuilder |
annotationIntrospector(AnnotationIntrospector annotationIntrospector)
Set an
AnnotationIntrospector for both serialization and deserialization. |
Jackson2ObjectMapperBuilder |
applicationContext(ApplicationContext applicationContext)
Set the Spring
ApplicationContext in order to autowire Jackson handlers (JsonSerializer ,
JsonDeserializer , KeyDeserializer , TypeResolverBuilder and TypeIdResolver ). |
Jackson2ObjectMapperBuilder |
autoDetectFields(boolean autoDetectFields)
Shortcut for
MapperFeature#AUTO_DETECT_FIELDS option. |
Jackson2ObjectMapperBuilder |
autoDetectGettersSetters(boolean autoDetectGettersSetters)
Shortcut for
MapperFeature#AUTO_DETECT_SETTERS /
MapperFeature#AUTO_DETECT_GETTERS /MapperFeature#AUTO_DETECT_IS_GETTERS
options. |
<T extends ObjectMapper> |
build()
Build a new
ObjectMapper instance. |
static Jackson2ObjectMapperBuilder |
cbor()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build a CBOR data format ObjectMapper instance. |
void |
configure(ObjectMapper objectMapper)
Configure an existing
ObjectMapper instance with this builder's
settings. |
private void |
configureFeature(ObjectMapper objectMapper,
java.lang.Object feature,
boolean enabled) |
Jackson2ObjectMapperBuilder |
createXmlMapper(boolean createXmlMapper)
If set to
true , an XmlMapper will be created using its
default constructor. |
private void |
customizeDefaultFeatures(ObjectMapper objectMapper) |
Jackson2ObjectMapperBuilder |
dateFormat(java.text.DateFormat dateFormat)
Define the format for date/time with the given
DateFormat . |
Jackson2ObjectMapperBuilder |
defaultTyping(<any> typeResolverBuilder)
Specify a
TypeResolverBuilder to use for Jackson's default typing. |
Jackson2ObjectMapperBuilder |
defaultUseWrapper(boolean defaultUseWrapper)
Define if a wrapper will be used for indexed (List, array) properties or not by
default (only applies to
XmlMapper ). |
Jackson2ObjectMapperBuilder |
defaultViewInclusion(boolean defaultViewInclusion)
Shortcut for
MapperFeature#DEFAULT_VIEW_INCLUSION option. |
Jackson2ObjectMapperBuilder |
deserializerByType(java.lang.Class<?> type,
<any> deserializer)
Configure a custom deserializer for the given type.
|
Jackson2ObjectMapperBuilder |
deserializers(<any>... deserializers)
Configure custom deserializers.
|
Jackson2ObjectMapperBuilder |
deserializersByType(java.util.Map<java.lang.Class<?>,<any>> deserializers)
Configure custom deserializers for the given types.
|
Jackson2ObjectMapperBuilder |
factory(JsonFactory factory)
Define the
JsonFactory to be used to create the ObjectMapper
instance. |
Jackson2ObjectMapperBuilder |
failOnEmptyBeans(boolean failOnEmptyBeans)
Shortcut for
SerializationFeature#FAIL_ON_EMPTY_BEANS option. |
Jackson2ObjectMapperBuilder |
failOnUnknownProperties(boolean failOnUnknownProperties)
Shortcut for
DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES option. |
Jackson2ObjectMapperBuilder |
featuresToDisable(java.lang.Object... featuresToDisable)
Specify features to disable.
|
Jackson2ObjectMapperBuilder |
featuresToEnable(java.lang.Object... featuresToEnable)
Specify features to enable.
|
Jackson2ObjectMapperBuilder |
filters(FilterProvider filters)
Set the global filters to use in order to support
@JsonFilter annotated POJO. |
Jackson2ObjectMapperBuilder |
findModulesViaServiceLoader(boolean findModules)
Set whether to let Jackson find available modules via the JDK ServiceLoader,
based on META-INF metadata in the classpath.
|
Jackson2ObjectMapperBuilder |
handlerInstantiator(HandlerInstantiator handlerInstantiator)
Customize the construction of Jackson handlers (
JsonSerializer , JsonDeserializer ,
KeyDeserializer , TypeResolverBuilder and TypeIdResolver ). |
Jackson2ObjectMapperBuilder |
indentOutput(boolean indentOutput)
Shortcut for
SerializationFeature#INDENT_OUTPUT option. |
static Jackson2ObjectMapperBuilder |
json()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build a regular JSON ObjectMapper instance. |
Jackson2ObjectMapperBuilder |
locale(java.util.Locale locale)
Override the default
Locale to use for formatting. |
Jackson2ObjectMapperBuilder |
locale(java.lang.String localeString)
Override the default
Locale to use for formatting. |
Jackson2ObjectMapperBuilder |
mixIn(java.lang.Class<?> target,
java.lang.Class<?> mixinSource)
Add mix-in annotations to use for augmenting specified class or interface.
|
Jackson2ObjectMapperBuilder |
mixIns(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> mixIns)
Add mix-in annotations to use for augmenting specified class or interface.
|
Jackson2ObjectMapperBuilder |
moduleClassLoader(java.lang.ClassLoader moduleClassLoader)
Set the ClassLoader to use for loading Jackson extension modules.
|
Jackson2ObjectMapperBuilder |
modules(java.util.List<Module> modules)
Set a complete list of modules to be registered with the
ObjectMapper . |
Jackson2ObjectMapperBuilder |
modules(Module... modules)
Specify one or more modules to be registered with the
ObjectMapper . |
Jackson2ObjectMapperBuilder |
modulesToInstall(Module... modules)
Specify one or more modules to be registered with the
ObjectMapper . |
Jackson2ObjectMapperBuilder |
propertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy)
Specify a
com.fasterxml.jackson.databind.PropertyNamingStrategy to
configure the ObjectMapper with. |
private void |
registerWellKnownModulesIfAvailable(ObjectMapper objectMapper) |
Jackson2ObjectMapperBuilder |
serializationInclusion(JsonInclude.Include serializationInclusion)
Set a custom inclusion strategy for serialization.
|
Jackson2ObjectMapperBuilder |
serializerByType(java.lang.Class<?> type,
<any> serializer)
Configure a custom serializer for the given type.
|
Jackson2ObjectMapperBuilder |
serializers(<any>... serializers)
Configure custom serializers.
|
Jackson2ObjectMapperBuilder |
serializersByType(java.util.Map<java.lang.Class<?>,<any>> serializers)
Configure custom serializers for the given types.
|
Jackson2ObjectMapperBuilder |
simpleDateFormat(java.lang.String format)
Define the date/time format with a
SimpleDateFormat . |
static Jackson2ObjectMapperBuilder |
smile()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build a Smile data format ObjectMapper instance. |
Jackson2ObjectMapperBuilder |
timeZone(java.lang.String timeZoneString)
Override the default
TimeZone to use for formatting. |
Jackson2ObjectMapperBuilder |
timeZone(java.util.TimeZone timeZone)
Override the default
TimeZone to use for formatting. |
static Jackson2ObjectMapperBuilder |
xml()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build an XmlMapper instance. |
private final Log logger
private final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> mixIns
private final java.util.Map<java.lang.Class<?>,<any>> serializers
private final java.util.Map<java.lang.Class<?>,<any>> deserializers
private final java.util.Map<java.lang.Object,java.lang.Boolean> features
private boolean createXmlMapper
@Nullable private JsonFactory factory
@Nullable private java.text.DateFormat dateFormat
@Nullable private java.util.Locale locale
@Nullable private java.util.TimeZone timeZone
@Nullable private AnnotationIntrospector annotationIntrospector
@Nullable private PropertyNamingStrategy propertyNamingStrategy
@Nullable private <any> defaultTyping
@Nullable private JsonInclude.Include serializationInclusion
@Nullable private FilterProvider filters
@Nullable private java.util.List<Module> modules
@Nullable private java.lang.Class<? extends Module>[] moduleClasses
private boolean findModulesViaServiceLoader
private boolean findWellKnownModules
private java.lang.ClassLoader moduleClassLoader
@Nullable private HandlerInstantiator handlerInstantiator
@Nullable private ApplicationContext applicationContext
@Nullable private java.lang.Boolean defaultUseWrapper
public Jackson2ObjectMapperBuilder createXmlMapper(boolean createXmlMapper)
true
, an XmlMapper
will be created using its
default constructor. This is only applicable to build()
calls,
not to configure(ObjectMapper)
calls.public Jackson2ObjectMapperBuilder factory(JsonFactory factory)
JsonFactory
to be used to create the ObjectMapper
instance.public Jackson2ObjectMapperBuilder dateFormat(java.text.DateFormat dateFormat)
DateFormat
.
Note: Setting this property makes the exposed ObjectMapper
non-thread-safe, according to Jackson's thread safety rules.
simpleDateFormat(String)
public Jackson2ObjectMapperBuilder simpleDateFormat(java.lang.String format)
SimpleDateFormat
.
Note: Setting this property makes the exposed ObjectMapper
non-thread-safe, according to Jackson's thread safety rules.
dateFormat(DateFormat)
public Jackson2ObjectMapperBuilder locale(java.util.Locale locale)
Locale
to use for formatting.
Default value used is Locale.getDefault()
.public Jackson2ObjectMapperBuilder locale(java.lang.String localeString)
Locale
to use for formatting.
Default value used is Locale.getDefault()
.localeString
- the locale ID as a String representationpublic Jackson2ObjectMapperBuilder timeZone(java.util.TimeZone timeZone)
TimeZone
to use for formatting.
Default value used is UTC (NOT local timezone).public Jackson2ObjectMapperBuilder timeZone(java.lang.String timeZoneString)
TimeZone
to use for formatting.
Default value used is UTC (NOT local timezone).timeZoneString
- the zone ID as a String representationpublic Jackson2ObjectMapperBuilder annotationIntrospector(AnnotationIntrospector annotationIntrospector)
AnnotationIntrospector
for both serialization and deserialization.public Jackson2ObjectMapperBuilder propertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy)
com.fasterxml.jackson.databind.PropertyNamingStrategy
to
configure the ObjectMapper
with.public Jackson2ObjectMapperBuilder defaultTyping(<any> typeResolverBuilder)
TypeResolverBuilder
to use for Jackson's default typing.public Jackson2ObjectMapperBuilder serializationInclusion(JsonInclude.Include serializationInclusion)
com.fasterxml.jackson.annotation.JsonInclude.Include
public Jackson2ObjectMapperBuilder filters(FilterProvider filters)
@JsonFilter
annotated POJO.MappingJacksonValue.setFilters(FilterProvider)
public Jackson2ObjectMapperBuilder mixIn(java.lang.Class<?> target, java.lang.Class<?> mixinSource)
target
- class (or interface) whose annotations to effectively overridemixinSource
- class (or interface) whose annotations are to be "added"
to target's annotations as valuecom.fasterxml.jackson.databind.ObjectMapper#addMixInAnnotations(Class, Class)
public Jackson2ObjectMapperBuilder mixIns(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> mixIns)
mixIns
- Map of entries with target classes (or interface) whose annotations
to effectively override as key and mix-in classes (or interface) whose
annotations are to be "added" to target's annotations as value.com.fasterxml.jackson.databind.ObjectMapper#addMixInAnnotations(Class, Class)
public Jackson2ObjectMapperBuilder serializers(<any>... serializers)
JsonSerializer#handledType()
, which must not be null
.serializersByType(Map)
public Jackson2ObjectMapperBuilder serializerByType(java.lang.Class<?> type, <any> serializer)
#serializers(JsonSerializer...)
public Jackson2ObjectMapperBuilder serializersByType(java.util.Map<java.lang.Class<?>,<any>> serializers)
#serializers(JsonSerializer...)
public Jackson2ObjectMapperBuilder deserializers(<any>... deserializers)
JsonDeserializer#handledType()
, which must not be null
.deserializersByType(Map)
public Jackson2ObjectMapperBuilder deserializerByType(java.lang.Class<?> type, <any> deserializer)
public Jackson2ObjectMapperBuilder deserializersByType(java.util.Map<java.lang.Class<?>,<any>> deserializers)
public Jackson2ObjectMapperBuilder autoDetectFields(boolean autoDetectFields)
MapperFeature#AUTO_DETECT_FIELDS
option.public Jackson2ObjectMapperBuilder autoDetectGettersSetters(boolean autoDetectGettersSetters)
MapperFeature#AUTO_DETECT_SETTERS
/
MapperFeature#AUTO_DETECT_GETTERS
/MapperFeature#AUTO_DETECT_IS_GETTERS
options.public Jackson2ObjectMapperBuilder defaultViewInclusion(boolean defaultViewInclusion)
MapperFeature#DEFAULT_VIEW_INCLUSION
option.public Jackson2ObjectMapperBuilder failOnUnknownProperties(boolean failOnUnknownProperties)
DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES
option.public Jackson2ObjectMapperBuilder failOnEmptyBeans(boolean failOnEmptyBeans)
SerializationFeature#FAIL_ON_EMPTY_BEANS
option.public Jackson2ObjectMapperBuilder indentOutput(boolean indentOutput)
SerializationFeature#INDENT_OUTPUT
option.public Jackson2ObjectMapperBuilder defaultUseWrapper(boolean defaultUseWrapper)
XmlMapper
).public Jackson2ObjectMapperBuilder featuresToEnable(java.lang.Object... featuresToEnable)
com.fasterxml.jackson.core.JsonParser.Feature
,
com.fasterxml.jackson.core.JsonGenerator.Feature
,
com.fasterxml.jackson.databind.SerializationFeature
,
com.fasterxml.jackson.databind.DeserializationFeature
,
com.fasterxml.jackson.databind.MapperFeature
public Jackson2ObjectMapperBuilder featuresToDisable(java.lang.Object... featuresToDisable)
com.fasterxml.jackson.core.JsonParser.Feature
,
com.fasterxml.jackson.core.JsonGenerator.Feature
,
com.fasterxml.jackson.databind.SerializationFeature
,
com.fasterxml.jackson.databind.DeserializationFeature
,
com.fasterxml.jackson.databind.MapperFeature
public Jackson2ObjectMapperBuilder modules(Module... modules)
ObjectMapper
.
Note: If this is set, no finding of modules is going to happen - not by
Jackson, and not by Spring either (see findModulesViaServiceLoader
).
As a consequence, specifying an empty list here will suppress any kind of
module detection.
Specify either this or modulesToInstall(Module...)
, not both.
modules(List)
,
com.fasterxml.jackson.databind.Module
public Jackson2ObjectMapperBuilder modules(java.util.List<Module> modules)
ObjectMapper
.
Note: If this is set, no finding of modules is going to happen - not by
Jackson, and not by Spring either (see findModulesViaServiceLoader
).
As a consequence, specifying an empty list here will suppress any kind of
module detection.
Specify either this or modulesToInstall(Module...)
, not both.
modules(Module...)
,
com.fasterxml.jackson.databind.Module
public Jackson2ObjectMapperBuilder modulesToInstall(Module... modules)
ObjectMapper
.
Modules specified here will be registered after
Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
finding of modules (see findModulesViaServiceLoader
),
allowing to eventually override their configuration.
Specify either this or modules
, not both.
com.fasterxml.jackson.databind.Module
public Jackson2ObjectMapperBuilder findModulesViaServiceLoader(boolean findModules)
If this mode is not set, Spring's Jackson2ObjectMapperBuilder itself will try to find the JSR-310 and Joda-Time support modules on the classpath - provided that Java 8 and Joda-Time themselves are available, respectively.
com.fasterxml.jackson.databind.ObjectMapper#findModules()
public Jackson2ObjectMapperBuilder moduleClassLoader(java.lang.ClassLoader moduleClassLoader)
public Jackson2ObjectMapperBuilder handlerInstantiator(HandlerInstantiator handlerInstantiator)
JsonSerializer
, JsonDeserializer
,
KeyDeserializer
, TypeResolverBuilder
and TypeIdResolver
).applicationContext(ApplicationContext)
public Jackson2ObjectMapperBuilder applicationContext(ApplicationContext applicationContext)
ApplicationContext
in order to autowire Jackson handlers (JsonSerializer
,
JsonDeserializer
, KeyDeserializer
, TypeResolverBuilder
and TypeIdResolver
).SpringHandlerInstantiator
public <T extends ObjectMapper> T build()
ObjectMapper
instance.
Each build operation produces an independent ObjectMapper
instance.
The builder's settings can get modified, with a subsequent build operation
then producing a new ObjectMapper
based on the most recent settings.
public void configure(ObjectMapper objectMapper)
ObjectMapper
instance with this builder's
settings. This can be applied to any number of ObjectMappers
.objectMapper
- the ObjectMapper to configureprivate void customizeDefaultFeatures(ObjectMapper objectMapper)
private <T> void addSerializers(SimpleModule module)
private <T> void addDeserializers(SimpleModule module)
private void configureFeature(ObjectMapper objectMapper, java.lang.Object feature, boolean enabled)
private void registerWellKnownModulesIfAvailable(ObjectMapper objectMapper)
public static Jackson2ObjectMapperBuilder json()
Jackson2ObjectMapperBuilder
instance in order to
build a regular JSON ObjectMapper
instance.public static Jackson2ObjectMapperBuilder xml()
Jackson2ObjectMapperBuilder
instance in order to
build an XmlMapper
instance.public static Jackson2ObjectMapperBuilder smile()
Jackson2ObjectMapperBuilder
instance in order to
build a Smile data format ObjectMapper
instance.public static Jackson2ObjectMapperBuilder cbor()
Jackson2ObjectMapperBuilder
instance in order to
build a CBOR data format ObjectMapper
instance.