public class ObjectWriter extends Object implements Versioned
ObjectMapper
and can be
reused in completely thread-safe manner with no explicit synchronizationModifier and Type | Field and Description |
---|---|
protected SerializationConfig |
_config
General serialization configuration settings
|
protected JsonFactory |
_jsonFactory
Factory used for constructing
JsonGenerator s |
protected PrettyPrinter |
_prettyPrinter
To allow for dynamic enabling/disabling of pretty printing,
pretty printer can be optionally configured for writer
as well
|
protected SerializerProvider |
_provider |
protected JavaType |
_rootType
Specified root serialization type to use; can be same
as runtime type, but usually one of its super types
|
protected FormatSchema |
_schema
When using data format that uses a schema, schema is passed
to generator.
|
protected SerializerFactory |
_serializerFactory |
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.
|
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 by
ObjectMapper for initial instantiation |
protected |
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.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_configAndWriteValue(JsonGenerator jgen,
Object value)
Method called to configure the generator as necessary and then
call write functionality
|
boolean |
canSerialize(Class<?> type) |
Version |
version()
Method that will return version information stored in and read from jar
that contains this class.
|
ObjectWriter |
withDateFormat(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 |
withDefaultPrettyPrinter()
Method that will construct a new instance that will use the default
pretty printer for serialization.
|
ObjectWriter |
withFilters(FilterProvider filterProvider)
Method that will construct a new instance that uses specified
provider for resolving filter instances by id.
|
ObjectWriter |
withPrettyPrinter(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 |
withSchema(FormatSchema schema) |
ObjectWriter |
withType(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 |
withType(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 |
withType(TypeReference<?> rootType) |
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 jgen,
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.
|
protected static final PrettyPrinter NULL_PRETTY_PRINTER
protected final SerializationConfig _config
protected final SerializerProvider _provider
protected final SerializerFactory _serializerFactory
protected final JsonFactory _jsonFactory
JsonGenerator
sprotected final JavaType _rootType
protected final PrettyPrinter _prettyPrinter
protected final FormatSchema _schema
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp)
ObjectMapper
for initial instantiationprotected ObjectWriter(ObjectMapper mapper, SerializationConfig config)
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, FormatSchema s)
protected ObjectWriter(ObjectWriter base, SerializationConfig config, JavaType rootType, PrettyPrinter pp, FormatSchema s)
protected ObjectWriter(ObjectWriter base, SerializationConfig config)
public Version version()
public ObjectWriter withView(Class<?> view)
public ObjectWriter withType(JavaType rootType)
public ObjectWriter withType(Class<?> rootType)
public ObjectWriter withType(TypeReference<?> rootType)
public ObjectWriter withPrettyPrinter(PrettyPrinter pp)
public ObjectWriter withDefaultPrettyPrinter()
public ObjectWriter withFilters(FilterProvider filterProvider)
public ObjectWriter withSchema(FormatSchema schema)
public ObjectWriter withDateFormat(DateFormat df)
public void writeValue(JsonGenerator jgen, 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 IOException, JsonGenerationException, JsonMappingException
writeValue(Writer,Object)
with StringWriter
and constructing String, but more efficient.public byte[] writeValueAsBytes(Object value) throws IOException, JsonGenerationException, JsonMappingException
writeValue(Writer,Object)
with ByteArrayOutputStream
and getting bytes, but more efficient.
Encoding used will be UTF-8.public boolean canSerialize(Class<?> type)
protected final void _configAndWriteValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException