com.fasterxml.jackson.databind

Class ObjectWriter

  • All Implemented Interfaces:
    com.fasterxml.jackson.core.Versioned, Serializable


    public class ObjectWriter
    extends Object
    implements com.fasterxml.jackson.core.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 Detail

      • NULL_PRETTY_PRINTER

        protected static final com.fasterxml.jackson.core.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
      • _generatorFactory

        protected final com.fasterxml.jackson.core.JsonFactory _generatorFactory
        Factory used for constructing JsonGenerators
      • _generatorSettings

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

        protected final ObjectWriter.Prefetch _prefetch
        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.5
    • Method Detail

      • version

        public com.fasterxml.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 com.fasterxml.jackson.core.Versioned
      • _new

        protected ObjectWriter _new(ObjectWriter base,
                        com.fasterxml.jackson.core.JsonFactory f)
        Overridable factory method called by various "withXxx()" methods
        Since:
        2.5
      • _newSequenceWriter

        protected SequenceWriter _newSequenceWriter(boolean wrapInArray,
                                        com.fasterxml.jackson.core.JsonGenerator gen,
                                        boolean managedInput)
                                             throws IOException
        Overridable factory method called by #createSequenceWriter(JsonGenerator) method (and its various overrides), and initializes it as necessary.
        Throws:
        IOException
        Since:
        2.5
      • 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.
      • withoutFeatures

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

        public ObjectWriter with(com.fasterxml.jackson.core.JsonGenerator.Feature feature)
        Since:
        2.5
      • withFeatures

        public ObjectWriter withFeatures(com.fasterxml.jackson.core.JsonGenerator.Feature... features)
        Since:
        2.5
      • without

        public ObjectWriter without(com.fasterxml.jackson.core.JsonGenerator.Feature feature)
        Since:
        2.5
      • withoutFeatures

        public ObjectWriter withoutFeatures(com.fasterxml.jackson.core.JsonGenerator.Feature... features)
        Since:
        2.5
      • 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(com.fasterxml.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 SerializationConfig.withRootName(String) for details.

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

      • with

        public ObjectWriter with(com.fasterxml.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.

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

        Since:
        2.5
      • 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.
        Since:
        2.5
      • forType

        public ObjectWriter forType(com.fasterxml.jackson.core.type.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(com.fasterxml.jackson.core.Base64Variant b64variant)
        Method that will construct a new instance that uses specified default Base64Variant for base64 encoding
        Since:
        2.1
      • with

        public ObjectWriter with(com.fasterxml.jackson.core.io.CharacterEscapes escapes)
        Since:
        2.3
      • with

        public ObjectWriter with(com.fasterxml.jackson.core.JsonFactory f)
        Since:
        2.3
      • withRootValueSeparator

        public ObjectWriter withRootValueSeparator(String sep)
        Since:
        2.5
      • withRootValueSeparator

        public ObjectWriter withRootValueSeparator(com.fasterxml.jackson.core.SerializableString sep)
        Since:
        2.5
      • writeValues

        public SequenceWriter writeValues(File out)
                                   throws IOException
        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:
        out - Target file to write value sequence to.
        Throws:
        IOException
        Since:
        2.5
      • writeValues

        public SequenceWriter writeValues(com.fasterxml.jackson.core.JsonGenerator gen)
                                   throws IOException
        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:
        gen - Low-level generator caller has already constructed that will be used for actual writing of token stream.
        Throws:
        IOException
        Since:
        2.5
      • writeValues

        public SequenceWriter writeValues(Writer out)
                                   throws IOException
        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:
        out - Target writer to use for writing the token stream
        Throws:
        IOException
        Since:
        2.5
      • writeValues

        public SequenceWriter writeValues(OutputStream out)
                                   throws IOException
        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:
        out - Physical output stream to use for writing the token stream
        Throws:
        IOException
        Since:
        2.5
      • writeValuesAsArray

        public SequenceWriter writeValuesAsArray(File out)
                                          throws IOException
        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:
        out - File to write token stream to
        Throws:
        IOException
        Since:
        2.5
      • writeValuesAsArray

        public SequenceWriter writeValuesAsArray(com.fasterxml.jackson.core.JsonGenerator gen)
                                          throws IOException
        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:
        gen - Underlying generator to use for writing the token stream
        Throws:
        IOException
        Since:
        2.5
      • writeValuesAsArray

        public SequenceWriter writeValuesAsArray(Writer out)
                                          throws IOException
        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:
        out - Writer to use for writing the token stream
        Throws:
        IOException
        Since:
        2.5
      • writeValuesAsArray

        public SequenceWriter writeValuesAsArray(OutputStream out)
                                          throws IOException
        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:
        out - Physical output stream to use for writing the token stream
        Throws:
        IOException
        Since:
        2.5
      • isEnabled

        public boolean isEnabled(com.fasterxml.jackson.core.JsonParser.Feature f)
      • getJsonFactory

        @Deprecated
        public com.fasterxml.jackson.core.JsonFactory getJsonFactory()
        Deprecated. Since 2.2, use getFactory() instead.
      • getFactory

        public com.fasterxml.jackson.core.JsonFactory getFactory()
        Since:
        2.2
      • 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
      • writeValue

        public void writeValue(com.fasterxml.jackson.core.JsonGenerator gen,
                      Object value)
                        throws IOException,
                               com.fasterxml.jackson.core.JsonGenerationException,
                               JsonMappingException
        Method that can be used to serialize any Java value as JSON output, using provided JsonGenerator.
        Throws:
        IOException
        com.fasterxml.jackson.core.JsonGenerationException
        JsonMappingException
      • writeValue

        public void writeValue(OutputStream out,
                      Object value)
                        throws IOException,
                               com.fasterxml.jackson.core.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
        com.fasterxml.jackson.core.JsonGenerationException
        JsonMappingException
      • writeValue

        public void writeValue(Writer w,
                      Object value)
                        throws IOException,
                               com.fasterxml.jackson.core.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
        com.fasterxml.jackson.core.JsonGenerationException
        JsonMappingException
      • writeValueAsString

        public String writeValueAsString(Object value)
                                  throws com.fasterxml.jackson.core.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:
        com.fasterxml.jackson.core.JsonProcessingException
      • writeValueAsBytes

        public byte[] writeValueAsBytes(Object value)
                                 throws com.fasterxml.jackson.core.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:
        com.fasterxml.jackson.core.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)
      • canSerialize

        public boolean canSerialize(Class<?> type,
                           AtomicReference<Throwable> cause)
        Method for checking whether instances of given type can be serialized, and optionally why (as per Throwable returned).
        Since:
        2.3
      • _verifySchemaType

        protected void _verifySchemaType(com.fasterxml.jackson.core.FormatSchema schema)
        Since:
        2.2
      • _configAndWriteValue

        protected final void _configAndWriteValue(com.fasterxml.jackson.core.JsonGenerator gen,
                                Object value)
                                           throws IOException
        Method called to configure the generator as necessary and then call write functionality
        Throws:
        IOException
      • _prefetchRootSerializer

        protected ObjectWriter.Prefetch _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.
      • _configureJsonGenerator

        @Deprecated
        protected void _configureJsonGenerator(com.fasterxml.jackson.core.JsonGenerator gen)
        Deprecated. Since 2.5 (to be removed from 2.6 or later)
        Helper method called to set or override settings of passed-in JsonGenerator
        Since:
        2.1
      • _configureGenerator

        protected com.fasterxml.jackson.core.JsonGenerator _configureGenerator(com.fasterxml.jackson.core.JsonGenerator gen)
        Helper method called to set or override settings of passed-in JsonGenerator
        Since:
        2.5

Copyright © 2014 FasterXML. All Rights Reserved.