public abstract class AbstractXStreamSerializer extends Object implements Serializer
Modifier and Type | Class and Description |
---|---|
static class |
AbstractXStreamSerializer.Builder
Abstract Builder class to instantiate
AbstractXStreamSerializer . |
Modifier | Constructor and Description |
---|---|
protected |
AbstractXStreamSerializer(AbstractXStreamSerializer.Builder builder)
Instantiate a
AbstractXStreamSerializer based on the fields contained in the AbstractXStreamSerializer.Builder . |
Modifier and Type | Method and Description |
---|---|
void |
addAlias(String name,
Class type)
Adds an alias to use instead of the fully qualified class name.
|
void |
addFieldAlias(String alias,
Class definedIn,
String fieldName)
Adds an alias to use for a given field in the given class.
|
void |
addPackageAlias(String alias,
String pkgName)
Add an alias for a package.
|
<T> boolean |
canSerializeTo(Class<T> expectedRepresentation)
Indicates whether this Serializer is capable of serializing to the given
expectedRepresentation . |
Class |
classForType(SerializedType type)
Returns the class for the given type identifier.
|
protected <S,T> T |
convert(S source,
Class<S> sourceType,
Class<T> targetType)
Convert the given
source , of type sourceType to the given targetType . |
<S,T> T |
deserialize(SerializedObject<S> serializedObject)
Deserializes the first object read from the given
bytes . |
protected abstract Object |
doDeserialize(SerializedObject serializedObject,
com.thoughtworks.xstream.XStream xStream)
Deserialize the given
serializedObject . |
protected abstract <T> T |
doSerialize(Object object,
Class<T> expectedFormat,
com.thoughtworks.xstream.XStream xStream)
Serialize the given
object to the given expectedFormat . |
Charset |
getCharset()
Returns the character set used to convert character to bytes and vice versa.
|
Converter |
getConverter()
Returns the Converter used by this serialized.
|
com.thoughtworks.xstream.XStream |
getXStream()
Returns a reference to the underlying
XStream instance, that does the actual
serialization. |
protected abstract void |
registerConverters(ChainingConverter converter)
Registers any converters that are specific to the type of content written by this serializer.
|
<T> SerializedObject<T> |
serialize(Object object,
Class<T> expectedType)
Serialize the given
object into a Serialized Object containing the given
expectedRepresentation . |
SerializedType |
typeForClass(Class type)
Returns the type identifier for the given class.
|
protected AbstractXStreamSerializer(AbstractXStreamSerializer.Builder builder)
AbstractXStreamSerializer
based on the fields contained in the AbstractXStreamSerializer.Builder
.builder
- the AbstractXStreamSerializer.Builder
used to instantiate a AbstractXStreamSerializer
instanceprotected abstract void registerConverters(ChainingConverter converter)
converter
- the Converter to register the converters withpublic <T> boolean canSerializeTo(Class<T> expectedRepresentation)
Serializer
expectedRepresentation
.
When true
, this does *not* guarantee that the serialization and (optional) conversion will also
succeed when executed. For example, when a serializer produces a byte[]
containing JSON, trying to
convert to a Dom4J Document will fail, even though this serializer has a converter to convert
byte[]
to Dom4J instances.canSerializeTo
in interface Serializer
T
- The type of data a Serialized Object should containexpectedRepresentation
- The type of data a Serialized Object should containtrue
if the expectedRepresentation
is supported, otherwise false
.public <T> SerializedObject<T> serialize(Object object, Class<T> expectedType)
Serializer
object
into a Serialized Object containing the given
expectedRepresentation
.
Use Serializer.canSerializeTo(Class)
to detect whether the expectedRepresentation
is supported by
this serializer.serialize
in interface Serializer
T
- The expected data type representing the serialized objectobject
- The object to serializeexpectedType
- The expected data type representing the serialized objectprotected abstract <T> T doSerialize(Object object, Class<T> expectedFormat, com.thoughtworks.xstream.XStream xStream)
object
to the given expectedFormat
. The subclass may use convert(Object, Class, Class)
to convert the result of the serialization to the expected type.T
- The format in which the serialized object must be returnedobject
- The object to serializeexpectedFormat
- The format in which the serialized object must be returnedxStream
- The XStream instance to serialize withprotected abstract Object doDeserialize(SerializedObject serializedObject, com.thoughtworks.xstream.XStream xStream)
serializedObject
.serializedObject
- The instance containing the serialized format of the objectxStream
- The XStream instance to deserialize withprotected <S,T> T convert(S source, Class<S> sourceType, Class<T> targetType)
source
, of type sourceType
to the given targetType
.S
- The type of data that needs to be convertedT
- The target type of the conversionsource
- The object to convertsourceType
- The source type of the conversiontargetType
- The target type of the conversionpublic <S,T> T deserialize(SerializedObject<S> serializedObject)
Serializer
bytes
. The bytes
are not consumed
from the array or modified in any way. The resulting object instance is cast to the expected type.deserialize
in interface Serializer
S
- The data type of the serialized objectT
- The expected deserialized typeserializedObject
- the instance describing the type of object and the bytes providing the serialized datapublic Class classForType(SerializedType type)
Serializer
type
is an instance of the returned Class.
If a class cannot be resolved (i.e. because the class is not available on this JVM's classpath) this method
returns an UnknownSerializedType
instance which proides access to the raw underlying data.classForType
in interface Serializer
type
- The type identifier of the objectpublic SerializedType typeForClass(Class type)
Serializer
Serializer.serialize(Object, Class)
.typeForClass
in interface Serializer
type
- Class representing the type of the serializable Object.public void addAlias(String name, Class type)
name
- The alias to usetype
- The Class to use the alias forXStream.alias(String, Class)
public void addPackageAlias(String alias, String pkgName)
alias
- The alias to use.pkgName
- The package to use the alias forXStream.aliasPackage(String, String)
public void addFieldAlias(String alias, Class definedIn, String fieldName)
alias
- The alias to use instead of the original field namedefinedIn
- The class that defines the field.fieldName
- The name of the field to use the alias forXStream.aliasField(String, Class, String)
public com.thoughtworks.xstream.XStream getXStream()
XStream
instance, that does the actual
serialization.XStream
public Charset getCharset()
public Converter getConverter()
getConverter
in interface Serializer
Copyright © 2010–2020. All rights reserved.