public class ObjectWriter extends java.lang.Object implements Versioned, java.io.Serializable
ObjectMapper
and can be
reused in completely thread-safe manner with no explicit synchronizationModifier and Type | Class and Description |
---|---|
static class |
ObjectWriter.GeneratorSettings
Helper class used for containing settings specifically related
to (re)configuring
JsonGenerator constructed for
writing output. |
static class |
ObjectWriter.Prefetch
As a minor optimization, we will make an effort to pre-fetch a serializer,
or at least relevant
TypeSerializer , if given enough
information. |
Modifier and Type | Method and Description |
---|---|
void |
acceptJsonFormatVisitor(java.lang.Class<?> type,
JsonFormatVisitorWrapper visitor)
Since 2.6
|
void |
acceptJsonFormatVisitor(JavaType type,
JsonFormatVisitorWrapper visitor)
Method for visiting type hierarchy for given type, using specified visitor.
|
boolean |
canSerialize(java.lang.Class<?> type) |
boolean |
canSerialize(java.lang.Class<?> type,
java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
Method for checking whether instances of given type can be serialized,
and optionally why (as per
Throwable returned). |
ObjectWriter |
forType(java.lang.Class<?> rootType)
Method that will construct a new instance that uses specific type
as the root type for serialization, instead of runtime dynamic
type of the root object itself.
|
ObjectWriter |
forType(JavaType rootType)
Method that will construct a new instance that uses specific type
as the root type for serialization, instead of runtime dynamic
type of the root object itself.
|
ObjectWriter |
forType(TypeReference<?> rootType)
Method that will construct a new instance that uses specific type
as the root type for serialization, instead of runtime dynamic
type of the root object itself.
|
ContextAttributes |
getAttributes() |
SerializationConfig |
getConfig() |
JsonFactory |
getFactory() |
TypeFactory |
getTypeFactory() |
boolean |
hasPrefetchedSerializer()
Diagnostics method that can be called to check whether this writer
has pre-fetched serializer to use: pre-fetching improves performance
when writer instances are reused as it avoids a per-call serializer
lookup.
|
boolean |
isEnabled(JsonGenerator.Feature f) |
boolean |
isEnabled(JsonParser.Feature f)
Deprecated.
|
boolean |
isEnabled(MapperFeature f) |
boolean |
isEnabled(SerializationFeature f) |
Version |
version()
Method that will return version information stored in and read from jar
that contains this class.
|
ObjectWriter |
with(Base64Variant b64variant)
Method that will construct a new instance that uses specified default
Base64Variant for base64 encoding |
ObjectWriter |
with(CharacterEscapes escapes) |
ObjectWriter |
with(ContextAttributes attrs) |
ObjectWriter |
with(java.text.DateFormat df)
Fluent factory method that will construct a new writer instance that will
use specified date format for serializing dates; or if null passed, one
that will serialize dates as numeric timestamps.
|
ObjectWriter |
with(FilterProvider filterProvider)
Method that will construct a new instance that uses specified
provider for resolving filter instances by id.
|
ObjectWriter |
with(FormatFeature feature) |
ObjectWriter |
with(FormatSchema schema)
Method that will construct a new instance that uses specific format schema
for serialization.
|
ObjectWriter |
with(JsonFactory f) |
ObjectWriter |
with(JsonGenerator.Feature feature) |
ObjectWriter |
with(java.util.Locale l) |
ObjectWriter |
with(PrettyPrinter pp)
Method that will construct a new instance that will use specified pretty
printer (or, if null, will not do any pretty-printing)
|
ObjectWriter |
with(SerializationFeature feature)
Method for constructing a new instance that is configured
with specified feature enabled.
|
ObjectWriter |
with(SerializationFeature first,
SerializationFeature... other)
Method for constructing a new instance that is configured
with specified features enabled.
|
ObjectWriter |
with(java.util.TimeZone tz) |
ObjectWriter |
withAttribute(java.lang.Object key,
java.lang.Object value) |
ObjectWriter |
withAttributes(java.util.Map<?,?> attrs)
Mutant factory method that allows construction of a new writer instance
that uses specified set of default attribute values.
|
ObjectWriter |
withDefaultPrettyPrinter()
Method that will construct a new instance that will use the default
pretty printer for serialization.
|
ObjectWriter |
withFeatures(FormatFeature... features) |
ObjectWriter |
withFeatures(JsonGenerator.Feature... features) |
ObjectWriter |
withFeatures(SerializationFeature... features)
Method for constructing a new instance that is configured
with specified features enabled.
|
ObjectWriter |
without(FormatFeature feature) |
ObjectWriter |
without(JsonGenerator.Feature feature) |
ObjectWriter |
without(SerializationFeature feature)
Method for constructing a new instance that is configured
with specified feature enabled.
|
ObjectWriter |
without(SerializationFeature first,
SerializationFeature... other)
Method for constructing a new instance that is configured
with specified features enabled.
|
ObjectWriter |
withoutAttribute(java.lang.Object key) |
ObjectWriter |
withoutFeatures(FormatFeature... features) |
ObjectWriter |
withoutFeatures(JsonGenerator.Feature... features) |
ObjectWriter |
withoutFeatures(SerializationFeature... features)
Method for constructing a new instance that is configured
with specified features enabled.
|
ObjectWriter |
withoutRootName()
Convenience method that is same as calling:
withRootName("")
which will forcibly prevent use of root name wrapping when writing
values with this ObjectWriter . |
ObjectWriter |
withRootName(PropertyName rootName) |
ObjectWriter |
withRootName(java.lang.String rootName)
Method for constructing a new instance with configuration that
specifies what root name to use for "root element wrapping".
|
ObjectWriter |
withRootValueSeparator(SerializableString sep) |
ObjectWriter |
withRootValueSeparator(java.lang.String sep) |
ObjectWriter |
withSchema(FormatSchema schema)
Deprecated.
Since 2.5 use
with(FormatSchema) instead |
ObjectWriter |
withType(java.lang.Class<?> rootType)
Deprecated.
since 2.5 Use
forType(Class) instead |
ObjectWriter |
withType(JavaType rootType)
Deprecated.
since 2.5 Use
forType(JavaType) instead |
ObjectWriter |
withType(TypeReference<?> rootType)
Deprecated.
since 2.5 Use
forType(TypeReference) instead |
ObjectWriter |
withView(java.lang.Class<?> view)
Method that will construct a new instance that uses specified
serialization view for serialization (with null basically disables
view processing)
|
void |
writeValue(java.io.DataOutput out,
java.lang.Object value) |
void |
writeValue(java.io.File resultFile,
java.lang.Object value)
Method that can be used to serialize any Java value as
JSON output, written to File provided.
|
void |
writeValue(JsonGenerator g,
java.lang.Object value)
Method that can be used to serialize any Java value as
JSON output, using provided
JsonGenerator . |
void |
writeValue(java.io.OutputStream out,
java.lang.Object value)
Method that can be used to serialize any Java value as
JSON output, using output stream provided (using encoding
JsonEncoding.UTF8 ). |
void |
writeValue(java.io.Writer w,
java.lang.Object value)
Method that can be used to serialize any Java value as
JSON output, using Writer provided.
|
byte[] |
writeValueAsBytes(java.lang.Object value)
Method that can be used to serialize any Java value as
a byte array.
|
java.lang.String |
writeValueAsString(java.lang.Object value)
Method that can be used to serialize any Java value as
a String.
|
SequenceWriter |
writeValues(java.io.DataOutput out) |
SequenceWriter |
writeValues(java.io.File out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(JsonGenerator g)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(java.io.OutputStream out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(java.io.Writer out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(java.io.DataOutput out) |
SequenceWriter |
writeValuesAsArray(java.io.File out)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(JsonGenerator gen)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(java.io.OutputStream out)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(java.io.Writer out)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
public Version version()
public ObjectWriter with(SerializationFeature feature)
public ObjectWriter with(SerializationFeature first, SerializationFeature... other)
public ObjectWriter withFeatures(SerializationFeature... features)
public ObjectWriter without(SerializationFeature feature)
public ObjectWriter without(SerializationFeature first, SerializationFeature... other)
public ObjectWriter withoutFeatures(SerializationFeature... features)
public ObjectWriter with(JsonGenerator.Feature feature)
public ObjectWriter withFeatures(JsonGenerator.Feature... features)
public ObjectWriter without(JsonGenerator.Feature feature)
public ObjectWriter withoutFeatures(JsonGenerator.Feature... features)
public ObjectWriter with(FormatFeature feature)
public ObjectWriter withFeatures(FormatFeature... features)
public ObjectWriter without(FormatFeature feature)
public ObjectWriter withoutFeatures(FormatFeature... features)
public ObjectWriter forType(JavaType rootType)
Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
public ObjectWriter forType(java.lang.Class<?> rootType)
public ObjectWriter forType(TypeReference<?> rootType)
@Deprecated public ObjectWriter withType(JavaType rootType)
forType(JavaType)
instead@Deprecated public ObjectWriter withType(java.lang.Class<?> rootType)
forType(Class)
instead@Deprecated public ObjectWriter withType(TypeReference<?> rootType)
forType(TypeReference)
insteadpublic ObjectWriter with(java.text.DateFormat df)
Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
public ObjectWriter withDefaultPrettyPrinter()
public ObjectWriter with(FilterProvider filterProvider)
public ObjectWriter with(PrettyPrinter pp)
public ObjectWriter withRootName(java.lang.String rootName)
MapperConfigBase.withRootName(String)
for details.
Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
rootName
- Root name to use, if non-empty; `null` for "use defaults",
and empty String ("") for "do NOT add root wrapper"public ObjectWriter withRootName(PropertyName rootName)
public ObjectWriter withoutRootName()
withRootName("")
which will forcibly prevent use of root name wrapping when writing
values with this ObjectWriter
.public ObjectWriter with(FormatSchema schema)
Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
@Deprecated public ObjectWriter withSchema(FormatSchema schema)
with(FormatSchema)
insteadpublic ObjectWriter withView(java.lang.Class<?> view)
Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
public ObjectWriter with(java.util.Locale l)
public ObjectWriter with(java.util.TimeZone tz)
public ObjectWriter with(Base64Variant b64variant)
Base64Variant
for base64 encodingpublic ObjectWriter with(CharacterEscapes escapes)
public ObjectWriter with(JsonFactory f)
public ObjectWriter with(ContextAttributes attrs)
public ObjectWriter withAttributes(java.util.Map<?,?> attrs)
public ObjectWriter withAttribute(java.lang.Object key, java.lang.Object value)
public ObjectWriter withoutAttribute(java.lang.Object key)
public ObjectWriter withRootValueSeparator(java.lang.String sep)
public ObjectWriter withRootValueSeparator(SerializableString sep)
public SequenceWriter writeValues(java.io.File out) throws java.io.IOException
SequenceWriter
to write a sequence of root
values using configuration of this ObjectWriter
.
Sequence is not surrounded by JSON array; some backend types may not
support writing of such sequences as root level.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.out
- Target file to write value sequence to.java.io.IOException
public SequenceWriter writeValues(JsonGenerator g) throws java.io.IOException
SequenceWriter
to write a sequence of root
values using configuration of this ObjectWriter
.
Sequence is not surrounded by JSON array; some backend types may not
support writing of such sequences as root level.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure that all content gets flushed by
the generator. However, since a JsonGenerator
is explicitly passed,
it will NOT be closed when SequenceWriter.close()
is called.g
- Low-level generator caller has already constructed that will
be used for actual writing of token stream.java.io.IOException
public SequenceWriter writeValues(java.io.Writer out) throws java.io.IOException
SequenceWriter
to write a sequence of root
values using configuration of this ObjectWriter
.
Sequence is not surrounded by JSON array; some backend types may not
support writing of such sequences as root level.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.out
- Target writer to use for writing the token streamjava.io.IOException
public SequenceWriter writeValues(java.io.OutputStream out) throws java.io.IOException
SequenceWriter
to write a sequence of root
values using configuration of this ObjectWriter
.
Sequence is not surrounded by JSON array; some backend types may not
support writing of such sequences as root level.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.out
- Physical output stream to use for writing the token streamjava.io.IOException
public SequenceWriter writeValues(java.io.DataOutput out) throws java.io.IOException
java.io.IOException
public SequenceWriter writeValuesAsArray(java.io.File out) throws java.io.IOException
SequenceWriter
to write an array of
root-level values, using configuration of this ObjectWriter
.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.
Note that the type to use with forType(Class)
needs to
be type of individual values (elements) to write and NOT matching array
or Collection
type.
out
- File to write token stream tojava.io.IOException
public SequenceWriter writeValuesAsArray(JsonGenerator gen) throws java.io.IOException
SequenceWriter
to write an array of
root-level values, using configuration of this ObjectWriter
.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure that all content gets flushed by
the generator. However, since a JsonGenerator
is explicitly passed,
it will NOT be closed when SequenceWriter.close()
is called.
Note that the type to use with forType(Class)
needs to
be type of individual values (elements) to write and NOT matching array
or Collection
type.
gen
- Underlying generator to use for writing the token streamjava.io.IOException
public SequenceWriter writeValuesAsArray(java.io.Writer out) throws java.io.IOException
SequenceWriter
to write an array of
root-level values, using configuration of this ObjectWriter
.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.
Note that the type to use with forType(Class)
needs to
be type of individual values (elements) to write and NOT matching array
or Collection
type.
out
- Writer to use for writing the token streamjava.io.IOException
public SequenceWriter writeValuesAsArray(java.io.OutputStream out) throws java.io.IOException
SequenceWriter
to write an array of
root-level values, using configuration of this ObjectWriter
.
Resulting writer needs to be SequenceWriter.close()
d after all
values have been written to ensure closing of underlying generator and
output stream.
Note that the type to use with forType(Class)
needs to
be type of individual values (elements) to write and NOT matching array
or Collection
type.
out
- Physical output stream to use for writing the token streamjava.io.IOException
public SequenceWriter writeValuesAsArray(java.io.DataOutput out) throws java.io.IOException
java.io.IOException
public boolean isEnabled(SerializationFeature f)
public boolean isEnabled(MapperFeature f)
@Deprecated public boolean isEnabled(JsonParser.Feature f)
public boolean isEnabled(JsonGenerator.Feature f)
public SerializationConfig getConfig()
public JsonFactory getFactory()
public TypeFactory getTypeFactory()
public boolean hasPrefetchedSerializer()
public ContextAttributes getAttributes()
public void writeValue(JsonGenerator g, java.lang.Object value) throws java.io.IOException
JsonGenerator
.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.Writer w, java.lang.Object value) throws java.io.IOException, JsonGenerationException, JsonMappingException
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 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 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
public void acceptJsonFormatVisitor(java.lang.Class<?> type, JsonFormatVisitorWrapper visitor) throws JsonMappingException
JsonMappingException
public boolean canSerialize(java.lang.Class<?> type)
public boolean canSerialize(java.lang.Class<?> type, java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> cause)
Throwable
returned).Copyright © 2010 - 2020 Adobe. All Rights Reserved