public class ObjectWriter extends Object implements Versioned, 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 | Field and Description |
---|---|
protected SerializationConfig |
_config
General serialization configuration settings
|
protected JsonFactory |
_generatorFactory
Factory used for constructing
JsonGenerator s |
protected ObjectWriter.GeneratorSettings |
_generatorSettings
Container for settings that need to be passed to
JsonGenerator
constructed for serializing values. |
protected ObjectWriter.Prefetch |
_prefetch
We may pre-fetch serializer if
#_rootType
is known, and if so, reuse it afterwards. |
protected SerializerFactory |
_serializerFactory |
protected DefaultSerializerProvider |
_serializerProvider |
protected static PrettyPrinter |
NULL_PRETTY_PRINTER
We need to keep track of explicit disabling of pretty printing;
easiest to do by a token value.
|
Modifier | Constructor and Description |
---|---|
protected |
ObjectWriter(ObjectMapper mapper,
SerializationConfig config)
Alternative constructor for initial instantiation by
ObjectMapper |
protected |
ObjectWriter(ObjectMapper mapper,
SerializationConfig config,
FormatSchema s)
Alternative constructor for initial instantiation by
ObjectMapper |
protected |
ObjectWriter(ObjectMapper mapper,
SerializationConfig config,
JavaType rootType,
PrettyPrinter pp)
Constructor used by
ObjectMapper for initial instantiation |
protected |
ObjectWriter(ObjectWriter base,
JsonFactory f) |
protected |
ObjectWriter(ObjectWriter base,
SerializationConfig config)
Copy constructor used for building variations.
|
protected |
ObjectWriter(ObjectWriter base,
SerializationConfig config,
ObjectWriter.GeneratorSettings genSettings,
ObjectWriter.Prefetch prefetch)
Copy constructor used for building variations.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_configAndWriteValue(JsonGenerator gen,
Object value)
Method called to configure the generator as necessary and then
call write functionality
|
protected JsonGenerator |
_configureGenerator(JsonGenerator gen)
Helper method called to set or override settings of passed-in
JsonGenerator |
protected void |
_configureJsonGenerator(JsonGenerator gen)
Deprecated.
Since 2.5 (to be removed from 2.6 or later)
|
protected ObjectWriter |
_new(ObjectWriter.GeneratorSettings genSettings,
ObjectWriter.Prefetch prefetch)
Overridable factory method called by various "withXxx()" methods.
|
protected ObjectWriter |
_new(ObjectWriter base,
JsonFactory f)
Overridable factory method called by various "withXxx()" methods
|
protected ObjectWriter |
_new(ObjectWriter base,
SerializationConfig config)
Overridable factory method called by various "withXxx()" methods
|
protected SequenceWriter |
_newSequenceWriter(boolean wrapInArray,
JsonGenerator gen,
boolean managedInput)
Overridable factory method called by
#createSequenceWriter(JsonGenerator)
method (and its various overrides), and initializes it as necessary. |
protected ObjectWriter.Prefetch |
_prefetchRootSerializer(SerializationConfig config,
JavaType valueType)
Method called to locate (root) serializer ahead of time, if permitted
by configuration.
|
protected DefaultSerializerProvider |
_serializerProvider(SerializationConfig config)
Overridable helper method used for constructing
SerializerProvider to use for serialization. |
protected void |
_verifySchemaType(FormatSchema schema) |
void |
acceptJsonFormatVisitor(JavaType type,
JsonFormatVisitorWrapper visitor)
Method for visiting type hierarchy for given type, using specified visitor.
|
boolean |
canSerialize(Class<?> type) |
boolean |
canSerialize(Class<?> type,
AtomicReference<Throwable> cause)
Method for checking whether instances of given type can be serialized,
and optionally why (as per
Throwable returned). |
ObjectWriter |
forType(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) |
ContextAttributes |
getAttributes() |
SerializationConfig |
getConfig() |
JsonFactory |
getFactory() |
JsonFactory |
getJsonFactory()
Deprecated.
Since 2.2, use
getFactory() instead. |
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(JsonParser.Feature f) |
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(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(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(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(TimeZone tz) |
ObjectWriter |
withAttribute(Object key,
Object value) |
ObjectWriter |
withAttributes(Map<Object,Object> attrs) |
ObjectWriter |
withDefaultPrettyPrinter()
Method that will construct a new instance that will use the default
pretty printer for serialization.
|
ObjectWriter |
withFeatures(JsonGenerator.Feature... features) |
ObjectWriter |
withFeatures(SerializationFeature... features)
Method for constructing a new instance that is configured
with specified features enabled.
|
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(Object key) |
ObjectWriter |
withoutFeatures(JsonGenerator.Feature... features) |
ObjectWriter |
withoutFeatures(SerializationFeature... features)
Method for constructing a new instance that is configured
with specified features enabled.
|
ObjectWriter |
withRootName(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(String sep) |
ObjectWriter |
withSchema(FormatSchema schema)
Deprecated.
Since 2.5 use
with(FormatSchema) instead |
ObjectWriter |
withType(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(Class<?> view)
Method that will construct a new instance that uses specified
serialization view for serialization (with null basically disables
view processing)
|
void |
writeValue(File resultFile,
Object value)
Method that can be used to serialize any Java value as
JSON output, written to File provided.
|
void |
writeValue(JsonGenerator gen,
Object value)
Method that can be used to serialize any Java value as
JSON output, using provided
JsonGenerator . |
void |
writeValue(OutputStream out,
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(Writer w,
Object value)
Method that can be used to serialize any Java value as
JSON output, using Writer provided.
|
byte[] |
writeValueAsBytes(Object value)
Method that can be used to serialize any Java value as
a byte array.
|
String |
writeValueAsString(Object value)
Method that can be used to serialize any Java value as
a String.
|
SequenceWriter |
writeValues(File out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(JsonGenerator gen)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(OutputStream out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValues(Writer out)
Method for creating a
SequenceWriter to write a sequence of root
values using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(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(OutputStream out)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
SequenceWriter |
writeValuesAsArray(Writer out)
Method for creating a
SequenceWriter to write an array of
root-level values, using configuration of this ObjectWriter . |
protected static final PrettyPrinter NULL_PRETTY_PRINTER
protected final SerializationConfig _config
protected final DefaultSerializerProvider _serializerProvider
protected final SerializerFactory _serializerFactory
protected final JsonFactory _generatorFactory
JsonGenerator
sprotected final ObjectWriter.GeneratorSettings _generatorSettings
JsonGenerator
constructed for serializing values.protected final ObjectWriter.Prefetch _prefetch
#_rootType
is known, and if so, reuse it afterwards.
This allows avoiding further serializer lookups and increases
performance a bit on cases where readers are reused.protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp)
ObjectMapper
for initial instantiationprotected ObjectWriter(ObjectMapper mapper, SerializationConfig config)
ObjectMapper
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, FormatSchema s)
ObjectMapper
protected ObjectWriter(ObjectWriter base, SerializationConfig config, ObjectWriter.GeneratorSettings genSettings, ObjectWriter.Prefetch prefetch)
protected ObjectWriter(ObjectWriter base, SerializationConfig config)
protected ObjectWriter(ObjectWriter base, JsonFactory f)
public Version version()
protected ObjectWriter _new(ObjectWriter base, JsonFactory f)
protected ObjectWriter _new(ObjectWriter base, SerializationConfig config)
protected ObjectWriter _new(ObjectWriter.GeneratorSettings genSettings, ObjectWriter.Prefetch prefetch)
protected SequenceWriter _newSequenceWriter(boolean wrapInArray, JsonGenerator gen, boolean managedInput) throws IOException
#createSequenceWriter(JsonGenerator)
method (and its various overrides), and initializes it as necessary.IOException
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(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(String rootName)
SerializationConfig.withRootName(String)
for details.
Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
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 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(Class<?> rootType)
public ObjectWriter forType(TypeReference<?> rootType)
@Deprecated public ObjectWriter withType(JavaType rootType)
forType(JavaType)
instead@Deprecated public ObjectWriter withType(Class<?> rootType)
forType(Class)
instead@Deprecated public ObjectWriter withType(TypeReference<?> rootType)
forType(TypeReference)
insteadpublic ObjectWriter withView(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(Locale l)
public ObjectWriter with(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(Map<Object,Object> attrs)
public ObjectWriter withAttribute(Object key, Object value)
public ObjectWriter withoutAttribute(Object key)
public ObjectWriter withRootValueSeparator(String sep)
public ObjectWriter withRootValueSeparator(SerializableString sep)
public SequenceWriter writeValues(File out) throws 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.IOException
public SequenceWriter writeValues(JsonGenerator gen) throws 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.gen
- Low-level generator caller has already constructed that will
be used for actual writing of token stream.IOException
public SequenceWriter writeValues(Writer out) throws 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 streamIOException
public SequenceWriter writeValues(OutputStream out) throws 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 streamIOException
public SequenceWriter writeValuesAsArray(File out) throws 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 toIOException
public SequenceWriter writeValuesAsArray(JsonGenerator gen) throws 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 streamIOException
public SequenceWriter writeValuesAsArray(Writer out) throws 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 streamIOException
public SequenceWriter writeValuesAsArray(OutputStream out) throws 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 streamIOException
public boolean isEnabled(SerializationFeature f)
public boolean isEnabled(MapperFeature f)
public boolean isEnabled(JsonParser.Feature f)
public SerializationConfig getConfig()
@Deprecated public JsonFactory getJsonFactory()
getFactory()
instead.public JsonFactory getFactory()
public TypeFactory getTypeFactory()
public boolean hasPrefetchedSerializer()
public ContextAttributes getAttributes()
public void writeValue(JsonGenerator gen, Object value) throws IOException, JsonGenerationException, JsonMappingException
JsonGenerator
.public void writeValue(File resultFile, Object value) throws IOException, JsonGenerationException, JsonMappingException
public void writeValue(OutputStream out, Object value) throws 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).
public void writeValue(Writer w, Object value) throws 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).
public String writeValueAsString(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(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 boolean canSerialize(Class<?> type)
public boolean canSerialize(Class<?> type, AtomicReference<Throwable> cause)
Throwable
returned).protected DefaultSerializerProvider _serializerProvider(SerializationConfig config)
SerializerProvider
to use for serialization.protected void _verifySchemaType(FormatSchema schema)
protected final void _configAndWriteValue(JsonGenerator gen, Object value) throws IOException
IOException
protected ObjectWriter.Prefetch _prefetchRootSerializer(SerializationConfig config, JavaType valueType)
@Deprecated protected void _configureJsonGenerator(JsonGenerator gen)
JsonGenerator
protected JsonGenerator _configureGenerator(JsonGenerator gen)
JsonGenerator
Copyright © 2016 JBoss by Red Hat. All rights reserved.