Class ObjectWriter
- All Implemented Interfaces:
Versioned
,Serializable
ObjectMapper
and can be
reused in completely thread-safe manner with no explicit synchronization- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Helper class used for containing settings specifically related to (re)configuringJsonGenerator
constructed for writing output.static final class
As a minor optimization, we will make an effort to pre-fetch a serializer, or at least relevantTypeSerializer
, if given enough information. -
Method Summary
Modifier and TypeMethodDescriptionvoid
acceptJsonFormatVisitor
(JavaType type, JsonFormatVisitorWrapper visitor) Method for visiting type hierarchy for given type, using specified visitor.void
acceptJsonFormatVisitor
(Class<?> type, JsonFormatVisitorWrapper visitor) Since 2.6boolean
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 perThrowable
returned).Factory method for constructing properly initializedJsonGenerator
to write content using specifiedDataOutput
.createGenerator
(File outputFile, JsonEncoding enc) Factory method for constructing properly initializedJsonGenerator
to write content to specifiedFile
, using specified encoding.Factory method for constructing properly initializedJsonGenerator
to write content using specifiedOutputStream
.createGenerator
(OutputStream out, JsonEncoding enc) Factory method for constructing properly initializedJsonGenerator
to write content using specifiedOutputStream
and encoding.Factory method for constructing properly initializedJsonGenerator
to write content using specifiedWriter
.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.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.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.boolean
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
boolean
Deprecated.boolean
boolean
boolean
boolean
version()
Method that will return version information stored in and read from jar that contains this class.with
(Base64Variant b64variant) Method that will construct a new instance that uses specified defaultBase64Variant
for base64 encodingwith
(FormatFeature feature) with
(FormatSchema schema) Method that will construct a new instance that uses specific format schema for serialization.with
(CharacterEscapes escapes) with
(JsonFactory f) with
(JsonGenerator.Feature feature) 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)with
(StreamWriteFeature feature) with
(ContextAttributes attrs) with
(DatatypeFeature feature) Method for constructing a new instance that is configured with specified feature enabled.with
(FilterProvider filterProvider) Method that will construct a new instance that uses specified provider for resolving filter instances by id.with
(SerializationFeature feature) Method for constructing a new instance that is configured with specified feature enabled.with
(SerializationFeature first, SerializationFeature... other) Method for constructing a new instance that is configured with specified features enabled.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.withAttribute
(Object key, Object value) withAttributes
(Map<?, ?> attrs) Mutant factory method that allows construction of a new writer instance that uses specified set of default attribute values.Method that will construct a new instance that will use the default pretty printer for serialization.withFeatures
(FormatFeature... features) withFeatures
(JsonGenerator.Feature... features) withFeatures
(DatatypeFeature... features) Method for constructing a new instance that is configured with specified features enabled.withFeatures
(SerializationFeature... features) Method for constructing a new instance that is configured with specified features enabled.without
(FormatFeature feature) without
(JsonGenerator.Feature feature) without
(StreamWriteFeature feature) without
(DatatypeFeature feature) Method for constructing a new instance that is configured with specified feature disabled.without
(SerializationFeature feature) Method for constructing a new instance that is configured with specified feature disabled.without
(SerializationFeature first, SerializationFeature... other) Method for constructing a new instance that is configured with specified features disabled.withoutAttribute
(Object key) withoutFeatures
(FormatFeature... features) withoutFeatures
(JsonGenerator.Feature... features) withoutFeatures
(DatatypeFeature... features) Method for constructing a new instance that is configured with specified features disabled.withoutFeatures
(SerializationFeature... features) Method for constructing a new instance that is configured with specified features disabled.Convenience method that is same as calling:withRootName("")
which will forcibly prevent use of root name wrapping when writing values with thisObjectWriter
.withRootName
(PropertyName rootName) withRootName
(String rootName) Method for constructing a new instance with configuration that specifies what root name to use for "root element wrapping".withSchema
(FormatSchema schema) Deprecated.withType
(TypeReference<?> rootType) Deprecated.since 2.5 UseforType(TypeReference)
insteadDeprecated.since 2.5 UseforType(JavaType)
insteadDeprecated.since 2.5 UseforType(Class)
insteadMethod that will construct a new instance that uses specified serialization view for serialization (with null basically disables view processing)void
writeValue
(JsonGenerator g, Object value) Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator
.void
writeValue
(DataOutput out, Object value) 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
(OutputStream out, Object value) Method that can be used to serialize any Java value as JSON output, using output stream provided (using encodingJsonEncoding.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.writeValueAsString
(Object value) Method that can be used to serialize any Java value as a String.Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
.writeValues
(DataOutput out) writeValues
(File out) Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
.writeValues
(OutputStream out) Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
.writeValues
(Writer out) Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
.Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
.writeValuesAsArray
(File out) Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
.Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
.writeValuesAsArray
(Writer out) Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
.
-
Method Details
-
version
Method that will return version information stored in and read from jar that contains this class. -
with
Method for constructing a new instance that is configured with specified feature enabled. -
with
Method for constructing a new instance that is configured with specified features enabled. -
withFeatures
Method for constructing a new instance that is configured with specified features enabled. -
without
Method for constructing a new instance that is configured with specified feature disabled. -
without
Method for constructing a new instance that is configured with specified features disabled. -
withoutFeatures
Method for constructing a new instance that is configured with specified features disabled. -
with
Method for constructing a new instance that is configured with specified feature enabled.- Since:
- 2.14
-
withFeatures
Method for constructing a new instance that is configured with specified features enabled.- Since:
- 2.14
-
without
Method for constructing a new instance that is configured with specified feature disabled.- Since:
- 2.14
-
withoutFeatures
Method for constructing a new instance that is configured with specified features disabled.- Since:
- 2.14
-
with
- Since:
- 2.5
-
withFeatures
- Since:
- 2.5
-
without
- Since:
- 2.5
-
withoutFeatures
- Since:
- 2.5
-
with
- Since:
- 2.11
-
without
- Since:
- 2.11
-
with
- Since:
- 2.7
-
withFeatures
- Since:
- 2.7
-
without
- Since:
- 2.7
-
withoutFeatures
- Since:
- 2.7
-
forType
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.Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
- Since:
- 2.5
-
forType
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.- Since:
- 2.5
-
forType
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.- Since:
- 2.5
-
withType
Deprecated.since 2.5 UseforType(JavaType)
instead -
withType
Deprecated.since 2.5 UseforType(Class)
instead -
withType
Deprecated.since 2.5 UseforType(TypeReference)
instead -
with
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.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withDefaultPrettyPrinter
Method that will construct a new instance that will use the default pretty printer for serialization. -
with
Method that will construct a new instance that uses specified provider for resolving filter instances by id. -
with
Method that will construct a new instance that will use specified pretty printer (or, if null, will not do any pretty-printing) -
withRootName
Method for constructing a new instance with configuration that specifies what root name to use for "root element wrapping". SeeMapperConfigBase.withRootName(String)
for details.Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
- Parameters:
rootName
- Root name to use, if non-empty; `null` for "use defaults", and empty String ("") for "do NOT add root wrapper"
-
withRootName
- Since:
- 2.6
-
withoutRootName
Convenience method that is same as calling:withRootName("")
which will forcibly prevent use of root name wrapping when writing values with thisObjectWriter
.- Since:
- 2.6
-
with
Method that will construct a new instance that uses specific format schema for serialization.Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withSchema
Deprecated.Since 2.5 usewith(FormatSchema)
instead -
withView
Method that will construct a new instance that uses specified serialization view for serialization (with null basically disables view processing)Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
with
-
with
-
with
Method that will construct a new instance that uses specified defaultBase64Variant
for base64 encoding- Since:
- 2.1
-
with
- Since:
- 2.3
-
with
- Since:
- 2.3
-
with
- Since:
- 2.3
-
withAttributes
Mutant factory method that allows construction of a new writer instance that uses specified set of default attribute values.- Since:
- 2.3
-
withAttribute
- Since:
- 2.3
-
withoutAttribute
- Since:
- 2.3
-
withRootValueSeparator
- Since:
- 2.5
-
withRootValueSeparator
- Since:
- 2.5
-
createGenerator
Factory method for constructing properly initializedJsonGenerator
to write content using specifiedOutputStream
. Generator is not managed (or "owned") by ObjectWriter: caller is responsible for properly closing it once content generation is complete.- Throws:
IOException
- Since:
- 2.11
-
createGenerator
Factory method for constructing properly initializedJsonGenerator
to write content using specifiedOutputStream
and encoding. Generator is not managed (or "owned") by ObjectWriter: caller is responsible for properly closing it once content generation is complete.- Throws:
IOException
- Since:
- 2.11
-
createGenerator
Factory method for constructing properly initializedJsonGenerator
to write content using specifiedWriter
. Generator is not managed (or "owned") by ObjectWriter: caller is responsible for properly closing it once content generation is complete.- Throws:
IOException
- Since:
- 2.11
-
createGenerator
Factory method for constructing properly initializedJsonGenerator
to write content to specifiedFile
, using specified encoding. Generator is not managed (or "owned") by ObjectWriter: caller is responsible for properly closing it once content generation is complete.- Throws:
IOException
- Since:
- 2.11
-
createGenerator
Factory method for constructing properly initializedJsonGenerator
to write content using specifiedDataOutput
. Generator is not managed (or "owned") by ObjectWriter: caller is responsible for properly closing it once content generation is complete.- Throws:
IOException
- Since:
- 2.11
-
writeValues
Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to beSequenceWriter.close()
d after all values have been written to ensure closing of underlying generator and output stream.- Parameters:
out
- Target file to write value sequence to.- Throws:
IOException
- Since:
- 2.5
-
writeValues
Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to beSequenceWriter.close()
d after all values have been written to ensure that all content gets flushed by the generator. However, since aJsonGenerator
is explicitly passed, it will NOT be closed whenSequenceWriter.close()
is called.- Parameters:
g
- Low-level generator caller has already constructed that will be used for actual writing of token stream.- Throws:
IOException
- Since:
- 2.5
-
writeValues
Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to beSequenceWriter.close()
d after all values have been written to ensure closing of underlying generator and output stream.- Parameters:
out
- Target writer to use for writing the token stream- Throws:
IOException
- Since:
- 2.5
-
writeValues
Method for creating aSequenceWriter
to write a sequence of root values using configuration of thisObjectWriter
. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to beSequenceWriter.close()
d after all values have been written to ensure closing of underlying generator and output stream.- Parameters:
out
- Physical output stream to use for writing the token stream- Throws:
IOException
- Since:
- 2.5
-
writeValues
- Throws:
IOException
- Since:
- 2.8
-
writeValuesAsArray
Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
. Resulting writer needs to beSequenceWriter.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 orCollection
type.- Parameters:
out
- File to write token stream to- Throws:
IOException
- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
. Resulting writer needs to beSequenceWriter.close()
d after all values have been written to ensure that all content gets flushed by the generator. However, since aJsonGenerator
is explicitly passed, it will NOT be closed whenSequenceWriter.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 orCollection
type.- Parameters:
gen
- Underlying generator to use for writing the token stream- Throws:
IOException
- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
. Resulting writer needs to beSequenceWriter.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 orCollection
type.- Parameters:
out
- Writer to use for writing the token stream- Throws:
IOException
- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriter
to write an array of root-level values, using configuration of thisObjectWriter
. Resulting writer needs to beSequenceWriter.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 orCollection
type.- Parameters:
out
- Physical output stream to use for writing the token stream- Throws:
IOException
- Since:
- 2.5
-
writeValuesAsArray
- Throws:
IOException
- Since:
- 2.8
-
isEnabled
-
isEnabled
-
isEnabled
- Since:
- 2.14
-
isEnabled
Deprecated.- Since:
- 2.9
-
isEnabled
- Since:
- 2.9
-
isEnabled
- Since:
- 2.11
-
getConfig
- Since:
- 2.2
-
getFactory
- Since:
- 2.2
-
getTypeFactory
-
hasPrefetchedSerializer
public 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.- Since:
- 2.2
-
getAttributes
- Since:
- 2.3
-
writeValue
Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator
.Note that the given
JsonGenerator
is not closed; caller is expected to handle that as necessary.- Throws:
IOException
-
writeValue
public void writeValue(File resultFile, Object value) throws IOException, StreamWriteException, DatabindException Method that can be used to serialize any Java value as JSON output, written to File provided. -
writeValue
public void writeValue(OutputStream out, Object value) throws IOException, StreamWriteException, DatabindException Method that can be used to serialize any Java value as JSON output, using output stream provided (using encodingJsonEncoding.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 whenJsonGenerator
we construct is closed). -
writeValue
public void writeValue(Writer w, Object value) throws IOException, StreamWriteException, DatabindException Method that can be used to serialize any Java value as JSON output, using Writer provided.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 whenJsonGenerator
we construct is closed). -
writeValue
public void writeValue(DataOutput out, Object value) throws IOException, StreamWriteException, DatabindException - Throws:
IOException
StreamWriteException
DatabindException
- Since:
- 2.8
-
writeValueAsString
Method that can be used to serialize any Java value as a String. Functionally equivalent to callingwriteValue(Writer,Object)
withStringWriter
and constructing String, but more efficient.Note: prior to version 2.1, throws clause included
IOException
; 2.1 removed it.- Throws:
JsonProcessingException
-
writeValueAsBytes
Method that can be used to serialize any Java value as a byte array. Functionally equivalent to callingwriteValue(Writer,Object)
withByteArrayOutputStream
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.- Throws:
JsonProcessingException
-
acceptJsonFormatVisitor
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor) throws JsonMappingException Method for visiting type hierarchy for given type, using specified visitor. Visitation usesSerializer
hierarchy and related propertiesThis method can be used for things like generating Json Schema instance for specified type.
- Parameters:
type
- Type to generate schema for (possibly with generic signature)- Throws:
JsonMappingException
- Since:
- 2.2
-
acceptJsonFormatVisitor
public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor) throws JsonMappingException Since 2.6- Throws:
JsonMappingException
-
canSerialize
-
canSerialize
Method for checking whether instances of given type can be serialized, and optionally why (as perThrowable
returned).- Since:
- 2.3
-
with(FormatSchema)
instead