![]() |
难过的牛排 · 小米汽车SU7正式上市;前2月我国造船三大指 ...· 2 月前 · |
![]() |
勤奋的日光灯 · 人在斗罗,开局救了朱竹清_(骄傲的油炸糕)小 ...· 5 月前 · |
![]() |
刚毅的酱肘子 · 微软放弃动视暴雪云游戏,搬开最后一颗绊脚石_ ...· 8 月前 · |
![]() |
睿智的登山鞋 · 请问还有没有向《综漫之无尽邪欲》这样好看的综 ...· 10 月前 · |
![]() |
痴情的电池 · 中国共产党桃江历史(第一卷完整版)· 1 年前 · |
JsonNode
), as well as
related functionality for performing conversions.
It is also highly customizable to work both with different styles of JSON
content, and to support more advanced Object concepts such as
polymorphism and Object identity.
ObjectMapper
also acts as a factory for more advanced
ObjectReader
and
ObjectWriter
classes.
Mapper (and
ObjectReader
s,
ObjectWriter
s it constructs) will
use instances of
JsonParser
and
JsonGenerator
for implementing actual reading/writing of JSON.
Note that although most read and write methods are exposed through this class,
some of the functionality is only exposed via
ObjectReader
and
ObjectWriter
: specifically, reading/writing of longer sequences of
values is only available through
ObjectReader.readValues(InputStream)
and
ObjectWriter.writeValues(OutputStream)
.
Simplest usage is of form:
final ObjectMapper mapper = new ObjectMapper(); // can use static singleton, inject: just make sure to reuse!
MyValue value = new MyValue();
// ... and configure
File newState = new File("my-stuff.json");
mapper.writeValue(newState, value); // writes JSON serialization of MyValue instance
// or, read
MyValue older = mapper.readValue(new File("my-older-stuff.json"), MyValue.class);
// Or if you prefer JSON Tree representation:
JsonNode root = mapper.readTree(newState);
// and find values by, for example, using a
JsonPointer
expression:
int age = root.at("/personal/age").getValueAsInt();
The main conversion API is defined in
ObjectCodec
, so that
implementation details of this class need not be exposed to
streaming parser and generator classes. Usage via
ObjectCodec
is,
however, usually only for cases where dependency to
ObjectMapper
is
either not possible (from Streaming API), or undesireable (when only relying
on Streaming API).
Mapper instances are fully thread-safe provided that ALL configuration of the
instance occurs before ANY read or write calls. If configuration of a mapper instance
is modified after first usage, changes may or may not take effect, and configuration
calls themselves may fail.
If you need to use different configuration, you have two main possibilities:
ObjectReader
for reading,
ObjectWriter
for writing.
Both types are fully immutable and you can freely create new instances with different
configuration using either factory methods of
ObjectMapper
, or readers/writers
themselves. Construction of new
ObjectReader
s and
ObjectWriter
s is
a very light-weight operation so it is usually appropriate to create these on per-call
basis, as needed, for configuring things like optional indentation of JSON.
ObjectReader
and
ObjectWriter
, you may need to use multiple
ObjectMapper
instead (for example:
you cannot change mix-in annotations on-the-fly; or, set of custom (de)serializers).
To help with this usage, you may want to use method
copy()
which creates a clone
of the mapper with specific configuration, and allows configuration of the copied instance
before it gets used. Note that
copy()
operation is as expensive as constructing
a new
ObjectMapper
instance: if possible, you should still pool and reuse mappers
if you intend to use them for multiple operations.
Note on caching: root-level deserializers are always cached, and accessed
using full (generics-aware) type information. This is different from
caching of referenced types, which is more limited and is done only
for a subset of all deserializer types. The main reason for difference
is that at root-level there is no incoming reference (and hence no
referencing property, no referral information or annotations to
produce differing deserializers), and that the performance impact
greatest at root level (since it'll essentially cache the full
graph of deserializers involved).
Notes on security: use "default typing" feature (see
enableDefaultTyping()
)
is a potential security risk, if used with untrusted content (content generated by
untrusted external parties). If so, you may want to construct a custom
TypeResolverBuilder
implementation to limit possible types to instantiate,
(using
setDefaultTyping(com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder<?>)
).
static class
ObjectMapper.DefaultTypeResolverBuilder
TypeResolverBuilder
that provides type resolver builders
used with so-called "default typing"
(see
activateDefaultTyping(PolymorphicTypeValidator)
for details).
static class
ObjectMapper.DefaultTyping
activateDefaultTyping(PolymorphicTypeValidator)
to specify what kind of types (classes) default typing should
be used for.
ObjectMapper
()
JsonFactory
as necessary, use
SerializerProvider
as its
SerializerProvider
, and
BeanSerializerFactory
as its
SerializerFactory
.
ObjectMapper
(
JsonFactory
jf)
JsonFactory
for constructing necessary
JsonParser
s and/or
JsonGenerator
s.
ObjectMapper
(
JsonFactory
jf,
DefaultSerializerProvider
sp,
DefaultDeserializationContext
dc)
JsonFactory
for constructing necessary
JsonParser
s and/or
JsonGenerator
s, and uses given providers for accessing
serializers and deserializers.
acceptJsonFormatVisitor
(java.lang.Class<?> type,
JsonFormatVisitorWrapper
visitor)
acceptJsonFormatVisitor
(
JavaType
type,
JsonFormatVisitorWrapper
visitor)
ObjectMapper
activateDefaultTyping
(
PolymorphicTypeValidator
ptv)
ObjectMapper
activateDefaultTyping
(
PolymorphicTypeValidator
ptv,
ObjectMapper.DefaultTyping
applicability)
ObjectMapper
activateDefaultTyping
(
PolymorphicTypeValidator
ptv,
ObjectMapper.DefaultTyping
applicability,
JsonTypeInfo.As
includeAs)
JsonTypeInfo
).
ObjectMapper
activateDefaultTypingAsProperty
(
PolymorphicTypeValidator
ptv,
ObjectMapper.DefaultTyping
applicability,
java.lang.String propertyName)
JsonTypeInfo
) --
using "As.PROPERTY" inclusion mechanism and specified property name
to use for inclusion (default being "@class" since default type information
always uses class name as type identifier)
ObjectMapper
addHandler
(
DeserializationProblemHandler
h)
DeserializationProblemHandler
to be used for handling specific problems during deserialization.
ObjectMapper
addMixIn
(java.lang.Class<?> target,
java.lang.Class<?> mixinSource)
addMixInAnnotations
(java.lang.Class<?> target,
java.lang.Class<?> mixinSource)
addMixIn(Class, Class)
.
boolean
canDeserialize
(
JavaType
type)
boolean
canDeserialize
(
JavaType
type,
java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
canDeserialize(JavaType)
but that can return
actual
Throwable
that was thrown when trying to construct
serializer: this may be useful in figuring out what the actual problem is.
boolean
canSerialize
(java.lang.Class<?> type)
boolean
canSerialize
(java.lang.Class<?> type,
java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
canSerialize(Class)
but that can return
actual
Throwable
that was thrown when trying to construct
serializer: this may be useful in figuring out what the actual problem is.
ObjectMapper
clearProblemHandlers
()
DeserializationProblemHandler
s
instances from this mapper.
MutableConfigOverride
configOverride
(java.lang.Class<?> type)
ObjectMapper
configure
(
DeserializationFeature
f,
boolean state)
ObjectMapper
configure
(
JsonGenerator.Feature
f,
boolean state)
JsonGenerator
feature for
generator instances this object mapper creates.
ObjectMapper
configure
(
JsonParser.Feature
f,
boolean state)
JsonParser.Feature
s
for parser instances this object mapper creates.
ObjectMapper
configure
(
MapperFeature
f,
boolean state)
ObjectMapper
configure
(
SerializationFeature
f,
boolean state)
JavaType
constructType
(java.lang.reflect.Type t)
JavaType
out of given
type (typically
java.lang.Class
), but without explicit
context.
convertValue
(java.lang.Object fromValue,
java.lang.Class<T> toValueType)
convertValue
(java.lang.Object fromValue,
JavaType
toValueType)
convertValue
(java.lang.Object fromValue,
TypeReference
<T> toValueTypeRef)
ObjectMapper
copy
()
ObjectMapper
instance that
has same initial configuration as this instance.
ArrayNode
createArrayNode
()
Note: return type is co-variant, as basic ObjectCodec
abstraction cannot refer to concrete node types (as it's
part of core package, whereas impls are part of mapper
package)
ObjectNode
createObjectNode
()
Note: return type is co-variant, as basic ObjectCodec
abstraction cannot refer to concrete node types (as it's
part of core package, whereas impls are part of mapper
package)
ObjectMapper
deactivateDefaultTyping
()
JsonTypeInfo
) will have
additional embedded type information.
ObjectMapper
disable
(
DeserializationFeature
feature)
DeserializationConfig
features.
ObjectMapper
disable
(
DeserializationFeature
first,
DeserializationFeature
... f)
DeserializationConfig
features.
ObjectMapper
disable
(
JsonGenerator.Feature
... features)
JsonGenerator.Feature
s
for parser instances this object mapper creates.
ObjectMapper
disable
(
JsonParser.Feature
... features)
JsonParser.Feature
s
for parser instances this object mapper creates.
ObjectMapper
disable
(
MapperFeature
... f)
ObjectMapper
disable
(
SerializationFeature
f)
DeserializationConfig
features.
ObjectMapper
disable
(
SerializationFeature
first,
SerializationFeature
... f)
DeserializationConfig
features.
ObjectMapper
disableDefaultTyping
()
deactivateDefaultTyping()
instead
ObjectMapper
enable
(
JsonGenerator.Feature
... features)
JsonGenerator.Feature
s
for parser instances this object mapper creates.
ObjectMapper
enable
(
JsonParser.Feature
... features)
JsonParser.Feature
s
for parser instances this object mapper creates.
ObjectMapper
enable
(
MapperFeature
... f)
ObjectMapper
enable
(
SerializationFeature
f)
DeserializationConfig
feature.
ObjectMapper
enable
(
SerializationFeature
first,
SerializationFeature
... f)
DeserializationConfig
features.
ObjectMapper
enableDefaultTyping
()
activateDefaultTyping(PolymorphicTypeValidator)
instead
ObjectMapper
enableDefaultTyping
(
ObjectMapper.DefaultTyping
applicability,
JsonTypeInfo.As
includeAs)
activateDefaultTyping(PolymorphicTypeValidator,DefaultTyping,JsonTypeInfo.As)
instead
ObjectMapper
enableDefaultTypingAsProperty
(
ObjectMapper.DefaultTyping
applicability,
java.lang.String propertyName)
activateDefaultTypingAsProperty(PolymorphicTypeValidator,DefaultTyping,String)
instead
ObjectMapper
findAndRegisterModules
()
static java.util.List<
Module
>
findModules
()
ServiceLoader
facility, along with module-provided SPI.
static java.util.List<
Module
>
findModules
(java.lang.ClassLoader classLoader)
ServiceLoader
facility, along with module-provided SPI.
JsonSchema
generateJsonSchema
(java.lang.Class<?> t)
acceptJsonFormatVisitor(JavaType, JsonFormatVisitorWrapper)
)
getDeserializationConfig
()
DeserializationConfig
object
that defines configuration settings for deserialization.
DeserializationContext
getDeserializationContext
()
DeserializationContext
.
JsonFactory
getFactory
()
JsonFactory
) codec will use for data binding.
InjectableValues
getInjectableValues
()
JsonFactory
getJsonFactory
()
getFactory()
instead
getNodeFactory
()
JsonNodeFactory
that this mapper will use when directly constructing
root
JsonNode
instances for Trees.
PolymorphicTypeValidator
getPolymorphicTypeValidator
()
PolymorphicTypeValidator
used for validating
polymorphic subtypes used with explicit polymorphic types (annotation-based),
but NOT one with "default typing" (see
activateDefaultTyping(PolymorphicTypeValidator)
for details).
PropertyNamingStrategy
getPropertyNamingStrategy
()
java.util.Set<java.lang.Object>
getRegisteredModuleIds
()
Module
typeIds that are registered in this
ObjectMapper.
SerializationConfig
getSerializationConfig
()
SerializationConfig
object that defines configuration settings for serialization.
SerializerFactory
getSerializerFactory
()
SerializerFactory
.
SerializerProvider
getSerializerProvider
()
DefaultSerializerProvider.createInstance(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.ser.SerializerFactory)
.
SerializerProvider
getSerializerProviderInstance
()
SerializerProvider
instance that may be used for accessing serializers.
SubtypeResolver
getSubtypeResolver
()
TypeFactory
getTypeFactory
()
TypeFactory
instance.
VisibilityChecker
<?>
getVisibilityChecker
()
boolean
isEnabled
(
DeserializationFeature
f)
boolean
isEnabled
(
JsonFactory.Feature
f)
boolean
isEnabled
(
JsonGenerator.Feature
f)
boolean
isEnabled
(
JsonParser.Feature
f)
boolean
isEnabled
(
MapperFeature
f)
MapperFeature
is enabled.
boolean
isEnabled
(
SerializationFeature
f)
boolean
isEnabled
(
StreamReadFeature
f)
boolean
isEnabled
(
StreamWriteFeature
f)
JsonNode
missingNode
()
mixInCount
()
JsonNode
nullNode
()
ObjectReader
reader
()
ObjectReader
with
default settings.
ObjectReader
reader
(
Base64Variant
defaultBase64)
ObjectReader
that will
use specified Base64 encoding variant for Base64-encoded binary data.
ObjectReader
reader
(java.lang.Class<?> type)
readerFor(Class)
instead
ObjectReader
reader
(
ContextAttributes
attrs)
ObjectReader
that will
use specified default attributes.
ObjectReader
reader
(
DeserializationFeature
feature)
ObjectReader
with
specified feature enabled (compared to settings that this
mapper instance has).
ObjectReader
reader
(
DeserializationFeature
first,
DeserializationFeature
... other)
ObjectReader
with
specified features enabled (compared to settings that this
mapper instance has).
ObjectReader
reader
(
FormatSchema
schema)
ObjectReader
that will
pass specific schema object to
JsonParser
used for
reading content.
ObjectReader
reader
(
InjectableValues
injectableValues)
ObjectReader
that will
use specified injectable values.
ObjectReader
reader
(
JavaType
type)
readerFor(JavaType)
instead
ObjectReader
reader
(
JsonNodeFactory
f)
ObjectReader
that will
use specified
JsonNodeFactory
for constructing JSON trees.
ObjectReader
reader
(
TypeReference
<?> type)
readerFor(TypeReference)
instead
ObjectReader
readerFor
(java.lang.Class<?> type)
ObjectReader
that will
read or update instances of specified type
ObjectReader
readerFor
(
JavaType
type)
ObjectReader
that will
read or update instances of specified type
ObjectReader
readerFor
(
TypeReference
<?> type)
ObjectReader
that will
read or update instances of specified type
ObjectReader
readerForUpdating
(java.lang.Object valueToUpdate)
ObjectReader
that will
update given Object (usually Bean, but can be a Collection or Map
as well, but NOT an array) with JSON data.
ObjectReader
readerWithView
(java.lang.Class<?> view)
ObjectReader
that will
deserialize objects using specified JSON View (filter).
JsonNode
readTree
(byte[] content)
readTree(InputStream)
except content read from
passed-in byte array.
JsonNode
readTree
(byte[] content,
int offset,
int len)
readTree(InputStream)
except content read from
passed-in byte array.
JsonNode
readTree
(java.io.File file)
readTree(InputStream)
except content read from
passed-in
File
.
JsonNode
readTree
(java.io.InputStream in)
JsonNode
instances.
<T extends
TreeNode
>
T
readTree
(
JsonParser
p)
JsonNode
.
JsonNode
readTree
(java.io.Reader r)
readTree(InputStream)
except content accessed through
passed-in
Reader
JsonNode
readTree
(java.lang.String content)
readTree(InputStream)
except content read from
passed-in
String
JsonNode
readTree
(java.net.URL source)
readTree(InputStream)
except content read from
passed-in
URL
.
readValue
(byte[] src,
java.lang.Class<T> valueType)
readValue
(byte[] src,
int offset,
int len,
java.lang.Class<T> valueType)
readValue
(byte[] src,
int offset,
int len,
JavaType
valueType)
readValue
(byte[] src,
int offset,
int len,
TypeReference
<T> valueTypeRef)
readValue
(byte[] src,
JavaType
valueType)
readValue
(byte[] src,
TypeReference
<T> valueTypeRef)
readValue
(java.io.DataInput src,
java.lang.Class<T> valueType)
readValue
(java.io.DataInput src,
JavaType
valueType)
readValue
(java.io.File src,
java.lang.Class<T> valueType)
readValue
(java.io.File src,
JavaType
valueType)
readValue
(java.io.File src,
TypeReference
<T> valueTypeRef)
readValue
(java.io.InputStream src,
java.lang.Class<T> valueType)
readValue
(java.io.InputStream src,
JavaType
valueType)
readValue
(java.io.InputStream src,
TypeReference
<T> valueTypeRef)
readValue
(
JsonParser
p,
java.lang.Class<T> valueType)
Boolean
).
readValue
(
JsonParser
p,
JavaType
valueType)
readValue(JsonParser, Class)
.
readValue
(
JsonParser
p,
ResolvedType
valueType)
readValue
(
JsonParser
p,
TypeReference
<T> valueTypeRef)
readValue
(java.io.Reader src,
java.lang.Class<T> valueType)
readValue
(java.io.Reader src,
JavaType
valueType)
readValue
(java.io.Reader src,
TypeReference
<T> valueTypeRef)
readValue
(java.lang.String content,
java.lang.Class<T> valueType)
readValue
(java.lang.String content,
JavaType
valueType)
readValue
(java.lang.String content,
TypeReference
<T> valueTypeRef)
readValue
(java.net.URL src,
java.lang.Class<T> valueType)
readValue
(java.net.URL src,
JavaType
valueType)
readValue(java.net.URL, Class)
except that target specified by
JavaType
.
readValue
(java.net.URL src,
TypeReference
<T> valueTypeRef)
readValue(java.net.URL, Class)
except that target specified by
TypeReference
.
<T>
MappingIterator
<T>
readValues
(
JsonParser
p,
java.lang.Class<T> valueType)
<T>
MappingIterator
<T>
readValues
(
JsonParser
p,
JavaType
valueType)
<T>
MappingIterator
<T>
readValues
(
JsonParser
p,
ResolvedType
valueType)
<T>
MappingIterator
<T>
readValues
(
JsonParser
p,
TypeReference
<T> valueTypeRef)
ObjectMapper
registerModule
(
Module
module)
ObjectMapper
registerModules
(java.lang.Iterable<? extends
Module
> modules)
ObjectMapper
registerModules
(
Module
... modules)
registerSubtypes
(java.lang.Class<?>... classes)
registerSubtypes
(java.util.Collection<java.lang.Class<?>> subtypes)
registerSubtypes
(
NamedType
... types)
ObjectMapper
setAnnotationIntrospector
(
AnnotationIntrospector
ai)
AnnotationIntrospector
used by this
mapper instance for both serialization and deserialization.
ObjectMapper
setAnnotationIntrospectors
(
AnnotationIntrospector
serializerAI,
AnnotationIntrospector
deserializerAI)
AnnotationIntrospector
instances used
by this mapper instance for serialization and deserialization,
specifying them separately so that different introspection can be
used for different aspects
ObjectMapper
setBase64Variant
(
Base64Variant
v)
Base64Variant
that
byte[]
serializers and deserializers will use.
ObjectMapper
setConfig
(
DeserializationConfig
config)
DeserializationConfig
object.
ObjectMapper
setConfig
(
SerializationConfig
config)
SerializationConfig
object, which contains serialization-specific configuration settings.
ObjectMapper
setDateFormat
(java.text.DateFormat dateFormat)
DateFormat
to use when serializing time
values as Strings, and deserializing from JSON Strings.
ObjectMapper
setDefaultLeniency
(java.lang.Boolean b)
ObjectMapper
setDefaultMergeable
(java.lang.Boolean b)
ObjectMapper
setDefaultPrettyPrinter
(
PrettyPrinter
pp)
PrettyPrinter
to use when "default pretty-printing"
is enabled (by enabling
SerializationFeature.INDENT_OUTPUT
)
ObjectMapper
setDefaultPropertyInclusion
(
JsonInclude.Include
incl)
ObjectMapper
setDefaultPropertyInclusion
(
JsonInclude.Value
incl)
ObjectMapper
setDefaultSetterInfo
(
JsonSetter.Value
v)
ObjectMapper
setDefaultTyping
(
TypeResolverBuilder
<?> typer)
ObjectMapper
setDefaultVisibility
(
JsonAutoDetect.Value
vis)
JsonAutoDetect
) or per-type
visibility overrides.
ObjectMapper
setFilterProvider
(
FilterProvider
filterProvider)
FilterProvider
for
mapping Filter Ids to actual filter instances.
setFilters
(
FilterProvider
filterProvider)
setFilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)
instead (allows chaining)
java.lang.Object
setHandlerInstantiator
(
HandlerInstantiator
hi)
HandlerInstantiator
to use for creating
instances of handlers (such as serializers, deserializers, type and type
id resolvers), given a class.
ObjectMapper
setInjectableValues
(
InjectableValues
injectableValues)
InjectableValues
which used to find
values to inject.
ObjectMapper
setLocale
(java.util.Locale l)
setMixInAnnotations
(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> sourceMixins)
setMixIns(java.util.Map<java.lang.Class<?>, java.lang.Class<?>>)
.
ObjectMapper
setMixInResolver
(
ClassIntrospector.MixInResolver
resolver)
ObjectMapper
setMixIns
(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> sourceMixins)
ObjectMapper
setNodeFactory
(
JsonNodeFactory
f)
JsonNodeFactory
to use for
constructing root level tree nodes (via method
createObjectNode()
ObjectMapper
setPolymorphicTypeValidator
(
PolymorphicTypeValidator
ptv)
PolymorphicTypeValidator
to use for validating
polymorphic subtypes used with explicit polymorphic types (annotation-based),
but NOT one with "default typing" (see
activateDefaultTyping(PolymorphicTypeValidator)
for details).
ObjectMapper
setPropertyInclusion
(
JsonInclude.Value
incl)
ObjectMapper
setSerializerFactory
(
SerializerFactory
f)
SerializerFactory
to use
for constructing (bean) serializers.
ObjectMapper
setSerializerProvider
(
DefaultSerializerProvider
p)
SerializerProvider
instance
to use as the base for actual provider instances to use for handling
caching of
JsonSerializer
instances.
ObjectMapper
setSubtypeResolver
(
SubtypeResolver
str)
ObjectMapper
setTimeZone
(java.util.TimeZone tz)
ObjectMapper
setTypeFactory
(
TypeFactory
f)
TypeFactory
instance
used by this mapper.
ObjectMapper
setVisibility
(
PropertyAccessor
forMethod,
JsonAutoDetect.Visibility
visibility)
VisibilityChecker
s, to change details of what kinds of
properties are auto-detected.
ObjectMapper
setVisibility
(
VisibilityChecker
<?> vc)
VisibilityChecker
,
object used for determining whether given property element
(method, field, constructor) can be auto-detected or not.
setVisibilityChecker
(
VisibilityChecker
<?> vc)
setVisibility(VisibilityChecker)
instead.
JsonFactory
tokenStreamFactory
()
JsonFactory
that this
mapper uses if it needs to construct
JsonParser
s
and/or
JsonGenerator
s.
JsonParser
treeAsTokens
(
TreeNode
n)
JsonParser
out of JSON tree
representation.
treeToValue
(
TreeNode
n,
java.lang.Class<T> valueType)
updateValue
(T valueToUpdate,
java.lang.Object overrides)
convertValue(Object, JavaType)
but one
in which
<T extends
JsonNode
>
T
valueToTree
(java.lang.Object fromValue)
treeToValue(com.fasterxml.jackson.core.TreeNode, java.lang.Class<T>)
; given a value (usually bean), will
construct equivalent JSON Tree representation.
Version
version
()
ObjectWriter
writer
()
ObjectWriter
with default settings.
ObjectWriter
writer
(
Base64Variant
defaultBase64)
ObjectWriter
that will
use specified Base64 encoding variant for Base64-encoded binary data.
ObjectWriter
writer
(
CharacterEscapes
escapes)
ObjectReader
that will
use specified character escaping details for output.
ObjectWriter
writer
(
ContextAttributes
attrs)
ObjectWriter
that will
use specified default attributes.
ObjectWriter
writer
(java.text.DateFormat df)
ObjectWriter
that will
serialize objects using specified
DateFormat
; or, if
null passed, using timestamp (64-bit number.
ObjectWriter
writer
(
FilterProvider
filterProvider)
ObjectWriter
that will
serialize objects using specified filter provider.
ObjectWriter
writer
(
FormatSchema
schema)
ObjectWriter
that will
pass specific schema object to
JsonGenerator
used for
writing content.
ObjectWriter
writer
(
PrettyPrinter
pp)
ObjectWriter
that will
serialize objects using specified pretty printer for indentation
(or if null, no pretty printer)
ObjectWriter
writer
(
SerializationFeature
feature)
ObjectWriter
with
specified feature enabled (compared to settings that this
mapper instance has).
ObjectWriter
writer
(
SerializationFeature
first,
SerializationFeature
... other)
ObjectWriter
with
specified features enabled (compared to settings that this
mapper instance has).
ObjectWriter
writerFor
(java.lang.Class<?> rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value.
ObjectWriter
writerFor
(
JavaType
rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value.
ObjectWriter
writerFor
(
TypeReference
<?> rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value.
ObjectWriter
writerWithDefaultPrettyPrinter
()
ObjectWriter
that will
serialize objects using the default pretty printer for indentation
ObjectWriter
writerWithType
(java.lang.Class<?> rootType)
writerFor(Class)
instead
ObjectWriter
writerWithView
(java.lang.Class<?> serializationView)
ObjectWriter
that will
serialize objects using specified JSON View (filter).
writeTree
(
JsonGenerator
g,
JsonNode
rootNode)
writeTree
(
JsonGenerator
g,
TreeNode
rootNode)
writeValue
(java.io.DataOutput out,
java.lang.Object value)
writeValue
(java.io.File resultFile,
java.lang.Object value)
writeValue
(
JsonGenerator
g,
java.lang.Object value)
JsonGenerator
.
writeValue
(java.io.OutputStream out,
java.lang.Object value)
JsonEncoding.UTF8
).
writeValue
(java.io.Writer w,
java.lang.Object value)
byte[]
writeValueAsBytes
(java.lang.Object value)
java.lang.String
writeValueAsString
(java.lang.Object value)
public ObjectMapper()
JsonFactory
as necessary, use
SerializerProvider
as its
SerializerProvider
, and
BeanSerializerFactory
as its
SerializerFactory
.
This means that it
can serialize all standard JDK types, as well as regular
Java Beans (based on method names and Jackson-specific annotations),
but does not support JAXB annotations.
public ObjectMapper(JsonFactory jf)
JsonFactory
for constructing necessary
JsonParser
s and/or
JsonGenerator
s.
public ObjectMapper(JsonFactory jf, DefaultSerializerProvider sp, DefaultDeserializationContext dc)
JsonFactory
for constructing necessary
JsonParser
s and/or
JsonGenerator
s, and uses given providers for accessing
serializers and deserializers.
jf
- JsonFactory to use: if null, a new
MappingJsonFactory
will be constructed
sp
- SerializerProvider to use: if null, a
SerializerProvider
will be constructed
dc
- Blueprint deserialization context instance to use for creating
actual context objects; if null, will construct standard
DeserializationContext
public ObjectMapper copy()
ObjectMapper
instance that
has same initial configuration as this instance. Note that this
also requires making a copy of the underlying
JsonFactory
instance.
Method is typically
used when multiple, differently configured mappers are needed.
Although configuration is shared, cached serializers and deserializers
are NOT shared, which means that the new instance may be re-configured
before use; meaning that it behaves the same way as if an instance
was constructed from scratch.
public Version version()
version
in interface
Versioned
version
in class
ObjectCodec
public ObjectMapper registerModule(Module module)
module
- Module to register
public ObjectMapper registerModules(Module... modules)
public ObjectMapper registerModules(java.lang.Iterable<? extends Module> modules)
public java.util.Set<java.lang.Object> getRegisteredModuleIds()
Module
typeIds that are registered in this
ObjectMapper. By default the typeId for a module is it's full
class name (see
Module.getTypeId()
).
public static java.util.List<Module> findModules()
ServiceLoader
facility, along with module-provided SPI.
Note that method does not do any caching, so calls should be considered
potentially expensive.
public static java.util.List<Module> findModules(java.lang.ClassLoader classLoader)
ServiceLoader
facility, along with module-provided SPI.
Note that method does not do any caching, so calls should be considered
potentially expensive.
public ObjectMapper findAndRegisterModules()
findModules()
, no caching is done for modules, so care
needs to be taken to either create and share a single mapper instance;
or to cache introspected set of modules.
public SerializationConfig getSerializationConfig()
SerializationConfig
object that defines configuration settings for serialization.
Note that since instances are immutable, you can NOT change settings
by accessing an instance and calling methods: this will simply create
new instance of config object.
public DeserializationConfig getDeserializationConfig()
DeserializationConfig
object
that defines configuration settings for deserialization.
Note that since instances are immutable, you can NOT change settings
by accessing an instance and calling methods: this will simply create
new instance of config object.
public DeserializationContext getDeserializationContext()
DeserializationContext
.
Note that since instances are immutable, you can NOT change settings
by accessing an instance and calling methods: this will simply create
new instance of context object.
public ObjectMapper setSerializerFactory(SerializerFactory f)
SerializerFactory
to use
for constructing (bean) serializers.
public SerializerFactory getSerializerFactory()
SerializerFactory
.
Note that since instances are immutable, you can NOT change settings
by accessing an instance and calling methods: this will simply create
new instance of factory object.
public ObjectMapper setSerializerProvider(DefaultSerializerProvider p)
SerializerProvider
instance
to use as the base for actual provider instances to use for handling
caching of
JsonSerializer
instances.
public SerializerProvider getSerializerProvider()
DefaultSerializerProvider.createInstance(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.ser.SerializerFactory)
.
Note that returned instance cannot be directly used as it is not properly
configured: to get a properly configured instance to call, use
getSerializerProviderInstance()
instead.
public SerializerProvider getSerializerProviderInstance()
SerializerProvider
instance that may be used for accessing serializers. This is same as
calling
getSerializerProvider()
, and calling
createInstance
on it.
public ObjectMapper setMixIns(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> sourceMixins)
public ObjectMapper addMixIn(java.lang.Class<?> target, java.lang.Class<?> mixinSource)
mixinSource
are taken to override annotations
that
target
(or its supertypes) has.
target
- Class (or interface) whose annotations to effectively override
mixinSource
- Class (or interface) whose annotations are to
be "added" to target's annotations, overriding as necessary
public ObjectMapper setMixInResolver(ClassIntrospector.MixInResolver resolver)
@Deprecated public void setMixInAnnotations(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> sourceMixins)
setMixIns(java.util.Map<java.lang.Class<?>, java.lang.Class<?>>)
.
@Deprecated public final void addMixInAnnotations(java.lang.Class<?> target, java.lang.Class<?> mixinSource)
addMixIn(Class, Class)
.
public VisibilityChecker<?> getVisibilityChecker()
public ObjectMapper setVisibility(VisibilityChecker<?> vc)
VisibilityChecker
,
object used for determining whether given property element
(method, field, constructor) can be auto-detected or not.
This default checker is used as the base visibility:
per-class overrides (both via annotations and per-type config overrides)
can further change these settings.
public ObjectMapper setVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility)
VisibilityChecker
s, to change details of what kinds of
properties are auto-detected.
Basically short cut for doing:
mapper.setVisibilityChecker(
mapper.getVisibilityChecker().withVisibility(forMethod, visibility)
one common use case would be to do:
mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
which would make all member fields serializable without further annotations,
instead of just public fields (default setting).
forMethod
- Type of property descriptor affected (field, getter/isGetter,
setter, creator)
visibility
- Minimum visibility to require for the property descriptors of type
public ObjectMapper setAnnotationIntrospector(AnnotationIntrospector ai)
AnnotationIntrospector
used by this
mapper instance for both serialization and deserialization.
Note that doing this will replace the current introspector, which
may lead to unavailability of core Jackson annotations.
If you want to combine handling of multiple introspectors,
have a look at
AnnotationIntrospectorPair
.
AnnotationIntrospectorPair
public ObjectMapper setAnnotationIntrospectors(AnnotationIntrospector serializerAI, AnnotationIntrospector deserializerAI)
AnnotationIntrospector
instances used
by this mapper instance for serialization and deserialization,
specifying them separately so that different introspection can be
used for different aspects
serializerAI
-
AnnotationIntrospector
to use for configuring
serialization
deserializerAI
-
AnnotationIntrospector
to use for configuring
deserialization
AnnotationIntrospectorPair
public ObjectMapper setPropertyNamingStrategy(PropertyNamingStrategy s)
public ObjectMapper setDefaultPrettyPrinter(PrettyPrinter pp)
PrettyPrinter
to use when "default pretty-printing"
is enabled (by enabling
SerializationFeature.INDENT_OUTPUT
)
pp
- Pretty printer to use by default.
@Deprecated public void setVisibilityChecker(VisibilityChecker<?> vc)
setVisibility(VisibilityChecker)
instead.
public ObjectMapper setPolymorphicTypeValidator(PolymorphicTypeValidator ptv)
PolymorphicTypeValidator
to use for validating
polymorphic subtypes used with explicit polymorphic types (annotation-based),
but NOT one with "default typing" (see
activateDefaultTyping(PolymorphicTypeValidator)
for details).
public PolymorphicTypeValidator getPolymorphicTypeValidator()
PolymorphicTypeValidator
used for validating
polymorphic subtypes used with explicit polymorphic types (annotation-based),
but NOT one with "default typing" (see
activateDefaultTyping(PolymorphicTypeValidator)
for details).
public ObjectMapper setSerializationInclusion(JsonInclude.Include incl)
JsonInclude.Include.ALWAYS
.
@Deprecated public ObjectMapper setPropertyInclusion(JsonInclude.Value incl)
setDefaultPropertyInclusion(com.fasterxml.jackson.annotation.JsonInclude.Value)
public ObjectMapper setDefaultPropertyInclusion(JsonInclude.Value incl)
setPropertyInclusion
)
public ObjectMapper setDefaultPropertyInclusion(JsonInclude.Include incl)
setPropertyInclusion
)
public ObjectMapper setDefaultSetterInfo(JsonSetter.Value v)
public ObjectMapper setDefaultVisibility(JsonAutoDetect.Value vis)
JsonAutoDetect
) or per-type
visibility overrides.
public ObjectMapper setDefaultMergeable(java.lang.Boolean b)
public void registerSubtypes(java.lang.Class<?>... classes)
public void registerSubtypes(NamedType... types)
public void registerSubtypes(java.util.Collection<java.lang.Class<?>> subtypes)
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv)
PolymorphicTypeValidator
to pass is critical for security
as allowing all subtypes can be risky for untrusted content.
ptv
- Validator used to verify that actual subtypes to deserialize are valid against
whatever criteria validator uses: important in case where untrusted content is deserialized.
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv, ObjectMapper.DefaultTyping applicability)
PolymorphicTypeValidator
to pass is critical for security
as allowing all subtypes can be risky for untrusted content.
ptv
- Validator used to verify that actual subtypes to deserialize are valid against
whatever criteria validator uses: important in case where untrusted content is deserialized.
applicability
- Defines kinds of types for which additional type information
is added; see
ObjectMapper.DefaultTyping
for more information.
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv, ObjectMapper.DefaultTyping applicability, JsonTypeInfo.As includeAs)
JsonTypeInfo
).
NOTE: use of
JsonTypeInfo.As#EXTERNAL_PROPERTY
NOT SUPPORTED
;
and attempts of do so will throw an
IllegalArgumentException
to make
this limitation explicit.
NOTE: choice of
PolymorphicTypeValidator
to pass is critical for security
as allowing all subtypes can be risky for untrusted content.
ptv
- Validator used to verify that actual subtypes to deserialize are valid against
whatever criteria validator uses: important in case where untrusted content is deserialized.
applicability
- Defines kinds of types for which additional type information
is added; see
ObjectMapper.DefaultTyping
for more information.
includeAs
-
public ObjectMapper activateDefaultTypingAsProperty(PolymorphicTypeValidator ptv, ObjectMapper.DefaultTyping applicability, java.lang.String propertyName)
JsonTypeInfo
) --
using "As.PROPERTY" inclusion mechanism and specified property name
to use for inclusion (default being "@class" since default type information
always uses class name as type identifier)
NOTE: choice of
PolymorphicTypeValidator
to pass is critical for security
as allowing all subtypes can be risky for untrusted content.
ptv
- Validator used to verify that actual subtypes to deserialize are valid against
whatever criteria validator uses: important in case where untrusted content is deserialized.
applicability
- Defines kinds of types for which additional type information
is added; see
ObjectMapper.DefaultTyping
for more information.
propertyName
- Name of property used for including type id for polymorphic values.
public ObjectMapper deactivateDefaultTyping()
JsonTypeInfo
) will have
additional embedded type information.
public ObjectMapper setDefaultTyping(TypeResolverBuilder<?> typer)
TypeResolverBuilder
that can limit allowed classes to
deserialize. Note in particular that
StdTypeResolverBuilder
DOES NOT limit applicability but creates type (de)serializers for all
types.
typer
- Type information inclusion handler
@Deprecated public ObjectMapper enableDefaultTyping(ObjectMapper.DefaultTyping dti)
activateDefaultTyping(PolymorphicTypeValidator,DefaultTyping)
instead
@Deprecated public ObjectMapper enableDefaultTyping(ObjectMapper.DefaultTyping applicability, JsonTypeInfo.As includeAs)
activateDefaultTyping(PolymorphicTypeValidator,DefaultTyping,JsonTypeInfo.As)
instead
@Deprecated public ObjectMapper enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping applicability, java.lang.String propertyName)
activateDefaultTypingAsProperty(PolymorphicTypeValidator,DefaultTyping,String)
instead
public MutableConfigOverride configOverride(java.lang.Class<?> type)
Date
(possibly further overridden by per-property
annotations)
public ObjectMapper setTypeFactory(TypeFactory f)
TypeFactory
instance
used by this mapper.
Note: will also set
TypeFactory
that deserialization and
serialization config objects use.
public JavaType constructType(java.lang.reflect.Type t)
JavaType
out of given
type (typically
java.lang.Class
), but without explicit
context.
public JsonNodeFactory getNodeFactory()
JsonNodeFactory
that this mapper will use when directly constructing
root
JsonNode
instances for Trees.
Note: this is just a shortcut for calling
getDeserializationConfig().getNodeFactory()
public ObjectMapper setNodeFactory(JsonNodeFactory f)
JsonNodeFactory
to use for
constructing root level tree nodes (via method
createObjectNode()
public ObjectMapper addHandler(DeserializationProblemHandler h)
DeserializationProblemHandler
to be used for handling specific problems during deserialization.
public ObjectMapper clearProblemHandlers()
DeserializationProblemHandler
s
instances from this mapper.
public ObjectMapper setConfig(DeserializationConfig config)
DeserializationConfig
object.
It is added as a fallback method that may be used if no other configuration
modifier method works: it should not be used if there are alternatives,
and its use is generally discouraged.
NOTE
: only use this method if you know what you are doing -- it allows
by-passing some of checks applied to other configuration methods.
Also keep in mind that as with all configuration of
ObjectMapper
,
this is only thread-safe if done before calling any deserialization methods.
public ObjectMapper setFilterProvider(FilterProvider filterProvider)
FilterProvider
for
mapping Filter Ids to actual filter instances.
Note that usually it is better to use method
writer(FilterProvider)
;
however, sometimes
this method is more convenient. For example, some frameworks only allow configuring
of ObjectMapper instances and not
ObjectWriter
s.
public ObjectMapper setBase64Variant(Base64Variant v)
Base64Variant
that
byte[]
serializers and deserializers will use.
v
- Base64 variant to use
public ObjectMapper setConfig(SerializationConfig config)
SerializationConfig
object, which contains serialization-specific configuration settings.
It is added as a fallback method that may be used if no other configuration
modifier method works: it should not be used if there are alternatives,
and its use is generally discouraged.
NOTE
: only use this method if you know what you are doing -- it allows
by-passing some of checks applied to other configuration methods.
Also keep in mind that as with all configuration of
ObjectMapper
,
this is only thread-safe if done before calling any serialization methods.
public JsonFactory tokenStreamFactory()
JsonFactory
that this
mapper uses if it needs to construct
JsonParser
s
and/or
JsonGenerator
s.
WARNING: note that all
ObjectReader
and
ObjectWriter
instances created by this mapper usually share the same configured
JsonFactory
, so changes to its configuration will "leak".
To avoid such observed changes you should always use "with()" and
"without()" method of
ObjectReader
and
ObjectWriter
for changing
JsonParser.Feature
and
JsonGenerator.Feature
settings to use on per-call basis.
JsonFactory
that this mapper uses when it needs to
construct Json parser and generators
public JsonFactory getFactory()
ObjectCodec
JsonFactory
) codec will use for data binding.
getFactory
in class
ObjectCodec
@Deprecated public JsonFactory getJsonFactory()
getFactory()
instead
getJsonFactory
in class
ObjectCodec
public ObjectMapper setDateFormat(java.text.DateFormat dateFormat)
DateFormat
to use when serializing time
values as Strings, and deserializing from JSON Strings.
This is preferably to directly modifying
SerializationConfig
and
DeserializationConfig
instances.
If you need per-request configuration, use
writer(DateFormat)
to
create properly configured
ObjectWriter
and use that; this because
ObjectWriter
s are thread-safe whereas ObjectMapper itself is only
thread-safe when configuring methods (such as this one) are NOT called.
public java.lang.Object setHandlerInstantiator(HandlerInstantiator hi)
HandlerInstantiator
to use for creating
instances of handlers (such as serializers, deserializers, type and type
id resolvers), given a class.
hi
- Instantiator to use; if null, use the default implementation
public ObjectMapper setInjectableValues(InjectableValues injectableValues)
InjectableValues
which used to find
values to inject.
public ObjectMapper setLocale(java.util.Locale l)
Locale.getDefault()
.
public ObjectMapper setTimeZone(java.util.TimeZone tz)
public boolean isEnabled(SerializationFeature f)
public ObjectMapper configure(SerializationFeature f, boolean state)
public ObjectMapper enable(SerializationFeature f)
DeserializationConfig
feature.
Modifies and returns this instance; no new object is created.
public ObjectMapper enable(SerializationFeature first, SerializationFeature... f)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper disable(SerializationFeature f)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper disable(SerializationFeature first, SerializationFeature... f)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public boolean isEnabled(DeserializationFeature f)
public ObjectMapper configure(DeserializationFeature f, boolean state)
public ObjectMapper enable(DeserializationFeature feature)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper enable(DeserializationFeature first, DeserializationFeature... f)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper disable(DeserializationFeature feature)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper disable(DeserializationFeature first, DeserializationFeature... f)
DeserializationConfig
features.
Modifies and returns this instance; no new object is created.
public ObjectMapper configure(JsonParser.Feature f, boolean state)
JsonParser.Feature
s
for parser instances this object mapper creates.
Note that this is equivalent to directly calling same method
on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectReader
s as well -- to avoid
this, use
ObjectReader.with(JsonParser.Feature)
instead.
public ObjectMapper enable(JsonParser.Feature... features)
JsonParser.Feature
s
for parser instances this object mapper creates.
Note that this is equivalent to directly calling same method on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectReader
s as well -- to avoid
this, use
ObjectReader.with(JsonParser.Feature)
instead.
public ObjectMapper disable(JsonParser.Feature... features)
JsonParser.Feature
s
for parser instances this object mapper creates.
Note that this is equivalent to directly calling same method on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectReader
s as well -- to avoid
this, use
ObjectReader.without(JsonParser.Feature)
instead.
public ObjectMapper configure(JsonGenerator.Feature f, boolean state)
JsonGenerator
feature for
generator instances this object mapper creates.
Note that this is equivalent to directly calling same method
on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectWriter
s as well -- to avoid
this, use
ObjectWriter.with(JsonGenerator.Feature)
instead.
public ObjectMapper enable(JsonGenerator.Feature... features)
JsonGenerator.Feature
s
for parser instances this object mapper creates.
Note that this is equivalent to directly calling same method on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectWriter
s as well -- to avoid
this, use
ObjectWriter.with(JsonGenerator.Feature)
instead.
public ObjectMapper disable(JsonGenerator.Feature... features)
JsonGenerator.Feature
s
for parser instances this object mapper creates.
Note that this is equivalent to directly calling same method on
getFactory()
.
WARNING: since this method directly modifies state of underlying
JsonFactory
,
it will change observed configuration by
ObjectWriter
s as well -- to avoid
this, use
ObjectWriter.without(JsonGenerator.Feature)
instead.
public boolean isEnabled(JsonFactory.Feature f)
public <T> T readValue(JsonParser p, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
Boolean
).
Note: this method should NOT be used if the result type is a
container (
Collection
or
Map
.
The reason is that due to type erasure, key and value types
cannot be introspected when using this method.
readValue
in class
ObjectCodec
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public <T> T readValue(JsonParser p, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
readValue
in class
ObjectCodec
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public final <T> T readValue(JsonParser p, ResolvedType valueType) throws java.io.IOException, JsonParseException, JsonMappingException
TypeFactory
.
readValue
in class
ObjectCodec
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
readValue(JsonParser, Class)
.
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public <T extends TreeNode> T readTree(JsonParser p) throws java.io.IOException, JsonProcessingException
JsonNode
.
Returns
JsonNode
that represents the root of the resulting tree, if there
was content to read, or
null
if no more content is accessible
via passed
JsonParser
.
NOTE! Behavior with end-of-input (no more content) differs between this
readTree
method, and all other methods that take input source: latter
will return "missing node", NOT
null
readTree
in class
ObjectCodec
JsonNode
, if valid JSON content found; null
if input has no content to bind -- note, however, that if
JSON
null
token is found, it will be represented
as a non-null
JsonNode
(one that returns
true
for
JsonNode.isNull()
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonProcessingException
public <T> MappingIterator<T> readValues(JsonParser p, ResolvedType valueType) throws java.io.IOException, JsonProcessingException
JsonParser
MUST
point to the first token of
the first element, OR not point to any token (in which case it is advanced
to the next token). This means, specifically, that for wrapped sequences,
parser MUST NOT point to the surrounding
START_ARRAY
(one that
contains values to read) but rather to the token following it which is the first
token of the first value to read.
Note that
ObjectReader
has more complete set of variants.
readValues
in class
ObjectCodec
java.io.IOException
JsonProcessingException
public <T> MappingIterator<T> readValues(JsonParser p, JavaType valueType) throws java.io.IOException, JsonProcessingException
readValues(JsonParser, ResolvedType)
.
java.io.IOException
JsonProcessingException
public <T> MappingIterator<T> readValues(JsonParser p, java.lang.Class<T> valueType) throws java.io.IOException, JsonProcessingException
readValues(JsonParser, ResolvedType)
.
readValues
in class
ObjectCodec
java.io.IOException
JsonProcessingException
public <T> MappingIterator<T> readValues(JsonParser p, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonProcessingException
readValues
in class
ObjectCodec
java.io.IOException
JsonProcessingException
public JsonNode readTree(java.io.InputStream in) throws java.io.IOException
JsonNode
instances.
Returns root of the resulting tree (where root can consist
of just a single node if the current event is a
value event, not container).
If a low-level I/O problem (missing input, network error) occurs,
a
IOException
will be thrown.
If a parsing problem occurs (invalid JSON),
JsonParseException
will be thrown.
If no content is found from input (end-of-input), Java
null
will be returned.
in
- Input stream used to read JSON content
for building the JSON tree.
JsonNode
, if valid JSON content found; null
if input has no content to bind -- note, however, that if
JSON
null
token is found, it will be represented
as a non-null
JsonNode
(one that returns
true
for
JsonNode.isNull()
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
java.io.IOException
public JsonNode readTree(java.io.Reader r) throws java.io.IOException
readTree(InputStream)
except content accessed through
passed-in
Reader
java.io.IOException
public JsonNode readTree(java.lang.String content) throws JsonProcessingException, JsonMappingException
readTree(InputStream)
except content read from
passed-in
String
JsonProcessingException
JsonMappingException
public JsonNode readTree(byte[] content) throws java.io.IOException
readTree(InputStream)
except content read from
passed-in byte array.
java.io.IOException
readTree(InputStream)
except content read from
passed-in byte array.
java.io.IOException
public JsonNode readTree(java.io.File file) throws java.io.IOException, JsonProcessingException
readTree(InputStream)
except content read from
passed-in
File
.
java.io.IOException
JsonProcessingException
public JsonNode readTree(java.net.URL source) throws java.io.IOException
readTree(InputStream)
except content read from
passed-in
URL
.
NOTE: handling of
URL
is delegated to
JsonFactory.createParser(java.net.URL)
and usually simply
calls
URL.openStream()
, meaning no special handling
is done. If different HTTP connection options are needed you will need
to create
InputStream
separately.
java.io.IOException
public void writeValue(JsonGenerator g, java.lang.Object value) throws java.io.IOException, JsonGenerationException, JsonMappingException
JsonGenerator
.
writeValue
in class
ObjectCodec
java.io.IOException
JsonGenerationException
JsonMappingException
java.io.IOException
JsonProcessingException
public ObjectNode createObjectNode()Note: return type is co-variant, as basic ObjectCodec abstraction cannot refer to concrete node types (as it's part of core package, whereas impls are part of mapper package)
createObjectNode
in class
ObjectCodec
public ArrayNode createArrayNode()Note: return type is co-variant, as basic ObjectCodec abstraction cannot refer to concrete node types (as it's part of core package, whereas impls are part of mapper package)
createArrayNode
in class
ObjectCodec
public JsonParser treeAsTokens(TreeNode n)
JsonParser
out of JSON tree
representation.
treeAsTokens
in class
ObjectCodec
n
- Root node of the tree that resulting parser will read from
public <T> T treeToValue(TreeNode n, java.lang.Class<T> valueType) throws JsonProcessingException
treeToValue
in class
ObjectCodec
JsonProcessingException
public <T extends JsonNode> T valueToTree(java.lang.Object fromValue) throws java.lang.IllegalArgumentException
treeToValue(com.fasterxml.jackson.core.TreeNode, java.lang.Class<T>)
; given a value (usually bean), will
construct equivalent JSON Tree representation. Functionally similar
to serializing value into JSON and parsing JSON as tree, but
more efficient.
NOTE: while results are usually identical to that of serialization followed
by deserialization, this is not always the case. In some cases serialization
into intermediate representation will retain encapsulation of things like
raw value (
RawValue
) or basic
node identity (
JsonNode
). If so, result is a valid tree, but values
are not re-constructed through actual JSON representation. So if transformation
requires actual materialization of JSON (or other data format that this mapper
produces), it will be necessary to do actual serialization.
T
- Actual node type; usually either basic
JsonNode
or
ObjectNode
fromValue
- Bean value to convert
null
value,
node for which
JsonNode.isNull()
returns
true
.
java.lang.IllegalArgumentException
public boolean canSerialize(java.lang.Class<?> type)
Exception
,
call
canSerialize(Class, AtomicReference)
instead.
public boolean canSerialize(java.lang.Class<?> type, java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
canSerialize(Class)
but that can return
actual
Throwable
that was thrown when trying to construct
serializer: this may be useful in figuring out what the actual problem is.
public boolean canDeserialize(JavaType type)
Exception
being thrown, false
is returned.
NOTE
: in case an exception is thrown during course of trying
co construct matching deserializer, it will be effectively swallowed.
If you want access to that exception, call
canDeserialize(JavaType, AtomicReference)
instead.
public boolean canDeserialize(JavaType type, java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
canDeserialize(JavaType)
but that can return
actual
Throwable
that was thrown when trying to construct
serializer: this may be useful in figuring out what the actual problem is.
public <T> T readValue(java.io.File src, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public <T> T readValue(java.io.File src, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public <T> T readValue(java.net.URL src, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
URL
is delegated to
JsonFactory.createParser(java.net.URL)
and usually simply
calls
URL.openStream()
, meaning no special handling
is done. If different HTTP connection options are needed you will need
to create
InputStream
separately.
java.io.IOException
- if a low-level I/O problem (unexpected end-of-input,
network error) occurs (passed through as-is without additional wrapping -- note
that this is one case where
DeserializationFeature.WRAP_EXCEPTIONS
does NOT result in wrapping of exception even if enabled)
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
public <T> T readValue(java.net.URL src, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
readValue(java.net.URL, Class)
except that target specified by
TypeReference
.
java.io.IOException
JsonParseException
JsonMappingException
readValue(java.net.URL, Class)
except that target specified by
JavaType
.
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.lang.String content, java.lang.Class<T> valueType) throws JsonProcessingException, JsonMappingException
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
JsonProcessingException
public <T> T readValue(java.lang.String content, TypeReference<T> valueTypeRef) throws JsonProcessingException, JsonMappingException
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
JsonProcessingException
public <T> T readValue(java.lang.String content, JavaType valueType) throws JsonProcessingException, JsonMappingException
JsonParseException
- if underlying input contains invalid content
of type
JsonParser
supports (JSON for default case)
JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)
JsonProcessingException
public <T> T readValue(java.io.Reader src, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.Reader src, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.Reader src, JavaType valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.InputStream src, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.InputStream src, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.InputStream src, JavaType valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(byte[] src, java.lang.Class<T> valueType) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(byte[] src, TypeReference<T> valueTypeRef) throws java.io.IOException, JsonParseException, JsonMappingException
java.io.IOException
JsonParseException
JsonMappingException
public <T> T readValue(java.io.DataInput src, java.lang.Class<T> valueType) throws java.io.IOException
java.io.IOException
public <T> T readValue(java.io.DataInput src, JavaType valueType) throws java.io.IOException
java.io.IOException
public void writeValue(java.io.File resultFile, java.lang.Object value) throws java.io.IOException, JsonGenerationException, JsonMappingException
java.io.IOException
JsonGenerationException
JsonMappingException
public void writeValue(java.io.OutputStream out, java.lang.Object value) throws java.io.IOException, JsonGenerationException, JsonMappingException
JsonEncoding.UTF8
).
Note: method does not close the underlying stream explicitly
here; however,
JsonFactory
this mapper uses may choose
to close the stream depending on its settings (by default,
it will try to close it when
JsonGenerator
we construct
is closed).
java.io.IOException
JsonGenerationException
JsonMappingException
public void writeValue(java.io.DataOutput out, java.lang.Object value) throws java.io.IOException
java.io.IOException
public void writeValue(java.io.Writer w, java.lang.Object value) throws java.io.IOException, JsonGenerationException, JsonMappingException
JsonFactory
this mapper uses may choose
to close the stream depending on its settings (by default,
it will try to close it when
JsonGenerator
we construct
is closed).
java.io.IOException
JsonGenerationException
JsonMappingException
public java.lang.String writeValueAsString(java.lang.Object value) throws JsonProcessingException
writeValue(Writer,Object)
with
StringWriter
and constructing String, but more efficient.
Note: prior to version 2.1, throws clause included
IOException
; 2.1 removed it.
JsonProcessingException
public byte[] writeValueAsBytes(java.lang.Object value) throws JsonProcessingException
writeValue(Writer,Object)
with
ByteArrayOutputStream
and getting bytes, but more efficient.
Encoding used will be UTF-8.
Note: prior to version 2.1, throws clause included
IOException
; 2.1 removed it.
JsonProcessingException
public ObjectWriter writer(SerializationFeature feature)
ObjectWriter
with
specified feature enabled (compared to settings that this
mapper instance has).
public ObjectWriter writer(SerializationFeature first, SerializationFeature... other)
ObjectWriter
with
specified features enabled (compared to settings that this
mapper instance has).
public ObjectWriter writer(java.text.DateFormat df)
ObjectWriter
that will
serialize objects using specified
DateFormat
; or, if
null passed, using timestamp (64-bit number.
public ObjectWriter writerWithView(java.lang.Class<?> serializationView)
ObjectWriter
that will
serialize objects using specified JSON View (filter).
public ObjectWriter writerFor(java.lang.Class<?> rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value. Type must be a super-type of runtime type.
Main reason for using this method is performance, as writer is able
to pre-fetch serializer to use before write, and if writer is used
more than once this avoids addition per-value serializer lookups.
public ObjectWriter writerFor(TypeReference<?> rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value. Type must be a super-type of runtime type.
Main reason for using this method is performance, as writer is able
to pre-fetch serializer to use before write, and if writer is used
more than once this avoids addition per-value serializer lookups.
public ObjectWriter writerFor(JavaType rootType)
ObjectWriter
that will
serialize objects using specified root type, instead of actual
runtime type of value. Type must be a super-type of runtime type.
Main reason for using this method is performance, as writer is able
to pre-fetch serializer to use before write, and if writer is used
more than once this avoids addition per-value serializer lookups.
public ObjectWriter writer(PrettyPrinter pp)
ObjectWriter
that will
serialize objects using specified pretty printer for indentation
(or if null, no pretty printer)
public ObjectWriter writerWithDefaultPrettyPrinter()
ObjectWriter
that will
serialize objects using the default pretty printer for indentation
public ObjectWriter writer(FilterProvider filterProvider)
ObjectWriter
that will
serialize objects using specified filter provider.
public ObjectWriter writer(FormatSchema schema)
ObjectWriter
that will
pass specific schema object to
JsonGenerator
used for
writing content.
schema
- Schema to pass to generator
public ObjectWriter writer(Base64Variant defaultBase64)
ObjectWriter
that will
use specified Base64 encoding variant for Base64-encoded binary data.
public ObjectWriter writer(CharacterEscapes escapes)
ObjectReader
that will
use specified character escaping details for output.
public ObjectWriter writer(ContextAttributes attrs)
ObjectWriter
that will
use specified default attributes.
@Deprecated public ObjectWriter writerWithType(java.lang.Class<?> rootType)
writerFor(Class)
instead
@Deprecated public ObjectWriter writerWithType(TypeReference<?> rootType)
writerFor(TypeReference)
instead
public ObjectReader reader()
ObjectReader
with
default settings. Note that the resulting instance is NOT usable as is,
without defining expected value type.
public ObjectReader reader(DeserializationFeature feature)
ObjectReader
with
specified feature enabled (compared to settings that this
mapper instance has).
Note that the resulting instance is NOT usable as is,
without defining expected value type.
public ObjectReader reader(DeserializationFeature first, DeserializationFeature... other)
ObjectReader
with
specified features enabled (compared to settings that this
mapper instance has).
Note that the resulting instance is NOT usable as is,
without defining expected value type.
public ObjectReader readerForUpdating(java.lang.Object valueToUpdate)
ObjectReader
that will
update given Object (usually Bean, but can be a Collection or Map
as well, but NOT an array) with JSON data. Deserialization occurs
normally except that the root-level value in JSON is not used for
instantiating a new object; instead give updateable object is used
as root.
Runtime type of value object is used for locating deserializer,
unless overridden by other factory methods of
ObjectReader
public ObjectReader readerFor(JavaType type)
ObjectReader
that will
read or update instances of specified type
public ObjectReader readerFor(java.lang.Class<?> type)
ObjectReader
that will
read or update instances of specified type
public ObjectReader readerFor(TypeReference<?> type)
ObjectReader
that will
read or update instances of specified type
public ObjectReader reader(JsonNodeFactory f)
ObjectReader
that will
use specified
JsonNodeFactory
for constructing JSON trees.
public ObjectReader reader(FormatSchema schema)
ObjectReader
that will
pass specific schema object to
JsonParser
used for
reading content.
schema
- Schema to pass to parser
public ObjectReader reader(InjectableValues injectableValues)
ObjectReader
that will
use specified injectable values.
injectableValues
- Injectable values to use
public ObjectReader readerWithView(java.lang.Class<?> view)
ObjectReader
that will
deserialize objects using specified JSON View (filter).
public ObjectReader reader(Base64Variant defaultBase64)
ObjectReader
that will
use specified Base64 encoding variant for Base64-encoded binary data.
public ObjectReader reader(ContextAttributes attrs)
ObjectReader
that will
use specified default attributes.
public <T> T convertValue(java.lang.Object fromValue, java.lang.Class<T> toValueType) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- If conversion fails due to incompatible type;
if so, root cause will contain underlying checked exception data binding
functionality threw
public <T> T convertValue(java.lang.Object fromValue, TypeReference<T> toValueTypeRef) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public <T> T convertValue(java.lang.Object fromValue, JavaType toValueType) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public <T> T updateValue(T valueToUpdate, java.lang.Object overrides) throws JsonMappingException
convertValue(Object, JavaType)
but one
in which
Implementation is approximately as follows:
TokenBuffer
ObjectReader
with `valueToUpdate` (using
readerForUpdating(Object)
)
JsonParser
(using
TokenBuffer.asParser()
)
ObjectReader.readValue(JsonParser)
.
JsonMerge
) as well as using "config overrides" (see
configOverride(Class)
and
setDefaultMergeable(Boolean)
).
valueToUpdate
- Object to update
overrides
- Object to conceptually serialize and merge into value to
update; can be thought of as a provider for overrides to apply.
JsonMappingException
- if there are structural incompatibilities that prevent update.
@Deprecated public JsonSchema generateJsonSchema(java.lang.Class<?> t) throws JsonMappingException
acceptJsonFormatVisitor(JavaType, JsonFormatVisitorWrapper)
)
t
- The class to generate schema for
JsonMappingException
public void acceptJsonFormatVisitor(java.lang.Class<?> type, JsonFormatVisitorWrapper visitor) throws JsonMappingException
type
- Type to generate schema for (possibly with generic signature)
JsonMappingException
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor) throws JsonMappingException
Serializer
hierarchy and related properties
This method can be used for things like
generating
JSON Schema
instance for specified type.
type
- Type to generate schema for (possibly with generic signature)
JsonMappingException
![]() |
痴情的电池 · 中国共产党桃江历史(第一卷完整版) 1 年前 |