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 classHelper class used for containing settings specifically related to (re)configuringJsonGeneratorconstructed for writing output.static final classAs a minor optimization, we will make an effort to pre-fetch a serializer, or at least relevantTypeSerializer, if given enough information. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SerializationConfigGeneral serialization configuration settingsprotected final JsonFactoryFactory used for constructingJsonGeneratorsprotected final ObjectWriter.GeneratorSettingsContainer for settings that need to be passed toJsonGeneratorconstructed for serializing values.protected final ObjectWriter.PrefetchWe may pre-fetch serializer if root type is known (has been explicitly declared), and if so, reuse it afterwards.protected final SerializerFactoryprotected final DefaultSerializerProviderprotected static final PrettyPrinterWe need to keep track of explicit disabling of pretty printing; easiest to do by a token value. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedObjectWriter(ObjectMapper mapper, SerializationConfig config) Alternative constructor for initial instantiation byObjectMapperprotectedObjectWriter(ObjectMapper mapper, SerializationConfig config, FormatSchema s) Alternative constructor for initial instantiation byObjectMapperprotectedObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp) Constructor used byObjectMapperfor initial instantiationprotectedObjectWriter(ObjectWriter base, JsonFactory f) protectedObjectWriter(ObjectWriter base, SerializationConfig config) Copy constructor used for building variations.protectedObjectWriter(ObjectWriter base, SerializationConfig config, ObjectWriter.GeneratorSettings genSettings, ObjectWriter.Prefetch prefetch) Copy constructor used for building variations. -
Method Summary
Modifier and TypeMethodDescriptionprotected final void_assertNotNull(String paramName, Object src) protected final JsonGeneratorHelper method called to set or override settings of passed-inJsonGeneratorprotected 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()" methodsprotected ObjectWriter_new(ObjectWriter base, SerializationConfig config) Overridable factory method called by various "withXxx()" methodsprotected SequenceWriter_newSequenceWriter(boolean wrapInArray, JsonGenerator gen, boolean managedInput) Overridable factory method called bywriteValues(OutputStream)method (and its various overrides), and initializes it as necessary.protected DefaultSerializerProviderOverridable helper method used for constructingSerializerProviderto use for serialization.protected void_verifySchemaType(FormatSchema schema) protected final void_writeValueAndClose(JsonGenerator gen, Object value) Method called to configure the generator as necessary and then call write functionalityvoidacceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor) Method for visiting type hierarchy for given type, using specified visitor.voidacceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor) Since 2.6booleancanSerialize(Class<?> type) Deprecated.Since 2.18 use discouraged; method to be removed from Jackson 3.0booleancanSerialize(Class<?> type, AtomicReference<Throwable> cause) Deprecated.Since 2.18 use discouraged; method to be removed from Jackson 3.0Factory method for constructing properly initializedJsonGeneratorto write content using specifiedDataOutput.createGenerator(File outputFile, JsonEncoding enc) Factory method for constructing properly initializedJsonGeneratorto write content to specifiedFile, using specified encoding.Factory method for constructing properly initializedJsonGeneratorto write content using specifiedOutputStream.createGenerator(OutputStream out, JsonEncoding enc) Factory method for constructing properly initializedJsonGeneratorto write content using specifiedOutputStreamand encoding.Factory method for constructing properly initializedJsonGeneratorto 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.booleanDiagnostics 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.booleanbooleanDeprecated.booleanbooleanbooleanbooleanversion()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 defaultBase64Variantfor 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.Since 2.5 usewith(FormatSchema)insteadwithType(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)voidwriteValue(JsonGenerator g, Object value) Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator.voidwriteValue(DataOutput out, Object value) voidwriteValue(File resultFile, Object value) Method that can be used to serialize any Java value as JSON output, written to File provided.voidwriteValue(OutputStream out, Object value) Method that can be used to serialize any Java value as JSON output, using output stream provided (using encodingJsonEncoding.UTF8).voidwriteValue(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 aSequenceWriterto write a sequence of root values using configuration of thisObjectWriter.writeValues(DataOutput out) writeValues(File out) Method for creating aSequenceWriterto write a sequence of root values using configuration of thisObjectWriter.writeValues(OutputStream out) Method for creating aSequenceWriterto write a sequence of root values using configuration of thisObjectWriter.writeValues(Writer out) Method for creating aSequenceWriterto write a sequence of root values using configuration of thisObjectWriter.Method for creating aSequenceWriterto write an array of root-level values, using configuration of thisObjectWriter.writeValuesAsArray(File out) Method for creating aSequenceWriterto write an array of root-level values, using configuration of thisObjectWriter.Method for creating aSequenceWriterto write an array of root-level values, using configuration of thisObjectWriter.writeValuesAsArray(Writer out) Method for creating aSequenceWriterto write an array of root-level values, using configuration of thisObjectWriter.
-
Field Details
-
NULL_PRETTY_PRINTER
We need to keep track of explicit disabling of pretty printing; easiest to do by a token value. -
_config
General serialization configuration settings -
_serializerProvider
-
_serializerFactory
-
_generatorFactory
Factory used for constructingJsonGenerators -
_generatorSettings
Container for settings that need to be passed toJsonGeneratorconstructed for serializing values.- Since:
- 2.5
-
_prefetch
We may pre-fetch serializer if root type is known (has been explicitly declared), and if so, reuse it afterwards. This allows avoiding further serializer lookups and increases performance a bit on cases where readers are reused.- Since:
- 2.5
-
-
Constructor Details
-
ObjectWriter
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp) Constructor used byObjectMapperfor initial instantiation -
ObjectWriter
Alternative constructor for initial instantiation byObjectMapper -
ObjectWriter
Alternative constructor for initial instantiation byObjectMapper -
ObjectWriter
protected ObjectWriter(ObjectWriter base, SerializationConfig config, ObjectWriter.GeneratorSettings genSettings, ObjectWriter.Prefetch prefetch) Copy constructor used for building variations. -
ObjectWriter
Copy constructor used for building variations. -
ObjectWriter
- Since:
- 2.3
-
-
Method Details
-
version
Method that will return version information stored in and read from jar that contains this class. -
_new
Overridable factory method called by various "withXxx()" methods- Since:
- 2.5
-
_new
Overridable factory method called by various "withXxx()" methods- Since:
- 2.5
-
_new
protected ObjectWriter _new(ObjectWriter.GeneratorSettings genSettings, ObjectWriter.Prefetch prefetch) Overridable factory method called by various "withXxx()" methods. It assumes `this` as base for settings other than those directly passed in.- Since:
- 2.5
-
_newSequenceWriter
protected SequenceWriter _newSequenceWriter(boolean wrapInArray, JsonGenerator gen, boolean managedInput) throws IOException Overridable factory method called bywriteValues(OutputStream)method (and its various overrides), and initializes it as necessary.- Throws:
IOException- Since:
- 2.5
-
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 defaultBase64Variantfor 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 initializedJsonGeneratorto 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 initializedJsonGeneratorto write content using specifiedOutputStreamand 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 initializedJsonGeneratorto 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 initializedJsonGeneratorto 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 initializedJsonGeneratorto 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 aSequenceWriterto 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 aSequenceWriterto 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 aJsonGeneratoris 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 aSequenceWriterto 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 aSequenceWriterto 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 aSequenceWriterto 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 orCollectiontype.- Parameters:
out- File to write token stream to- Throws:
IOException- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriterto 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 aJsonGeneratoris 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 orCollectiontype.- Parameters:
gen- Underlying generator to use for writing the token stream- Throws:
IOException- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriterto 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 orCollectiontype.- Parameters:
out- Writer to use for writing the token stream- Throws:
IOException- Since:
- 2.5
-
writeValuesAsArray
Method for creating aSequenceWriterto 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 orCollectiontype.- 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
JsonGeneratoris 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,
JsonFactorythis mapper uses may choose to close the stream depending on its settings (by default, it will try to close it whenJsonGeneratorwe 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,
JsonFactorythis mapper uses may choose to close the stream depending on its settings (by default, it will try to close it whenJsonGeneratorwe construct is closed). -
writeValue
public void writeValue(DataOutput out, Object value) throws IOException, StreamWriteException, DatabindException - Throws:
IOExceptionStreamWriteExceptionDatabindException- Since:
- 2.8
-
writeValueAsString
Method that can be used to serialize any Java value as a String. Functionally equivalent to callingwriteValue(Writer,Object)withStringWriterand 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)withByteArrayOutputStreamand 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 usesSerializerhierarchy 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
Deprecated.Since 2.18 use discouraged; method to be removed from Jackson 3.0Method that can be called to check whetherObjectWriterthinks it could serialize an instance of given Class. -
canSerialize
Deprecated.Since 2.18 use discouraged; method to be removed from Jackson 3.0Method for checking whether instances of given type can be serialized, and optionally why (as perThrowablereturned).- Since:
- 2.3
-
_serializerProvider
Overridable helper method used for constructingSerializerProviderto use for serialization. -
_verifySchemaType
- Since:
- 2.2
-
_writeValueAndClose
Method called to configure the generator as necessary and then call write functionality- Throws:
IOException- Since:
- 2.11.2
-
_configureGenerator
Helper method called to set or override settings of passed-inJsonGenerator- Since:
- 2.5
-
_assertNotNull
-