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 disabledNote that Jackson's JSR-310 and Joda-Time support modules will be registered automatically when available (and when Java 8 and Joda-Time themselves are available, respectively).
Tested against Jackson 2.2, 2.3 and 2.4; compatible with Jackson 2.0 and higher.
build()
,
configure(ObjectMapper)
,
Jackson2ObjectMapperFactoryBean
Modifier and Type | Field and Description |
---|---|
private AnnotationIntrospector |
annotationIntrospector |
private ApplicationContext |
applicationContext |
private boolean |
createXmlMapper |
private java.text.DateFormat |
dateFormat |
private java.util.Map<java.lang.Class<?>,<any>> |
deserializers |
private java.util.Map<java.lang.Object,java.lang.Boolean> |
features |
private boolean |
findModulesViaServiceLoader |
private HandlerInstantiator |
handlerInstantiator |
private java.util.Map<java.lang.Class<?>,java.lang.Class<?>> |
mixIns |
private java.lang.ClassLoader |
moduleClassLoader |
private java.util.List<Module> |
modules |
private java.lang.Class<? extends Module>[] |
modulesToInstall |
private PropertyNamingStrategy |
propertyNamingStrategy |
private JsonInclude.Include |
serializationInclusion |
private java.util.Map<java.lang.Class<?>,<any>> |
serializers |
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 option. |
<T extends ObjectMapper> |
build()
Build a new
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 |
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 |
deserializersByType(java.util.Map<java.lang.Class<?>,<any>> deserializers)
Configure custom deserializers for the given types.
|
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 |
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 |
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 |
modulesToInstall(java.lang.Class<? extends Module>... modules)
Specify one or more modules by class (or class name in XML),
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 |
xml()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build an XmlMapper instance. |
private boolean createXmlMapper
private java.text.DateFormat dateFormat
private AnnotationIntrospector annotationIntrospector
private PropertyNamingStrategy propertyNamingStrategy
private JsonInclude.Include serializationInclusion
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.Class<?>,java.lang.Class<?>> mixIns
private final java.util.Map<java.lang.Object,java.lang.Boolean> features
private java.util.List<Module> modules
private java.lang.Class<? extends Module>[] modulesToInstall
private boolean findModulesViaServiceLoader
private java.lang.ClassLoader moduleClassLoader
private HandlerInstantiator handlerInstantiator
private ApplicationContext applicationContext
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 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 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 serializationInclusion(JsonInclude.Include serializationInclusion)
com.fasterxml.jackson.annotation.JsonInclude.Include
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 deserializerByType(java.lang.Class<?> type, <any> deserializer)
public Jackson2ObjectMapperBuilder deserializersByType(java.util.Map<java.lang.Class<?>,<any>> deserializers)
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 autoDetectFields(boolean autoDetectFields)
MapperFeature#AUTO_DETECT_FIELDS
option.public Jackson2ObjectMapperBuilder autoDetectGettersSetters(boolean autoDetectGettersSetters)
MapperFeature#AUTO_DETECT_SETTERS
/
MapperFeature#AUTO_DETECT_GETTERS
option.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 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(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
, not both.
com.fasterxml.jackson.databind.Module
public Jackson2ObjectMapperBuilder modulesToInstall(java.lang.Class<? extends Module>... modules)
ObjectMapper
.
Modules specified here will be registered in combination with
Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
finding of modules (see findModulesViaServiceLoader
).
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.