Package org.codehaus.jackson.map
Class ObjectWriter
- java.lang.Object
-
- org.codehaus.jackson.map.ObjectWriter
-
- All Implemented Interfaces:
Versioned
public class ObjectWriter extends 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 byObjectMapperand can be reused in completely thread-safe manner with no explicit synchronization- Since:
- 1.5
- Author:
- tatu
-
-
Field Summary
Fields Modifier and Type Field Description protected SerializationConfig_configGeneral serialization configuration settingsprotected JsonFactory_jsonFactoryFactory used for constructingJsonGeneratorsprotected PrettyPrinter_prettyPrinterTo allow for dynamic enabling/disabling of pretty printing, pretty printer can be optionally configured for writer as wellprotected SerializerProvider_providerprotected JavaType_rootTypeSpecified root serialization type to use; can be same as runtime type, but usually one of its super typesprotected FormatSchema_schemaWhen using data format that uses a schema, schema is passed to generator.protected SerializerFactory_serializerFactoryprotected static PrettyPrinterNULL_PRETTY_PRINTERWe need to keep track of explicit disabling of pretty printing; easiest to do by a token value.
-
Constructor Summary
Constructors Modifier Constructor Description protectedObjectWriter(ObjectMapper mapper, SerializationConfig config)Alternative constructor for initial instantiation.protectedObjectWriter(ObjectMapper mapper, SerializationConfig config, FormatSchema s)Alternative constructor for initial instantiation.protectedObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp)Constructor used byObjectMapperfor initial instantiationprotectedObjectWriter(ObjectWriter base, SerializationConfig config)Copy constructor used for building variations.protectedObjectWriter(ObjectWriter base, SerializationConfig config, JavaType rootType, PrettyPrinter pp, FormatSchema s)Copy constructor used for building variations.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_configAndWriteValue(JsonGenerator jgen, Object value)Method called to configure the generator as necessary and then call write functionalitybooleancanSerialize(Class<?> type)Versionversion()Method that will return version information stored in and read from jar that contains this class.ObjectWriterwithDateFormat(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.ObjectWriterwithDefaultPrettyPrinter()Method that will construct a new instance that will use the default pretty printer for serialization.ObjectWriterwithFilters(FilterProvider filterProvider)Method that will construct a new instance that uses specified provider for resolving filter instances by id.ObjectWriterwithPrettyPrinter(PrettyPrinter pp)Method that will construct a new instance that will use specified pretty printer (or, if null, will not do any pretty-printing)ObjectWriterwithSchema(FormatSchema schema)ObjectWriterwithType(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.ObjectWriterwithType(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.ObjectWriterwithType(TypeReference<?> rootType)ObjectWriterwithView(Class<?> view)Method that will construct a new instance that uses specified serialization view for serialization (with null basically disables view processing)voidwriteValue(File resultFile, Object value)Method that can be used to serialize any Java value as JSON output, written to File provided.voidwriteValue(OutputStream out, Object value)Method that can be used to serialize any Java value as JSON output, using output stream provided (using encodingJsonEncoding.UTF8).voidwriteValue(Writer w, Object value)Method that can be used to serialize any Java value as JSON output, using Writer provided.voidwriteValue(JsonGenerator jgen, Object value)Method that can be used to serialize any Java value as JSON output, using providedJsonGenerator.byte[]writeValueAsBytes(Object value)Method that can be used to serialize any Java value as a byte array.StringwriteValueAsString(Object value)Method that can be used to serialize any Java value as a String.
-
-
-
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
-
_provider
protected final SerializerProvider _provider
-
_serializerFactory
protected final SerializerFactory _serializerFactory
-
_jsonFactory
protected final JsonFactory _jsonFactory
Factory used for constructingJsonGenerators
-
_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
-
-
Constructor Detail
-
ObjectWriter
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp)
Constructor used byObjectMapperfor initial instantiation
-
ObjectWriter
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config)
Alternative constructor for initial instantiation.- Since:
- 1.7
-
ObjectWriter
protected ObjectWriter(ObjectMapper mapper, SerializationConfig config, FormatSchema s)
Alternative constructor for initial instantiation.- Since:
- 1.7
-
ObjectWriter
protected ObjectWriter(ObjectWriter base, SerializationConfig config, JavaType rootType, PrettyPrinter pp, FormatSchema s)
Copy constructor used for building variations.
-
ObjectWriter
protected ObjectWriter(ObjectWriter base, SerializationConfig config)
Copy constructor used for building variations.- Since:
- 1.7
-
-
Method Detail
-
version
public Version version()
Method that will return version information stored in and read from jar that contains this class.
-
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)
-
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(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)
- Since:
- 1.7
-
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
-
withSchema
public ObjectWriter withSchema(FormatSchema schema)
- Since:
- 1.8
-
withDateFormat
public 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.- Since:
- 1.9
-
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 providedJsonGenerator.
-
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.
-
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 encodingJsonEncoding.UTF8).Note: method does not close the underlying stream explicitly here; however,
JsonFactorythis mapper uses may choose to close the stream depending on its settings (by default, it will try to close it whenJsonGeneratorwe construct is closed).
-
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,
JsonFactorythis mapper uses may choose to close the stream depending on its settings (by default, it will try to close it whenJsonGeneratorwe construct is closed).
-
writeValueAsString
public String writeValueAsString(Object value) throws IOException, JsonGenerationException, JsonMappingException
Method that can be used to serialize any Java value as a String. Functionally equivalent to callingwriteValue(Writer,Object)withStringWriterand constructing String, but more efficient.
-
writeValueAsBytes
public byte[] writeValueAsBytes(Object value) throws IOException, JsonGenerationException, JsonMappingException
Method that can be used to serialize any Java value as a byte array. Functionally equivalent to callingwriteValue(Writer,Object)withByteArrayOutputStreamand getting bytes, but more efficient. Encoding used will be UTF-8.
-
canSerialize
public boolean canSerialize(Class<?> type)
-
_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
-
-