public class XStream
extends java.lang.Object
XStream xstream = new XStream(); String xml = xstream.toXML(myObject); // serialize to XML Object myObject2 = xstream.fromXML(xml); // deserialize from XML
To create shorter XML, you can specify aliases for classes using the alias()
method. For example, you
can shorten all occurrences of element <com.blah.MyThing>
to <my-thing>
by
registering an alias for the class.
xstream.alias("my-thing", MyThing.class);
XStream contains a map of Converter
instances, each of which acts as a
strategy for converting a particular type of class to XML and back again. Out of the box, XStream contains converters
for most basic types (String, Date, int, boolean, etc) and collections (Map, List, Set, Properties, etc). For other
objects reflection is used to serialize each field recursively.
Extra converters can be registered using the registerConverter()
method. Some non-standard converters
are supplied in the com.thoughtworks.xstream.converters.extended
package and you can create your own by
implementing the Converter
interface.
xstream.registerConverter(new SqlTimestampConverter()); xstream.registerConverter(new DynamicProxyConverter());
The converters can be registered with an explicit priority. By default they are registered with
XStream.PRIORITY_NORMAL. Converters of same priority will be used in the reverse sequence they have been registered.
The default converter, i.e. the converter which will be used if no other registered converter is suitable, can be
registered with priority XStream.PRIORITY_VERY_LOW. XStream uses by default the
ReflectionConverter
as the fallback converter.
xstream.registerConverter(new CustomDefaultConverter(), XStream.PRIORITY_VERY_LOW);
XStream has support for object graphs; a deserialized object graph will keep references intact, including circular references.
XStream can signify references in XML using either relative/absolute XPath or IDs. The mode can be changed using
setMode()
:
xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES); |
(Default) Uses XPath relative references to signify duplicate references. This produces XML with the least clutter. |
xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); |
Uses XPath absolute references to signify duplicate references. This produces XML with the least clutter. |
xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES); |
Uses XPath relative references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES); |
Uses XPath absolute references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
xstream.setMode(XStream.ID_REFERENCES); |
Uses ID references to signify duplicate references. In some scenarios, such as when using hand-written XML, this is easier to work with. |
xstream.setMode(XStream.NO_REFERENCES); |
This disables object graph support and treats the object structure like a tree. Duplicate references are treated as two separate objects and circular references cause an exception. This is slightly faster and uses less memory than the other two modes. |
The XStream instance is thread-safe. That is, once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently. Note, that this only applies if annotations are not auto-detected on-the-fly.
To avoid the need for special tags for collections, you can define implicit collections using one of the
addImplicitCollection
methods.
Modifier and Type | Class and Description |
---|---|
static class |
XStream.InitializationException
Deprecated.
As of 1.3, use
InitializationException instead |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
COLLECTION_UPDATE_LIMIT |
static java.lang.String |
COLLECTION_UPDATE_SECONDS |
static int |
ID_REFERENCES |
static int |
NO_REFERENCES |
static int |
PRIORITY_LOW |
static int |
PRIORITY_NORMAL |
static int |
PRIORITY_VERY_HIGH |
static int |
PRIORITY_VERY_LOW |
static int |
SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES |
static int |
SINGLE_NODE_XPATH_RELATIVE_REFERENCES |
static int |
XPATH_ABSOLUTE_REFERENCES |
static int |
XPATH_RELATIVE_REFERENCES |
Constructor and Description |
---|
XStream()
Constructs a default XStream.
|
XStream(HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a special
HierarchicalStreamDriver . |
XStream(ReflectionProvider reflectionProvider)
Constructs an XStream with a special
ReflectionProvider . |
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a special
HierarchicalStreamDriver and ReflectionProvider . |
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
java.lang.ClassLoader classLoader)
Deprecated.
|
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
java.lang.ClassLoader classLoader,
Mapper mapper)
Deprecated.
|
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
java.lang.ClassLoader classLoader,
Mapper mapper,
ConverterLookup converterLookup,
ConverterRegistry converterRegistry)
|
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
ClassLoaderReference classLoaderReference)
Constructs an XStream with a special
HierarchicalStreamDriver , ReflectionProvider and a
ClassLoaderReference . |
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
ClassLoaderReference classLoaderReference,
Mapper mapper)
Constructs an XStream with a special
HierarchicalStreamDriver , ReflectionProvider , a prepared
Mapper chain and the ClassLoaderReference . |
XStream(ReflectionProvider reflectionProvider,
HierarchicalStreamDriver driver,
ClassLoaderReference classLoaderReference,
Mapper mapper,
ConverterLookup converterLookup,
ConverterRegistry converterRegistry)
Constructs an XStream with a special
HierarchicalStreamDriver , ReflectionProvider , a prepared
Mapper chain, the ClassLoaderReference and an own ConverterLookup and
ConverterRegistry . |
XStream(ReflectionProvider reflectionProvider,
Mapper mapper,
HierarchicalStreamDriver driver)
Deprecated.
As of 1.3, use
XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoader, Mapper)
instead |
Modifier and Type | Method and Description |
---|---|
void |
addDefaultImplementation(java.lang.Class defaultImplementation,
java.lang.Class ofType)
Associate a default implementation of a class with an object.
|
void |
addImmutableType(java.lang.Class type)
Deprecated.
As of 1.4.9 use
addImmutableType(Class, boolean) |
void |
addImmutableType(java.lang.Class type,
boolean isReferenceable)
Add immutable types.
|
void |
addImplicitArray(java.lang.Class ownerType,
java.lang.String fieldName)
Adds an implicit array.
|
void |
addImplicitArray(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.Class itemType)
Adds an implicit array which is used for all items of the given itemType when the array type matches.
|
void |
addImplicitArray(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.String itemName)
Adds an implicit array which is used for all items of the given element name defined by itemName.
|
void |
addImplicitCollection(java.lang.Class ownerType,
java.lang.String fieldName)
Adds a default implicit collection which is used for any unmapped XML tag.
|
void |
addImplicitCollection(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.Class itemType)
Adds implicit collection which is used for all items of the given itemType.
|
void |
addImplicitCollection(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.String itemFieldName,
java.lang.Class itemType)
Adds implicit collection which is used for all items of the given element name defined by itemFieldName.
|
void |
addImplicitMap(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.Class itemType,
java.lang.String keyFieldName)
Adds an implicit map.
|
void |
addImplicitMap(java.lang.Class ownerType,
java.lang.String fieldName,
java.lang.String itemName,
java.lang.Class itemType,
java.lang.String keyFieldName)
Adds an implicit map.
|
void |
addPermission(TypePermission permission)
Add a new security permission.
|
void |
alias(java.lang.String name,
java.lang.Class type)
Alias a Class to a shorter name to be used in XML elements.
|
void |
alias(java.lang.String name,
java.lang.Class type,
java.lang.Class defaultImplementation)
Alias a Class to a shorter name to be used in XML elements.
|
void |
aliasAttribute(java.lang.Class definedIn,
java.lang.String attributeName,
java.lang.String alias)
Create an alias for an attribute.
|
void |
aliasAttribute(java.lang.String alias,
java.lang.String attributeName)
Create an alias for an attribute
|
void |
aliasField(java.lang.String alias,
java.lang.Class definedIn,
java.lang.String fieldName)
Create an alias for a field name.
|
void |
aliasPackage(java.lang.String name,
java.lang.String pkgName)
Alias a package to a shorter name to be used in XML elements.
|
void |
aliasSystemAttribute(java.lang.String alias,
java.lang.String systemAttributeName)
Create an alias for a system attribute.
|
void |
aliasType(java.lang.String name,
java.lang.Class type)
Alias a type to a shorter name to be used in XML elements.
|
void |
allowTypeHierarchy(java.lang.Class type)
Add security permission for a type hierarchy.
|
void |
allowTypes(java.lang.Class[] types)
Add security permission for explicit types.
|
void |
allowTypes(java.lang.String[] names)
Add security permission for explicit types by name.
|
void |
allowTypesByRegExp(java.util.regex.Pattern[] regexps)
Add security permission for types matching one of the specified regular expressions.
|
void |
allowTypesByRegExp(java.lang.String[] regexps)
Add security permission for types matching one of the specified regular expressions.
|
void |
allowTypesByWildcard(java.lang.String[] patterns)
Add security permission for types matching one of the specified wildcard patterns.
|
void |
autodetectAnnotations(boolean mode)
Set the auto-detection mode of the AnnotationMapper.
|
java.io.ObjectInputStream |
createObjectInputStream(HierarchicalStreamReader reader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
|
java.io.ObjectInputStream |
createObjectInputStream(HierarchicalStreamReader reader,
DataHolder dataHolder)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
|
java.io.ObjectInputStream |
createObjectInputStream(java.io.InputStream in)
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.
|
java.io.ObjectInputStream |
createObjectInputStream(java.io.Reader xmlReader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(HierarchicalStreamWriter writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(HierarchicalStreamWriter writer,
java.lang.String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(HierarchicalStreamWriter writer,
java.lang.String rootNodeName,
DataHolder dataHolder)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(java.io.OutputStream out)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(java.io.OutputStream out,
java.lang.String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(java.io.Writer writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
|
java.io.ObjectOutputStream |
createObjectOutputStream(java.io.Writer writer,
java.lang.String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
|
void |
denyPermission(TypePermission permission)
Add security permission denying another one.
|
void |
denyTypeHierarchy(java.lang.Class type)
Add security permission forbidding a type hierarchy.
|
void |
denyTypes(java.lang.Class[] types)
Add security permission forbidding explicit types.
|
void |
denyTypes(java.lang.String[] names)
Add security permission forbidding explicit types by name.
|
void |
denyTypesByRegExp(java.util.regex.Pattern[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.
|
void |
denyTypesByRegExp(java.lang.String[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.
|
void |
denyTypesByWildcard(java.lang.String[] patterns)
Add security permission forbidding types matching one of the specified wildcard patterns.
|
java.lang.Object |
fromXML(java.io.File file)
Deserialize an object from a file.
|
java.lang.Object |
fromXML(java.io.File file,
java.lang.Object root)
Deserialize an object from a file, populating the fields of the given root object instead of instantiating a new
one.
|
java.lang.Object |
fromXML(java.io.InputStream input)
Deserialize an object from an XML InputStream.
|
java.lang.Object |
fromXML(java.io.InputStream input,
java.lang.Object root)
Deserialize an object from an XML InputStream, populating the fields of the given root object instead of
instantiating a new one.
|
java.lang.Object |
fromXML(java.io.Reader reader)
Deserialize an object from an XML Reader.
|
java.lang.Object |
fromXML(java.io.Reader xml,
java.lang.Object root)
Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating
a new one.
|
java.lang.Object |
fromXML(java.lang.String xml)
Deserialize an object from an XML String.
|
java.lang.Object |
fromXML(java.lang.String xml,
java.lang.Object root)
Deserialize an object from an XML String, populating the fields of the given root object instead of instantiating
a new one.
|
java.lang.Object |
fromXML(java.net.URL url)
Deserialize an object from a URL.
|
java.lang.Object |
fromXML(java.net.URL url,
java.lang.Object root)
Deserialize an object from a URL, populating the fields of the given root object instead of instantiating a new
one.
|
java.lang.ClassLoader |
getClassLoader()
Retrieve the ClassLoader XStream uses to load classes.
|
ClassLoaderReference |
getClassLoaderReference()
Retrieve the reference to this instance' ClassLoader.
|
ConverterLookup |
getConverterLookup() |
Mapper |
getMapper()
Retrieve the
Mapper . |
ReflectionProvider |
getReflectionProvider()
Retrieve the
ReflectionProvider in use. |
void |
ignoreUnknownElements()
Ignore all unknown elements.
|
void |
ignoreUnknownElements(java.util.regex.Pattern pattern)
Add pattern for unknown element names to ignore.
|
void |
ignoreUnknownElements(java.lang.String pattern)
Add pattern for unknown element names to ignore.
|
void |
marshal(java.lang.Object obj,
HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML).
|
void |
marshal(java.lang.Object obj,
HierarchicalStreamWriter writer,
DataHolder dataHolder)
Serialize and object to a hierarchical data structure (such as XML).
|
DataHolder |
newDataHolder()
Create a DataHolder that can be used to pass data to the converters.
|
void |
omitField(java.lang.Class definedIn,
java.lang.String fieldName)
Prevents a field from being serialized.
|
void |
processAnnotations(java.lang.Class type)
Process the annotations of the given type and configure the XStream.
|
void |
processAnnotations(java.lang.Class[] types)
Process the annotations of the given types and configure the XStream.
|
void |
registerConverter(Converter converter) |
void |
registerConverter(Converter converter,
int priority) |
void |
registerConverter(SingleValueConverter converter) |
void |
registerConverter(SingleValueConverter converter,
int priority) |
void |
registerLocalConverter(java.lang.Class definedIn,
java.lang.String fieldName,
Converter converter)
Register a local
Converter for a field. |
void |
registerLocalConverter(java.lang.Class definedIn,
java.lang.String fieldName,
SingleValueConverter converter)
Register a local
SingleValueConverter for a field. |
void |
setClassLoader(java.lang.ClassLoader classLoader)
Change the ClassLoader XStream uses to load classes.
|
void |
setCollectionUpdateLimit(int maxSeconds)
Set time limit for adding elements to collections or maps.
|
void |
setMarshallingStrategy(MarshallingStrategy marshallingStrategy) |
void |
setMode(int mode)
Change mode for dealing with duplicate references.
|
protected void |
setupAliases() |
protected void |
setupConverters() |
protected void |
setupDefaultImplementations() |
static void |
setupDefaultSecurity(XStream xstream)
Deprecated.
As of 1.4.18
|
protected void |
setupImmutableTypes() |
protected void |
setupSecurity() |
java.lang.String |
toXML(java.lang.Object obj)
Serialize an object to a pretty-printed XML String.
|
void |
toXML(java.lang.Object obj,
java.io.OutputStream out)
Serialize an object to the given OutputStream as pretty-printed XML.
|
void |
toXML(java.lang.Object obj,
java.io.Writer out)
Serialize an object to the given Writer as pretty-printed XML.
|
java.lang.Object |
unmarshal(HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML).
|
java.lang.Object |
unmarshal(HierarchicalStreamReader reader,
java.lang.Object root)
Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root
object instead of instantiating a new one.
|
java.lang.Object |
unmarshal(HierarchicalStreamReader reader,
java.lang.Object root,
DataHolder dataHolder)
Deserialize an object from a hierarchical data structure (such as XML).
|
void |
useAttributeFor(java.lang.Class type)
Use an attribute for an arbitrary type.
|
void |
useAttributeFor(java.lang.Class definedIn,
java.lang.String fieldName)
Use an attribute for a field declared in a specific type.
|
void |
useAttributeFor(java.lang.String fieldName,
java.lang.Class type)
Use an attribute for a field or a specific type.
|
protected boolean |
useXStream11XmlFriendlyMapper()
Deprecated.
As of 1.4.8
|
protected MapperWrapper |
wrapMapper(MapperWrapper next) |
public static final int NO_REFERENCES
public static final int ID_REFERENCES
public static final int XPATH_RELATIVE_REFERENCES
public static final int XPATH_ABSOLUTE_REFERENCES
public static final int SINGLE_NODE_XPATH_RELATIVE_REFERENCES
public static final int SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES
public static final int PRIORITY_VERY_HIGH
public static final int PRIORITY_NORMAL
public static final int PRIORITY_LOW
public static final int PRIORITY_VERY_LOW
public static final java.lang.String COLLECTION_UPDATE_LIMIT
public static final java.lang.String COLLECTION_UPDATE_SECONDS
public XStream()
The instance will use the XppDriver
as default and tries to determine the best match for the
ReflectionProvider
on its own.
XStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider)
ReflectionProvider
.
The instance will use the XppDriver
as default.
reflectionProvider
- the reflection provider to use or null for best matching reflection providerXStream.InitializationException
- in case of an initialization problempublic XStream(HierarchicalStreamDriver hierarchicalStreamDriver)
HierarchicalStreamDriver
.
The instance will tries to determine the best match for the ReflectionProvider
on its own.
hierarchicalStreamDriver
- the driver instanceXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver)
HierarchicalStreamDriver
and ReflectionProvider
.reflectionProvider
- the reflection provider to use or null for best matching ProviderhierarchicalStreamDriver
- the driver instanceXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, Mapper mapper, HierarchicalStreamDriver driver)
XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoader, Mapper)
insteadHierarchicalStreamDriver
, ReflectionProvider
and a prepared
Mapper
chain.reflectionProvider
- the reflection provider to use or null for best matching Providermapper
- the instance with the Mapper
chain or null for the default chaindriver
- the driver instanceXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference)
HierarchicalStreamDriver
, ReflectionProvider
and a
ClassLoaderReference
.reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to the ClassLoader
to useXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, java.lang.ClassLoader classLoader)
XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference)
HierarchicalStreamDriver
, ReflectionProvider
and the
ClassLoader
to use.XStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, java.lang.ClassLoader classLoader, Mapper mapper)
XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper)
HierarchicalStreamDriver
, ReflectionProvider
, a prepared
Mapper
chain and the ClassLoader
to use.reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoader
- the ClassLoader
to usemapper
- the instance with the Mapper
chain or null for the default chainXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference, Mapper mapper)
HierarchicalStreamDriver
, ReflectionProvider
, a prepared
Mapper
chain and the ClassLoaderReference
.
The ClassLoaderReference
should also be used for the Mapper
chain.
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to the ClassLoader
to usemapper
- the instance with the Mapper
chain or null for the default chainXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, java.lang.ClassLoader classLoader, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)
XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper, ConverterLookup, ConverterRegistry)
HierarchicalStreamDriver
, ReflectionProvider
, a prepared
Mapper
chain, the ClassLoaderReference
and an own ConverterLookup
and
ConverterRegistry
.reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoader
- the ClassLoader
to usemapper
- the instance with the Mapper
chain or null for the default chainconverterLookup
- the instance that is used to lookup the convertersconverterRegistry
- an instance to manage the converter instancesXStream.InitializationException
- in case of an initialization problempublic XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)
HierarchicalStreamDriver
, ReflectionProvider
, a prepared
Mapper
chain, the ClassLoaderReference
and an own ConverterLookup
and
ConverterRegistry
.
The ClassLoaderReference should also be used for the Mapper chain. The ConverterLookup should access the
ConverterRegistry if you intent to register Converter
instances with XStream facade or you are using
annotations.
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to the ClassLoader
to usemapper
- the instance with the Mapper
chain or null for the default chainconverterLookup
- the instance that is used to lookup the convertersconverterRegistry
- an instance to manage the converter instances or null to prevent any further
registry (including annotations)XStream.InitializationException
- in case of an initialization problemprotected MapperWrapper wrapMapper(MapperWrapper next)
protected boolean useXStream11XmlFriendlyMapper()
protected void setupSecurity()
public static void setupDefaultSecurity(XStream xstream)
This method was a pure helper method for XStream 1.4.10 to 1.4.17. It initialized an XStream instance with a whitelist of well-known and simply types of the Java runtime as it is done in XStream 1.4.18 by default. This method will do therefore nothing in XStream 1.4.18 or higher.
xstream
- protected void setupAliases()
protected void setupDefaultImplementations()
protected void setupConverters()
protected void setupImmutableTypes()
public void setMarshallingStrategy(MarshallingStrategy marshallingStrategy)
public void setCollectionUpdateLimit(int maxSeconds)
InputManipulationException
is thrown, if the summed up time to add elements to collections or maps
exceeds the provided limit.
Note, that the time to add an individual element is calculated in seconds, not milliseconds. However, attacks
typically use objects with exponential growing calculation times.maxSeconds
- limit in seconds or 0 to disable checkpublic java.lang.String toXML(java.lang.Object obj)
XStreamException
- if the object cannot be serializedpublic void toXML(java.lang.Object obj, java.io.Writer out)
XStreamException
- if the object cannot be serializedpublic void toXML(java.lang.Object obj, java.io.OutputStream out)
XStreamException
- if the object cannot be serializedpublic void marshal(java.lang.Object obj, HierarchicalStreamWriter writer)
XStreamException
- if the object cannot be serializedpublic void marshal(java.lang.Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)
dataHolder
- Extra data you can use to pass to your converters. Use this as you want. If not present,
XStream shall create one lazily as needed.XStreamException
- if the object cannot be serializedpublic java.lang.Object fromXML(java.lang.String xml)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.Reader reader)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.InputStream input)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.net.URL url)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.File file)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.lang.String xml, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.Reader xml, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.net.URL url, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.File file, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object fromXML(java.io.InputStream input, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object unmarshal(HierarchicalStreamReader reader)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object unmarshal(HierarchicalStreamReader reader, java.lang.Object root)
XStreamException
- if the object cannot be deserializedpublic java.lang.Object unmarshal(HierarchicalStreamReader reader, java.lang.Object root, DataHolder dataHolder)
root
- If present, the passed in object will have its fields populated, as opposed to XStream creating a new
instance. Note, that this is a special use case! With the ReflectionConverter XStream will write
directly into the raw memory area of the existing object. Use with care!dataHolder
- Extra data you can use to pass to your converters. Use this as you want. If not present,
XStream shall create one lazily as needed.XStreamException
- if the object cannot be deserializedpublic void alias(java.lang.String name, java.lang.Class type)
name
- Short nametype
- Type to be aliasedXStream.InitializationException
- if no ClassAliasingMapper
is availablepublic void aliasType(java.lang.String name, java.lang.Class type)
name
- Short nametype
- Type to be aliasedXStream.InitializationException
- if no ClassAliasingMapper
is availablepublic void alias(java.lang.String name, java.lang.Class type, java.lang.Class defaultImplementation)
name
- Short nametype
- Type to be aliaseddefaultImplementation
- Default implementation of type to use if no other specified.XStream.InitializationException
- if no DefaultImplementationsMapper
or no ClassAliasingMapper
is
availablepublic void aliasPackage(java.lang.String name, java.lang.String pkgName)
name
- Short namepkgName
- package to be aliasedXStream.InitializationException
- if no DefaultImplementationsMapper
or no PackageAliasingMapper
is
availablepublic void aliasField(java.lang.String alias, java.lang.Class definedIn, java.lang.String fieldName)
alias
- the alias itselfdefinedIn
- the type that declares the fieldfieldName
- the name of the fieldXStream.InitializationException
- if no FieldAliasingMapper
is availablepublic void aliasAttribute(java.lang.String alias, java.lang.String attributeName)
alias
- the alias itselfattributeName
- the name of the attributeXStream.InitializationException
- if no AttributeAliasingMapper
is availablepublic void aliasSystemAttribute(java.lang.String alias, java.lang.String systemAttributeName)
null
. However, this is not reversible, i.e. deserialization of the result is likely to fail
afterwards and will not produce an object equal to the originally written one.alias
- the alias itself (may be null
)systemAttributeName
- the name of the system attributeXStream.InitializationException
- if no SystemAttributeAliasingMapper
is availablepublic void aliasAttribute(java.lang.Class definedIn, java.lang.String attributeName, java.lang.String alias)
definedIn
- the type where the attribute is definedattributeName
- the name of the attributealias
- the alias itselfXStream.InitializationException
- if no AttributeAliasingMapper
is availablepublic void useAttributeFor(java.lang.String fieldName, java.lang.Class type)
fieldName
- the name of the fieldtype
- the Class of the type to be rendered as XML attributeXStream.InitializationException
- if no AttributeMapper
is availablepublic void useAttributeFor(java.lang.Class definedIn, java.lang.String fieldName)
fieldName
- the name of the fielddefinedIn
- the Class containing such fieldXStream.InitializationException
- if no AttributeMapper
is availablepublic void useAttributeFor(java.lang.Class type)
type
- the Class of the type to be rendered as XML attributeXStream.InitializationException
- if no AttributeMapper
is availablepublic void addDefaultImplementation(java.lang.Class defaultImplementation, java.lang.Class ofType)
defaultImplementation
- ofType
- XStream.InitializationException
- if no DefaultImplementationsMapper
is availablepublic void addImmutableType(java.lang.Class type)
addImmutableType(Class, boolean)
XStream.InitializationException
- if no ImmutableTypesMapper
is availablepublic void addImmutableType(java.lang.Class type, boolean isReferenceable)
Note, while a reference-keeping marshaller will not write references for immutable types into the stream, a reference-keeping unmarshaller can still support such references in the stream for compatibility reasons at the expense of memory consumption. Therefore declare these types only as referenceable if your already persisted streams do contain such references. Otherwise you may waste a lot of memory during deserialization.
isReferenceable
- true
if support at deserialization time is required for compatibility at the
cost of a higher memory footprint, false
otherwiseXStream.InitializationException
- if no ImmutableTypesMapper
is availablepublic void registerConverter(Converter converter)
public void registerConverter(Converter converter, int priority)
public void registerConverter(SingleValueConverter converter)
public void registerConverter(SingleValueConverter converter, int priority)
public void registerLocalConverter(java.lang.Class definedIn, java.lang.String fieldName, Converter converter)
Converter
for a field.definedIn
- the class type the field is defined infieldName
- the field nameconverter
- the converter to usepublic void registerLocalConverter(java.lang.Class definedIn, java.lang.String fieldName, SingleValueConverter converter)
SingleValueConverter
for a field.definedIn
- the class type the field is defined infieldName
- the field nameconverter
- the converter to usepublic Mapper getMapper()
Mapper
. This is by default a chain of MapperWrappers
.public ReflectionProvider getReflectionProvider()
ReflectionProvider
in use.public ConverterLookup getConverterLookup()
public void setMode(int mode)
XPATH_ABSOLUTE_REFERENCES
,
XPATH_RELATIVE_REFERENCES
, XStream.ID_REFERENCES
and
XStream.NO_REFERENCES
.java.lang.IllegalArgumentException
- if the mode is not one of the declared typesXPATH_ABSOLUTE_REFERENCES
,
XPATH_RELATIVE_REFERENCES
,
ID_REFERENCES
,
NO_REFERENCES
public void addImplicitCollection(java.lang.Class ownerType, java.lang.String fieldName)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type.public void addImplicitCollection(java.lang.Class ownerType, java.lang.String fieldName, java.lang.Class itemType)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type.itemType
- type of the items to be part of this collectionXStream.InitializationException
- if no ImplicitCollectionMapper
is availablepublic void addImplicitCollection(java.lang.Class ownerType, java.lang.String fieldName, java.lang.String itemFieldName, java.lang.Class itemType)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type.itemFieldName
- element name of the implicit collectionitemType
- item type to be aliases be the itemFieldNameXStream.InitializationException
- if no ImplicitCollectionMapper
is availablepublic void addImplicitArray(java.lang.Class ownerType, java.lang.String fieldName)
ownerType
- class owning the implicit arrayfieldName
- name of the array fieldpublic void addImplicitArray(java.lang.Class ownerType, java.lang.String fieldName, java.lang.Class itemType)
ownerType
- class owning the implicit arrayfieldName
- name of the array field in the ownerTypeitemType
- type of the items to be part of this arrayXStream.InitializationException
- if no ImplicitCollectionMapper
is available or the array type does not
match the itemTypepublic void addImplicitArray(java.lang.Class ownerType, java.lang.String fieldName, java.lang.String itemName)
ownerType
- class owning the implicit arrayfieldName
- name of the array field in the ownerTypeitemName
- alias name of the itemsXStream.InitializationException
- if no ImplicitCollectionMapper
is availablepublic void addImplicitMap(java.lang.Class ownerType, java.lang.String fieldName, java.lang.Class itemType, java.lang.String keyFieldName)
ownerType
- class owning the implicit mapfieldName
- name of the field in the ownerType. This field must be a concrete map type or matching the
default implementation type of the map type.itemType
- type of the items to be part of this map as valuekeyFieldName
- the name of the field of the itemType that is used for the key in the mappublic void addImplicitMap(java.lang.Class ownerType, java.lang.String fieldName, java.lang.String itemName, java.lang.Class itemType, java.lang.String keyFieldName)
ownerType
- class owning the implicit mapfieldName
- name of the field in the ownerType. This field must be a concrete map type or matching the
default implementation type of the map type.itemName
- alias name of the itemsitemType
- type of the items to be part of this map as valuekeyFieldName
- the name of the field of the itemType that is used for the key in the mappublic DataHolder newDataHolder()
marshal(Object, HierarchicalStreamWriter, DataHolder)
,
unmarshal(HierarchicalStreamReader, Object, DataHolder)
,
createObjectInputStream(HierarchicalStreamReader, DataHolder)
or
createObjectOutputStream(HierarchicalStreamWriter, String, DataHolder)
.DataHolder
public java.io.ObjectOutputStream createObjectOutputStream(java.io.Writer writer) throws java.io.IOException
To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer) throws java.io.IOException
To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(java.io.Writer writer, java.lang.String rootNodeName) throws java.io.IOException
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(java.io.OutputStream out) throws java.io.IOException
To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(java.io.OutputStream out, java.lang.String rootNodeName) throws java.io.IOException
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer, java.lang.String rootNodeName) throws java.io.IOException
Because an ObjectOutputStream can contain multiple items and XML only allows a single root node, the stream must be written inside an enclosing node.
It is necessary to call ObjectOutputStream.close() when done, otherwise the stream will be incomplete.
ObjectOutputStream out = xstream.createObjectOutputStream(aWriter, "things"); out.writeInt(123); out.writeObject("Hello"); out.writeObject(someObject) out.close();
writer
- The writer to serialize the objects to.rootNodeName
- The name of the root node enclosing the stream of objects.java.io.IOException
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer, java.lang.String rootNodeName, DataHolder dataHolder) throws java.io.IOException
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public java.io.ObjectInputStream createObjectInputStream(java.io.Reader xmlReader) throws java.io.IOException
java.io.IOException
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
,
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
public java.io.ObjectInputStream createObjectInputStream(java.io.InputStream in) throws java.io.IOException
java.io.IOException
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
,
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
public java.io.ObjectInputStream createObjectInputStream(HierarchicalStreamReader reader) throws java.io.IOException
It is necessary to call ObjectInputStream.close() when done, otherwise the stream might keep system resources.
ObjectInputStream in = xstream.createObjectOutputStream(aReader); int a = out.readInt(); Object b = out.readObject(); Object c = out.readObject();
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
public java.io.ObjectInputStream createObjectInputStream(HierarchicalStreamReader reader, DataHolder dataHolder) throws java.io.IOException
java.io.IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public void setClassLoader(java.lang.ClassLoader classLoader)
public java.lang.ClassLoader getClassLoader()
public ClassLoaderReference getClassLoaderReference()
public void omitField(java.lang.Class definedIn, java.lang.String fieldName)
XStream.InitializationException
- if no ElementIgnoringMapper
is availablepublic void ignoreUnknownElements()
public void ignoreUnknownElements(java.lang.String pattern)
pattern
- the name pattern as regular expressionpublic void ignoreUnknownElements(java.util.regex.Pattern pattern)
pattern
- the name pattern as regular expressionpublic void processAnnotations(java.lang.Class[] types)
types
- the types with XStream annotationspublic void processAnnotations(java.lang.Class type)
type
- the type with XStream annotationspublic void autodetectAnnotations(boolean mode)
mode
- true
if annotations are auto-detectedpublic void addPermission(TypePermission permission)
Permissions are evaluated in the added sequence. An instance of NoTypePermission
or
AnyTypePermission
will implicitly wipe any existing permission.
permission
- the permission to addpublic void allowTypes(java.lang.String[] names)
names
- the type names to allowpublic void allowTypes(java.lang.Class[] types)
types
- the types to allowpublic void allowTypeHierarchy(java.lang.Class type)
type
- the base type to allowpublic void allowTypesByRegExp(java.lang.String[] regexps)
regexps
- the regular expressions to allow type namespublic void allowTypesByRegExp(java.util.regex.Pattern[] regexps)
regexps
- the regular expressions to allow type namespublic void allowTypesByWildcard(java.lang.String[] patterns)
Supported are patterns with path expressions using dot as separator:
patterns
- the patterns to allow type namespublic void denyPermission(TypePermission permission)
permission
- the permission to denypublic void denyTypes(java.lang.String[] names)
names
- the type names to forbidpublic void denyTypes(java.lang.Class[] types)
types
- the types to forbidpublic void denyTypeHierarchy(java.lang.Class type)
type
- the base type to forbidpublic void denyTypesByRegExp(java.lang.String[] regexps)
regexps
- the regular expressions to forbid type namespublic void denyTypesByRegExp(java.util.regex.Pattern[] regexps)
regexps
- the regular expressions to forbid type namespublic void denyTypesByWildcard(java.lang.String[] patterns)
Supported are patterns with path expressions using dot as separator:
patterns
- the patterns to forbid namesCopyright © 2004–2024 XStream. All rights reserved.