com.fasterxml.jackson.databind

## Class ObjectWriter

- java.lang.Object
- - com.fasterxml.jackson.databind.ObjectWriter

All Implemented Interfaces:
  Versioned, Serializable

    public class ObjectWriter
    extends Object
    implements Versioned, Serializable

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

See Also:
  Serialized Form

### Field Summary

Fields
`protected boolean` `_cfgBigDecimalAsPlain`
`protected CharacterEscapes` `_characterEscapes` Caller may want to specify character escaping details, either as defaults, or on call-by-call basis.
`protected SerializationConfig` `_config` General serialization configuration settings
`protected JsonFactory` `_generatorFactory` 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 JsonSerializer<Object>` `_rootSerializer` We may pre-fetch serializer if `_rootType` is known, and if so, reuse it afterwards.
`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 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.

### Constructor Summary

Constructors
`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, JavaType rootType, JsonSerializer<Object> rootSer, PrettyPrinter pp, FormatSchema s, CharacterEscapes escapes)` Copy constructor used for building variations.

### Method Summary

Methods
`protected void` `_configAndWriteValue(JsonGenerator jgen, Object value)` Method called to configure the generator as necessary and then call write functionality
`protected JsonSerializer<Object>` `_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)`
`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(JsonFactory f)`
`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(SerializationFeature... features)` Method for constructing a new instance that is configured with specified features enabled.
`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(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` `withSchema(FormatSchema schema)` Method that will construct a new instance that uses specific format schema for serialization.
`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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### NULL_PRETTY_PRINTER

    protected static final PrettyPrinter NULL_PRETTY_PRINTER

We need to keep track of explicit disabling of pretty printing; easiest to do by a token value.

#### _config

    protected final SerializationConfig _config

General serialization configuration settings

#### _serializerProvider

    protected final DefaultSerializerProvider _serializerProvider

#### _serializerFactory

    protected final SerializerFactory _serializerFactory

#### _generatorFactory

    protected final JsonFactory _generatorFactory

Factory used for constructing `JsonGenerator`s

#### _rootType

    protected final JavaType _rootType

Specified root serialization type to use; can be same as runtime type, but usually one of its super types

#### _rootSerializer

    protected final JsonSerializer<Object> _rootSerializer

We may pre-fetch serializer if `_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.

Since:
  2.1

#### _prettyPrinter

    protected final PrettyPrinter _prettyPrinter

To allow for dynamic enabling/disabling of pretty printing, pretty printer can be optionally configured for writer as well

#### _schema

    protected final FormatSchema _schema

When using data format that uses a schema, schema is passed to generator.

#### _characterEscapes

    protected final CharacterEscapes _characterEscapes

Caller may want to specify character escaping details, either as defaults, or on call-by-call basis.

Since:
  2.3

#### _cfgBigDecimalAsPlain

    protected final boolean _cfgBigDecimalAsPlain

Since:
  2.3

### Constructor Detail

#### ObjectWriter

    protected ObjectWriter(ObjectMapper mapper,
                SerializationConfig config,
                JavaType rootType,
                PrettyPrinter pp)

Constructor used by `ObjectMapper` for initial instantiation

#### ObjectWriter

    protected ObjectWriter(ObjectMapper mapper,
                SerializationConfig config)

Alternative constructor for initial instantiation by `ObjectMapper`

#### ObjectWriter

    protected ObjectWriter(ObjectMapper mapper,
                SerializationConfig config,
                FormatSchema s)

Alternative constructor for initial instantiation by `ObjectMapper`

#### ObjectWriter

    protected ObjectWriter(ObjectWriter base,
                SerializationConfig config,
                JavaType rootType,
                JsonSerializer<Object> rootSer,
                PrettyPrinter pp,
                FormatSchema s,
                CharacterEscapes escapes)

Copy constructor used for building variations.

#### ObjectWriter

    protected ObjectWriter(ObjectWriter base,
                SerializationConfig config)

Copy constructor used for building variations.

#### ObjectWriter

    protected ObjectWriter(ObjectWriter base,
                JsonFactory f)

Since:
  2.3

### Method Detail

#### version

    public Version version()

Method that will return version information stored in and read from jar that contains this class.

Specified by:
  `version` in interface `Versioned`

#### with

    public ObjectWriter with(SerializationFeature feature)

Method for constructing a new instance that is configured with specified feature enabled.

#### with

    public ObjectWriter with(SerializationFeature first,
                    SerializationFeature... other)

Method for constructing a new instance that is configured with specified features enabled.

#### withFeatures

    public ObjectWriter withFeatures(SerializationFeature... features)

Method for constructing a new instance that is configured with specified features enabled.

#### without

    public ObjectWriter without(SerializationFeature feature)

Method for constructing a new instance that is configured with specified feature enabled.

#### without

    public ObjectWriter without(SerializationFeature first,
                       SerializationFeature... other)

Method for constructing a new instance that is configured with specified features enabled.

#### withoutFeatures

    public ObjectWriter withoutFeatures(SerializationFeature... features)

Method for constructing a new instance that is configured with specified features enabled.

#### with

    public 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.

Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.

#### withDefaultPrettyPrinter

    public ObjectWriter withDefaultPrettyPrinter()

Method that will construct a new instance that will use the default pretty printer for serialization.

#### with

    public ObjectWriter with(FilterProvider filterProvider)

Method that will construct a new instance that uses specified provider for resolving filter instances by id.

#### with

    public 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)

#### withRootName

    public ObjectWriter withRootName(String rootName)

Method for constructing a new instance with configuration that specifies what root name to use for "root element wrapping". See `SerializationConfig.withRootName(String)` for details.

Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.

#### withSchema

    public ObjectWriter withSchema(FormatSchema schema)

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.

#### withType

    public 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.

Note that method does NOT change state of this reader, but rather construct and returns a newly configured instance.

#### withType

    public 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.

#### withType

    public ObjectWriter withType(TypeReference<?> rootType)

#### withView

    public ObjectWriter withView(Class<?> view)

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

    public ObjectWriter with(Locale l)

#### with

    public ObjectWriter with(TimeZone tz)

#### with

    public ObjectWriter with(Base64Variant b64variant)

Method that will construct a new instance that uses specified default `Base64Variant` for base64 encoding

Since:
  2.1

#### with

    public ObjectWriter with(CharacterEscapes escapes)

Since:
  2.3

#### with

    public ObjectWriter with(JsonFactory f)

Since:
  2.3

#### with

    public ObjectWriter with(ContextAttributes attrs)

Since:
  2.3

#### withAttributes

    public ObjectWriter withAttributes(Map<Object,Object> attrs)

Since:
  2.3

#### withAttribute

    public ObjectWriter withAttribute(Object key,
                             Object value)

Since:
  2.3

#### withoutAttribute

    public ObjectWriter withoutAttribute(Object key)

Since:
  2.3

#### isEnabled

    public boolean isEnabled(SerializationFeature f)

#### isEnabled

    public boolean isEnabled(MapperFeature f)

#### isEnabled

    public boolean isEnabled(JsonParser.Feature f)

#### getConfig

    public SerializationConfig getConfig()

Since:
  2.2

#### getJsonFactory

    @Deprecated
    public JsonFactory getJsonFactory()

Deprecated. Since 2.2, use `getFactory()` instead.

#### getFactory

    public JsonFactory getFactory()

Since:
  2.2

#### getTypeFactory

    public TypeFactory 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

    public ContextAttributes getAttributes()

Since:
  2.3

#### 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 provided `JsonGenerator`.

Throws:
  `IOException`
  `JsonGenerationException`
  `JsonMappingException`

#### 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.

Throws:
  `IOException`
  `JsonGenerationException`
  `JsonMappingException`

#### 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 encoding `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).

Throws:
  `IOException`
  `JsonGenerationException`
  `JsonMappingException`

#### 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 when `JsonGenerator` we construct is closed).

Throws:
  `IOException`
  `JsonGenerationException`
  `JsonMappingException`

#### writeValueAsString

    public String writeValueAsString(Object value)
                              throws JsonProcessingException

Method that can be used to serialize any Java value as a String. Functionally equivalent to calling `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.

Throws:
  `JsonProcessingException`

#### writeValueAsBytes

    public byte[] writeValueAsBytes(Object value)
                             throws JsonProcessingException

Method that can be used to serialize any Java value as a byte array. Functionally equivalent to calling `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.

Throws:
  `JsonProcessingException`

#### acceptJsonFormatVisitor

    public void acceptJsonFormatVisitor(JavaType type,
                               JsonFormatVisitorWrapper visitor)
                                 throws JsonMappingException

Method for visiting type hierarchy for given type, using specified visitor. Visitation uses `Serializer` hierarchy and related properties

This 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

#### canSerialize

    public boolean canSerialize(Class<?> type)

#### _serializerProvider

    protected DefaultSerializerProvider _serializerProvider(SerializationConfig config)

Overridable helper method used for constructing `SerializerProvider` to use for serialization.

#### _verifySchemaType

    protected void _verifySchemaType(FormatSchema schema)

Since:
  2.2

#### _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

Throws:
  `IOException`
  `JsonGenerationException`
  `JsonMappingException`

#### _prefetchRootSerializer

    protected JsonSerializer<Object> _prefetchRootSerializer(SerializationConfig config,
                                                 JavaType valueType)

Method called to locate (root) serializer ahead of time, if permitted by configuration. Method also is NOT to throw an exception if access fails.