Class ObjectWriter

java.lang.Object
tools.jackson.databind.ObjectWriter
All Implemented Interfaces:
tools.jackson.core.Versioned

public class ObjectWriter extends Object implements tools.jackson.core.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
  • Field Details

    • _config

      protected final SerializationConfig _config
      General serialization configuration settings
    • _serializationContexts

      protected final SerializationContexts _serializationContexts
      Factory used for constructing per-call SerializationContexts.

      Note: while serializers are only exposed SerializationContext, mappers and readers need to access additional API defined by SerializationContextExt

    • _generatorFactory

      protected final tools.jackson.core.TokenStreamFactory _generatorFactory
      Factory used for constructing JsonGenerators
    • _generatorSettings

      protected final GeneratorSettings _generatorSettings
      Container for settings that need to be passed to JsonGenerator constructed for serializing values.
    • _prefetch

      protected final ObjectWriter.Prefetch _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.
  • Constructor Details

  • Method Details

    • version

      public tools.jackson.core.Version version()
      Method that will return version information stored in and read from jar that contains this class.
      Specified by:
      version in interface tools.jackson.core.Versioned
    • _new

      protected ObjectWriter _new(ObjectWriter base, SerializationConfig config)
      Overridable factory method called by various "withXxx()" methods
    • _new

      protected ObjectWriter _new(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.
    • _newSequenceWriter

      protected final SequenceWriter _newSequenceWriter(SerializationContextExt ctxt, boolean wrapInArray, tools.jackson.core.JsonGenerator gen, boolean managedInput) throws tools.jackson.core.JacksonException
      Overridable factory method called by writeValues(OutputStream) method (and its various overrides), and initializes it as necessary.
      Throws:
      tools.jackson.core.JacksonException
    • 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 disabled.
    • without

      public ObjectWriter without(SerializationFeature first, SerializationFeature... other)
      Method for constructing a new instance that is configured with specified features disabled.
    • withoutFeatures

      public ObjectWriter withoutFeatures(SerializationFeature... features)
      Method for constructing a new instance that is configured with specified features disabled.
    • with

      public ObjectWriter with(DatatypeFeature feature)
      Method for constructing a new instance that is configured with specified feature enabled.
    • withFeatures

      public ObjectWriter withFeatures(DatatypeFeature... features)
      Method for constructing a new instance that is configured with specified features enabled.
    • without

      public ObjectWriter without(DatatypeFeature feature)
      Method for constructing a new instance that is configured with specified feature disabled.
    • withoutFeatures

      public ObjectWriter withoutFeatures(DatatypeFeature... features)
      Method for constructing a new instance that is configured with specified features disabled.
    • with

      public ObjectWriter with(tools.jackson.core.StreamWriteFeature feature)
    • withFeatures

      public ObjectWriter withFeatures(tools.jackson.core.StreamWriteFeature... features)
    • without

      public ObjectWriter without(tools.jackson.core.StreamWriteFeature feature)
    • withoutFeatures

      public ObjectWriter withoutFeatures(tools.jackson.core.StreamWriteFeature... features)
    • with

      public ObjectWriter with(tools.jackson.core.FormatFeature feature)
    • withFeatures

      public ObjectWriter withFeatures(tools.jackson.core.FormatFeature... features)
    • without

      public ObjectWriter without(tools.jackson.core.FormatFeature feature)
    • withoutFeatures

      public ObjectWriter withoutFeatures(tools.jackson.core.FormatFeature... features)
    • forType

      public ObjectWriter forType(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.

    • forType

      public ObjectWriter forType(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.
    • forType

      public ObjectWriter forType(tools.jackson.core.type.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.
    • 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(tools.jackson.core.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 MapperConfigBase.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

      public ObjectWriter withRootName(PropertyName rootName)
    • withoutRootName

      public ObjectWriter withoutRootName()
      Convenience method that is same as calling: withRootName("") which will forcibly prevent use of root name wrapping when writing values with this ObjectWriter.
    • with

      public ObjectWriter with(tools.jackson.core.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.

    • 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(tools.jackson.core.Base64Variant b64variant)
      Method that will construct a new instance that uses specified default Base64Variant for base64 encoding
    • with

      public ObjectWriter with(tools.jackson.core.io.CharacterEscapes escapes)
    • with

      public ObjectWriter with(ContextAttributes attrs)
      Mutant factory for overriding set of (default) attributes for ObjectWriter to use.

      Note that this will replace defaults passed by ObjectMapper.

      Parameters:
      attrs - Default ContextAttributes to use with a writer
      Returns:
      ObjectWriter instance with specified default attributes (which is usually a newly constructed writer instance with otherwise identical settings)
    • withAttributes

      public ObjectWriter withAttributes(Map<?,?> attrs)
      Mutant factory method that allows construction of a new writer instance that uses specified set of default attribute values.
    • withAttribute

      public ObjectWriter withAttribute(Object key, Object value)
    • withoutAttribute

      public ObjectWriter withoutAttribute(Object key)
    • withRootValueSeparator

      public ObjectWriter withRootValueSeparator(String sep)
    • withRootValueSeparator

      public ObjectWriter withRootValueSeparator(tools.jackson.core.SerializableString sep)
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(OutputStream target)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,OutputStream).
      Since:
      3.0
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(OutputStream target, tools.jackson.core.JsonEncoding enc)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,OutputStream,JsonEncoding).
      Since:
      3.0
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(Writer target)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,Writer).
      Since:
      3.0
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(File target, tools.jackson.core.JsonEncoding enc)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,File,JsonEncoding).
      Since:
      3.0
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(Path target, tools.jackson.core.JsonEncoding enc)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,Path,JsonEncoding).
      Since:
      3.0
    • createGenerator

      public tools.jackson.core.JsonGenerator createGenerator(DataOutput target)
      Factory method for constructing JsonGenerator that is properly wired to allow callbacks for serialization: basically constructs a ObjectWriteContext and then calls TokenStreamFactory.createGenerator(ObjectWriteContext,DataOutput).
      Since:
      3.0
    • createObjectNode

      public ObjectNode createObjectNode()
    • createArrayNode

      public ArrayNode createArrayNode()
    • writeValues

      public SequenceWriter writeValues(File target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write a sequence of root values using configuration of this ObjectWriter. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure closing of underlying generator and output stream.
      Parameters:
      target - Target file to write value sequence to.
      Throws:
      tools.jackson.core.JacksonException
    • writeValues

      public SequenceWriter writeValues(Path target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write a sequence of root values using configuration of this ObjectWriter. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure closing of underlying generator and output stream.
      Parameters:
      target - Target path to write value sequence to.
      Throws:
      tools.jackson.core.JacksonException
      Since:
      3.0
    • writeValues

      public SequenceWriter writeValues(tools.jackson.core.JsonGenerator g) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write a sequence of root values using configuration of this ObjectWriter. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure that all content gets flushed by the generator. However, since a JsonGenerator is explicitly passed, it will NOT be closed when SequenceWriter.close() is called.
      Parameters:
      g - Low-level generator caller has already constructed that will be used for actual writing of token stream.
      Throws:
      tools.jackson.core.JacksonException
    • writeValues

      public SequenceWriter writeValues(Writer target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write a sequence of root values using configuration of this ObjectWriter. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure closing of underlying generator and output stream.
      Parameters:
      target - Target writer to use for writing the token stream
      Throws:
      tools.jackson.core.JacksonException
    • writeValues

      public SequenceWriter writeValues(OutputStream target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write a sequence of root values using configuration of this ObjectWriter. Sequence is not surrounded by JSON array; some backend types may not support writing of such sequences as root level. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure closing of underlying generator and output stream.
      Parameters:
      target - Physical output stream to use for writing the token stream
      Throws:
      tools.jackson.core.JacksonException
    • writeValues

      public SequenceWriter writeValues(DataOutput target) throws tools.jackson.core.JacksonException
      Throws:
      tools.jackson.core.JacksonException
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(File target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write an array of root-level values, using configuration of this ObjectWriter. Resulting writer needs to be SequenceWriter.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 or Collection type.

      Parameters:
      target - File to write token stream to
      Throws:
      tools.jackson.core.JacksonException
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(Path target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write an array of root-level values, using configuration of this ObjectWriter. Resulting writer needs to be SequenceWriter.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 or Collection type.

      Parameters:
      target - Path to write token stream to
      Throws:
      tools.jackson.core.JacksonException
      Since:
      3.0
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(tools.jackson.core.JsonGenerator g) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write an array of root-level values, using configuration of this ObjectWriter. Resulting writer needs to be SequenceWriter.close()d after all values have been written to ensure that all content gets flushed by the generator. However, since a JsonGenerator is explicitly passed, it will NOT be closed when SequenceWriter.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 or Collection type.

      Parameters:
      g - Underlying generator to use for writing the token stream
      Throws:
      tools.jackson.core.JacksonException
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(Writer target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write an array of root-level values, using configuration of this ObjectWriter. Resulting writer needs to be SequenceWriter.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 or Collection type.

      Parameters:
      target - Writer to use for writing the token stream
      Throws:
      tools.jackson.core.JacksonException
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(OutputStream target) throws tools.jackson.core.JacksonException
      Method for creating a SequenceWriter to write an array of root-level values, using configuration of this ObjectWriter. Resulting writer needs to be SequenceWriter.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 or Collection type.

      Parameters:
      target - Physical output stream to use for writing the token stream
      Throws:
      tools.jackson.core.JacksonException
    • writeValuesAsArray

      public SequenceWriter writeValuesAsArray(DataOutput target) throws tools.jackson.core.JacksonException
      Throws:
      tools.jackson.core.JacksonException
    • isEnabled

      public boolean isEnabled(SerializationFeature f)
    • isEnabled

      public boolean isEnabled(MapperFeature f)
    • isEnabled

      public boolean isEnabled(DatatypeFeature f)
    • isEnabled

      public boolean isEnabled(tools.jackson.core.StreamWriteFeature f)
    • getConfig

      public SerializationConfig getConfig()
    • generatorFactory

      public tools.jackson.core.TokenStreamFactory generatorFactory()
      Since:
      3.0
    • typeFactory

      public TypeFactory typeFactory()
    • 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.
    • getAttributes

      public ContextAttributes getAttributes()
    • getTypeFactory

      @Deprecated public TypeFactory getTypeFactory()
      Deprecated.
      Since 3.0 use typeFactory()
    • writeValue

      public void writeValue(tools.jackson.core.JsonGenerator g, Object value) throws tools.jackson.core.JacksonException
      Method that can be used to serialize any Java value as JSON output, using provided JsonGenerator.
      Throws:
      tools.jackson.core.JacksonException
    • writeValue

      public void writeValue(File target, Object value) throws tools.jackson.core.JacksonException
      Method that can be used to serialize any Java value as JSON output, written to File provided.
      Throws:
      tools.jackson.core.JacksonException
    • writeValue

      public void writeValue(Path target, Object value) throws tools.jackson.core.JacksonException
      Method that can be used to serialize any Java value as JSON output, written to Path provided.
      Throws:
      tools.jackson.core.JacksonException
      Since:
      3.0
    • writeValue

      public void writeValue(OutputStream target, Object value) throws tools.jackson.core.JacksonException
      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, TokenStreamFactory 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:
      tools.jackson.core.JacksonException
    • writeValue

      public void writeValue(Writer target, Object value) throws tools.jackson.core.JacksonException
      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, TokenStreamFactory 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:
      tools.jackson.core.JacksonException
    • writeValue

      public void writeValue(DataOutput target, Object value) throws tools.jackson.core.JacksonException
      Throws:
      tools.jackson.core.JacksonException
    • writeValueAsString

      public String writeValueAsString(Object value) throws tools.jackson.core.JacksonException
      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.
      Throws:
      tools.jackson.core.JacksonException
    • writeValueAsBytes

      public byte[] writeValueAsBytes(Object value) throws tools.jackson.core.JacksonException
      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.
      Throws:
      tools.jackson.core.JacksonException
    • _configAndWriteValue

      protected final void _configAndWriteValue(SerializationContextExt ctxt, tools.jackson.core.JsonGenerator gen, Object value) throws tools.jackson.core.JacksonException
      Method called to configure the generator as necessary and then call write functionality
      Throws:
      tools.jackson.core.JacksonException
    • valueToTree

      public <T extends JsonNode> T valueToTree(Object fromValue) throws tools.jackson.core.JacksonException
      Method that will convert given Java value (usually bean) into its equivalent Tree model JsonNode representation. Functionally similar to serializing value into token stream and parsing that stream back as tree model node, but more efficient as TokenBuffer is used to contain the intermediate representation instead of fully serialized contents.

      NOTE: while results are usually identical to that of serialization followed by deserialization, this is not always the case. In some cases serialization into intermediate representation will retain encapsulation of things like raw value (RawValue) or basic node identity (JsonNode). If so, result is a valid tree, but values are not re-constructed through actual format representation. So if transformation requires actual materialization of encoded content, it will be necessary to do actual serialization.

      Type Parameters:
      T - Actual node type; usually either basic JsonNode or ObjectNode
      Parameters:
      fromValue - Java value to convert
      Returns:
      (non-null) Root node of the resulting content tree: in case of null value node for which JsonNode.isNull() returns true.
      Throws:
      tools.jackson.core.JacksonException
      Since:
      3.0
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor)
      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)
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor)
    • _serializationContext

      protected final SerializationContextExt _serializationContext()
      Overridable helper method used for constructing SerializationContext to use for serialization.
    • _verifySchemaType

      protected void _verifySchemaType(tools.jackson.core.FormatSchema schema)
    • _assertNotNull

      protected final void _assertNotNull(String paramName, Object src)