Package org.codehaus.jackson.map
Class ObjectWriter
java.lang.Object
org.codehaus.jackson.map.ObjectWriter
- All Implemented Interfaces:
Versioned
Builder object that can be used for per-serialization configuration of
serialization parameters, such as JSON View and root type to use.
(and thus fully thread-safe with no external synchronization);
new instances are constructed for different configurations.
Instances are initially constructed by
ObjectMapper
and can be
reused in completely thread-safe manner with no explicit synchronization- Since:
- 1.5
- Author:
- tatu
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SerializationConfig
General serialization configuration settingsprotected final JsonFactory
Factory used for constructingJsonGenerator
sprotected final PrettyPrinter
To allow for dynamic enabling/disabling of pretty printing, pretty printer can be optionally configured for writer as wellprotected final SerializerProvider
protected final JavaType
Specified root serialization type to use; can be same as runtime type, but usually one of its super typesprotected final FormatSchema
When using data format that uses a schema, schema is passed to generator.protected final SerializerFactory
protected static final PrettyPrinter
We need to keep track of explicit disabling of pretty printing; easiest to do by a token value. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ObjectWriter
(ObjectMapper mapper, SerializationConfig config) Alternative constructor for initial instantiation.protected
ObjectWriter
(ObjectMapper mapper, SerializationConfig config, FormatSchema s) Alternative constructor for initial instantiation.protected
ObjectWriter
(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp) Constructor used byObjectMapper
for initial instantiationprotected
ObjectWriter
(ObjectWriter base, SerializationConfig config) Copy constructor used for building variations.protected
ObjectWriter
(ObjectWriter base, SerializationConfig config, JavaType rootType, PrettyPrinter pp, FormatSchema s) Copy constructor used for building variations. -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
_configAndWriteValue
(JsonGenerator jgen, Object value) Method called to configure the generator as necessary and then call write functionalityboolean
canSerialize
(Class<?> type) version()
Method that will return version information stored in and read from jar that contains this class.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.Method that will construct a new instance that will use the default pretty printer for serialization.withFilters
(FilterProvider filterProvider) Method that will construct a new instance that uses specified provider for resolving filter instances by id.Method that will construct a new instance that will use specified pretty printer (or, if null, will not do any pretty-printing)withSchema
(FormatSchema schema) 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.withType
(TypeReference<?> rootType) 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
(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.void
writeValue
(JsonGenerator jgen, Object value) Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator
.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.
-
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 -
_provider
-
_serializerFactory
-
_jsonFactory
Factory used for constructingJsonGenerator
s -
_rootType
Specified root serialization type to use; can be same as runtime type, but usually one of its super types -
_prettyPrinter
To allow for dynamic enabling/disabling of pretty printing, pretty printer can be optionally configured for writer as well -
_schema
When using data format that uses a schema, schema is passed to generator.- Since:
- 1.8
-
-
Constructor Details
-
ObjectWriter
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp) Constructor used byObjectMapper
for initial instantiation -
ObjectWriter
Alternative constructor for initial instantiation.- Since:
- 1.7
-
ObjectWriter
Alternative constructor for initial instantiation.- Since:
- 1.7
-
ObjectWriter
protected ObjectWriter(ObjectWriter base, SerializationConfig config, JavaType rootType, PrettyPrinter pp, FormatSchema s) Copy constructor used for building variations. -
ObjectWriter
Copy constructor used for building variations.- Since:
- 1.7
-
-
Method Details
-
version
Method that will return version information stored in and read from jar that contains this class. -
withView
Method that will construct a new instance that uses specified serialization view for serialization (with null basically disables view processing) -
withType
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. -
withType
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. -
withType
- Since:
- 1.7
-
withPrettyPrinter
Method that will construct a new instance that will use specified pretty printer (or, if null, will not do any pretty-printing)- Since:
- 1.6
-
withDefaultPrettyPrinter
Method that will construct a new instance that will use the default pretty printer for serialization.- Since:
- 1.6
-
withFilters
Method that will construct a new instance that uses specified provider for resolving filter instances by id.- Since:
- 1.7
-
withSchema
- Since:
- 1.8
-
withDateFormat
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.- Since:
- 1.9
-
writeValue
public void writeValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator
. -
writeValue
public void writeValue(File resultFile, Object value) throws IOException, JsonGenerationException, JsonMappingException 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, JsonGenerationException, JsonMappingException 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, JsonGenerationException, JsonMappingException 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). -
writeValueAsString
public String writeValueAsString(Object value) throws IOException, JsonGenerationException, JsonMappingException 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. -
writeValueAsBytes
public byte[] writeValueAsBytes(Object value) throws IOException, JsonGenerationException, JsonMappingException 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. -
canSerialize
-
_configAndWriteValue
protected final void _configAndWriteValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException Method called to configure the generator as necessary and then call write functionality
-