public class Jackson2ObjectMapperBuilder extends 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:
Path
Optional
Tested against Jackson 2.2, 2.3, 2.4, 2.5, 2.6; compatible with Jackson 2.0 and higher.
build()
,
configure(ObjectMapper)
,
Jackson2ObjectMapperFactoryBean
Constructor and Description |
---|
Jackson2ObjectMapperBuilder() |
Modifier and Type | Method and Description |
---|---|
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. |
Jackson2ObjectMapperBuilder |
createXmlMapper(boolean createXmlMapper)
If set to
true , an XmlMapper will be created using its
default constructor. |
Jackson2ObjectMapperBuilder |
dateFormat(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(Class<?> type,
JsonDeserializer<?> deserializer)
Configure a custom deserializer for the given type.
|
Jackson2ObjectMapperBuilder |
deserializersByType(Map<Class<?>,JsonDeserializer<?>> 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(Object... featuresToDisable)
Specify features to disable.
|
Jackson2ObjectMapperBuilder |
featuresToEnable(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(Locale locale)
Override the default
Locale to use for formatting. |
Jackson2ObjectMapperBuilder |
locale(String localeString)
Override the default
Locale to use for formatting. |
Jackson2ObjectMapperBuilder |
mixIn(Class<?> target,
Class<?> mixinSource)
Add mix-in annotations to use for augmenting specified class or interface.
|
Jackson2ObjectMapperBuilder |
mixIns(Map<Class<?>,Class<?>> mixIns)
Add mix-in annotations to use for augmenting specified class or interface.
|
Jackson2ObjectMapperBuilder |
moduleClassLoader(ClassLoader moduleClassLoader)
Set the ClassLoader to use for loading Jackson extension modules.
|
Jackson2ObjectMapperBuilder |
modules(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(Class<? extends Module>... modules)
Specify one or more modules by class 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
PropertyNamingStrategy to
configure the ObjectMapper with. |
Jackson2ObjectMapperBuilder |
serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion)
Set a custom inclusion strategy for serialization.
|
Jackson2ObjectMapperBuilder |
serializerByType(Class<?> type,
JsonSerializer<?> serializer)
Configure a custom serializer for the given type.
|
Jackson2ObjectMapperBuilder |
serializers(JsonSerializer<?>... serializers)
Configure custom serializers.
|
Jackson2ObjectMapperBuilder |
serializersByType(Map<Class<?>,JsonSerializer<?>> serializers)
Configure custom serializers for the given types.
|
Jackson2ObjectMapperBuilder |
simpleDateFormat(String format)
Define the date/time format with a
SimpleDateFormat . |
Jackson2ObjectMapperBuilder |
timeZone(String timeZoneString)
Override the default
TimeZone to use for formatting. |
Jackson2ObjectMapperBuilder |
timeZone(TimeZone timeZone)
Override the default
TimeZone to use for formatting. |
static Jackson2ObjectMapperBuilder |
xml()
Obtain a
Jackson2ObjectMapperBuilder instance in order to
build an XmlMapper instance. |
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(com.fasterxml.jackson.databind.ObjectMapper)
calls.public Jackson2ObjectMapperBuilder dateFormat(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(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(Locale locale)
Locale
to use for formatting.
Default value used is Locale.getDefault()
.public Jackson2ObjectMapperBuilder locale(String localeString)
Locale
to use for formatting.
Default value used is Locale.getDefault()
.localeString
- the locale ID as a String representationpublic Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone)
TimeZone
to use for formatting.
Default value used is UTC (NOT local timezone).public Jackson2ObjectMapperBuilder timeZone(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)
PropertyNamingStrategy
to
configure the ObjectMapper
with.public Jackson2ObjectMapperBuilder serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion)
JsonInclude.Include
public Jackson2ObjectMapperBuilder filters(FilterProvider filters)
@JsonFilter
annotated POJO.MappingJacksonValue.setFilters(FilterProvider)
public Jackson2ObjectMapperBuilder mixIn(Class<?> target, 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 valueObjectMapper.addMixInAnnotations(Class, Class)
public Jackson2ObjectMapperBuilder mixIns(Map<Class<?>,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.ObjectMapper.addMixInAnnotations(Class, Class)
public Jackson2ObjectMapperBuilder serializers(JsonSerializer<?>... serializers)
JsonSerializer.handledType()
, which must not be
null
.serializersByType(Map)
public Jackson2ObjectMapperBuilder serializerByType(Class<?> type, JsonSerializer<?> serializer)
serializers(JsonSerializer...)
public Jackson2ObjectMapperBuilder serializersByType(Map<Class<?>,JsonSerializer<?>> serializers)
serializers(JsonSerializer...)
public Jackson2ObjectMapperBuilder deserializerByType(Class<?> type, JsonDeserializer<?> deserializer)
public Jackson2ObjectMapperBuilder deserializersByType(Map<Class<?>,JsonDeserializer<?>> deserializers)
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(Object... featuresToEnable)
public Jackson2ObjectMapperBuilder featuresToDisable(Object... featuresToDisable)
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(com.fasterxml.jackson.databind.Module...)
, not both.
modules(List)
,
Module
public Jackson2ObjectMapperBuilder modules(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(com.fasterxml.jackson.databind.Module...)
, not both.
modules(Module...)
,
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.
Module
public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends 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.
modulesToInstall(Module...)
,
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.
ObjectMapper.findModules()
public Jackson2ObjectMapperBuilder moduleClassLoader(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 configurepublic 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.