Class ObjectWriter

  • All Implemented Interfaces:
    Versioned

    public class ObjectWriter
    extends java.lang.Object
    implements 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 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
      • _rootType

        protected final JavaType _rootType
        Specified root serialization type to use; can be same as runtime type, but usually one of its super types
      • _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.
        Since:
        1.8
    • 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
        Since:
        1.6
      • withView

        public ObjectWriter withView​(java.lang.Class<?> view)
        Method that will construct a new instance that uses specified serialization view for serialization (with null basically disables view processing)
      • 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.
      • withType

        public ObjectWriter withType​(java.lang.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.
      • withPrettyPrinter

        public 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)
        Since:
        1.6
      • withDefaultPrettyPrinter

        public ObjectWriter withDefaultPrettyPrinter()
        Method that will construct a new instance that will use the default pretty printer for serialization.
        Since:
        1.6
      • withFilters

        public ObjectWriter withFilters​(FilterProvider filterProvider)
        Method that will construct a new instance that uses specified provider for resolving filter instances by id.
        Since:
        1.7
      • withDateFormat

        public ObjectWriter withDateFormat​(java.text.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.
        Since:
        1.9
      • writeValue

        public void writeValue​(java.io.Writer w,
                               java.lang.Object value)
                        throws java.io.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:
        java.io.IOException
        JsonGenerationException
        JsonMappingException
      • canSerialize

        public boolean canSerialize​(java.lang.Class<?> type)