The core of XStream consists of a registry of Converters. The responsibility of a Converter is to provide a strategy for converting particular types of objects found in the object graph, to and from XML.
XStream is provided with Converters for common types such as primitives, String, File, Collections, arrays, and Dates.
java.lang (and core special types) |
|||
Converter | Supported types | Example | Notes |
---|---|---|---|
NullConverter | null values | <null/> | Usually, null fields are left out of the XML, however when used in arrays or the root object they have to be explicitly marked. |
ArrayConverter | Any kind of array |
<string-array> <string>apple</string> <string>banana</string> <string>cabbage</string> </string-array> |
This supports arrays of primitives as well as objects. It also supports multi-dimensional arrays, even if they are non-rectangular. |
BooleanConverter | boolean java.lang.Boolean |
<boolean>true</boolean> | |
ByteConverter | byte java.lang.Byte |
<byte>22</byte> | The byte is represented as an integer. |
EncodedByteArrayConverter | byte[] | <byte-array>AHiEFiEABQ==</byte-array> | Uses Base64 encoding to store binary data in XML. Converter is not automatically registered. |
CharConverter | char java.lang.Character |
<char>X</char> | |
CharArrayConverter | char[] | <char-array>hello<char-array> | Joins all characters into a single String. |
DoubleConverter | double java.lang.Double |
<double>456774543443.4553435</double> | |
FloatConverter | float java.lang.Float |
<float>4563443.435</float> | |
IntConverter | int java.lang.Integer |
<int>12345678</int> | |
LongConverter | long java.lang.Long |
<long>2344556678888786</long> | |
ShortConverter | short java.lang.Short |
<short>1445</short> | |
StringConverter | java.lang.String | <string>hello world</string> | |
StringBufferConverter | java.lang.StringBuffer | <string-buffer>hello world</string-buffer> | |
StringBuilderConverter | java.lang.StringBuilder | <string-builder>hello world</string-builder> | Available under Java 1.5 or greater. |
ThrowableConverter | java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.Error ...and all subclasses of these. |
<java.io.IOException> <detailMessage>No file</detailMessage> <stack-trace> <trace>com.x.Foo.stuff(Foo.java:22)</trace> <trace>com.x.Foo.blah(Foo.java:31)</trace> <trace>com.x.Foo.main(Foo.java:43)</trace> </stack-trace> </java.io.IOException> |
This is only available under Java 1.4 or greater. It retains the full stack trace, including that of any nested exceptions. The stack trace elements are handled by the StackTraceElementConverter. |
EnumConverter | java.lang.Enum | <java.lang.annotation.RetentionPolicy> CLASS </java.lang.annotation.RetentionPolicy> |
Available under Java 1.5 or greater. |
java.util |
|||
Converter | Supported types | Example | Notes |
CollectionConverter | java.util.ArrayList java.util.LinkedList java.util.HashSet java.util.Vector java.util.LinkedHashSet |
<linked-list> <string>apple</string> <string>banana</string> <big-decimal>12345.4555</big-decimal> </linked-list> |
The objects inside the collection can be any type of objects, including nested collections. |
MapConverter | java.util.HashMap java.util.Hashtable java.util.LinkedHashMap |
<map> <entry> <string>apple</string> <float>123.553</float> </entry> <entry> <string>orange</string> <float>55.4</float> </entry> </map> |
Both key and values can be any type of objects. |
PropertiesConverter | java.util.Properties |
<properties> <property name="host" value="localhost"/> <property name="port" value="888"/> </properties> |
Because the Properties class only accepts Strings for keys and values, the XML can be more concise.
If the Properties instance includes a set of default Properties, these are serialized in a nested <defaults> element. |
SingletonCollectionConverter | java.util.Collections.singletonList().getClass() java.util.Collections.singleton().getClass() |
<singleton-set> <string>apple</string> </singleton-set> |
The objects inside the singleton collection can be any type of objects, including nested collections. |
SingletonMapConverter | java.util.Collections.singletonMap().getClass() |
<singleton-map> <entry> <string>apple</string> <float>123.553</float> </entry> </singleton-map> |
The objects inside the singleton collection can be any type of objects, including nested collections. |
TreeMapConverter | java.util.TreeMap |
<tree-map> <comparator class="com.blah.MyComparator"/> <entry> <string>apple</string> <float>123.553</float> </entry> <entry> <string>orange</string> <float>55.4</float> </entry> </tree-map> |
This is similar to MapConverter with an additional field for storing the java.util.Comparator associated with the TreeMap. |
TreeSetConverter | java.util.TreeSet |
<tree-set> <comparator class="com.blah.MyComparator"/> <string>apple</string> <string>banana</string> <string>cabbage</string> </tree-set> |
This is similar to CollectionConverter with an additional field for storing the java.util.Comparator associated with the TreeSet. |
BitSetConverter | java.util.BitSet | <bit-set>0,1,3,5,6,8,10</bit-set> | Stores a comma separated list of which bits are set. Designed to be readable without taking up too much space. |
DateConverter | java.util.Date | <date>2004-02-22 15:16:04.0 EST</date> | |
GregorianCalendarConverter | java.util.Calendar java.util.GregorianCalendar |
<gregorian-calendar> <time>555454646</time> </gregorian-calendar> |
|
ISO8601DateConverter | java.util.Date | <date>2006-07-28T12:06:17.654-03:00</date> | Not automatically registered, needs joda-time |
ISO8601GregorianCalendarConverter | java.util.GregorianCalendar |
<gregorian-calendar> 2006-07-28T12:07:02.788-03:00 </gregorian-calendar> |
Not automatically registered, needs joda-time |
LocaleConverter | java.util.Locale | <locale>en_GB</locale> | |
CurrencyConverter | java.util.Currency | <currency>USD</currency> | Available under Java 1.4 or greater. |
UUIDConverter | java.util.UUID | <uuid>ca05f023-e07f-4956-a6ef-14ddd23df47b</uuid> | Available under Java 1.5 or greater. |
EnumMapConverter | java.util.EnumMap |
<enum-map enum-type="simple"> <entry> <simple>GREEN</simple> <string>grass</string> </entry> <entry> <simple>BLUE</simple> <string>sky</string> </entry> </enum-map> |
Available under Java 1.5 or greater. |
EnumSetConverter | java.util.EnumSet |
<enum-set enum-type="simple"> GREEN,BLUE </enum-set> |
Available under Java 1.5 or greater. |
RegexPatternConverter | java.util.regex.Pattern |
<java.util.regex.Pattern> <pattern>.*</pattern> <flags>0</flags> </java.util.regex.Pattern> |
Available under Java 1.4 or greater. |
java.sql |
|||
Converter | Supported types | Example | Notes |
SqlDateConverter | java.sql.Date | <sql-date>1978-08-25</sql-date> | |
SqlTimeConverter | java.sql.Time | <sql-time>14:07:33</sql-time> | |
SqlTimestampConverter | java.sql.Timestamp | <sql-timestamp>1970-01-01 00:00:01.234</sql-timestamp> | |
ISO8601SqlTimestampConverter | java.sql.Timestamp | <sql-timestamp>2006-07-28T12:06:17.654000000-03:00</sql-timestamp> | Not automatically registered, needs joda-time |
java.math |
|||
Converter | Supported types | Example | Notes |
BigDecimalConverter | java.math.BigDecimal | <big-decimal>342346.445332</big-decimal> | |
BigIntegerConverter | java.math.BigInteger | <big-int>23434224556</big-int> | |
java.net |
|||
Converter | Supported types | Example | Notes |
URLConverter | java.net.Url | <url>http://codehaus.org/blah</url> | |
java.io |
|||
Converter | Supported types | Example | Notes |
FileConverter | java.io.File | <file>/stuff/hello.txt</file> | |
SerializableConverter | java.io.Serializable | See description at Generalized Converters | |
ExternalizableConverter | java.io.Externalizable | See description at Generalized Converters | |
java.lang.reflect |
|||
Converter | Supported types | Example | Notes |
JavaClassConverter | java.lang.Class | <class>com.foo.MyThing</class> | This converter will look for the class in the ClassLoader of the current XStream instance. It is possible to use another ClassLoader by re-registering a new instance of this converter that is wrapped around another ClassLoader. |
JavaFieldConverter | java.lang.reflect.Field |
<field> <name>myField</name> <class>com.foo.MyThing</class> </field> |
As with the JavaClassConverter, the converter will use the ClassLoader of the XStream instance to resolve classes by default. This can be changed by specifying another ClassLoader when constructing this Converter. |
JavaMethodConverter | java.lang.reflect.Method java.lang.reflect.Constructor |
<method> <class>com.foo.MyThing</class> <name>doStuff</name> <parameter-types> <class>java.lang.String</class> <class>java.util.Iterator</class> </parameter-types> </method> |
The enclosing element for this tag will either by <method> or <constructor>. As with the JavaClassConverter, the converter will use the ClassLoader of the XStream instance to resolve classes by default. This can be changed by specifying another ClassLoader when constructing this Converter. |
DynamicProxyConverter | Any dynamic proxy generated by java.lang.reflect.Proxy |
<dynamic-proxy> <interface>com.foo.Blah</interface> <interface>com.foo.Woo</interface> <handler class="com.foo.MyHandler"> <something>blah</something> </handler> </dynamic-proxy> |
The dynamic proxy itself is not serialized, however the interfaces it implements and the actual InvocationHandler instance are serialized. This allows the proxy to be reconstructed after deserialization. |
java.awt |
|||
Converter | Supported types | Example | Notes |
ColorConverter | java.awt.Color |
<awt-color> <red>255</red> <green>255</green> <blue>255</blue> <alpha>255</alpha> </awt-color> |
Warning: The AWT toolkit is initialized when a Color is deserialized due to the internals of the Color class. This should only be an issue if deserializing a Color on a headless server process. |
FontConverter | java.awt.Font | Warning: The AWT toolkit is initialized when a Font is deserialized due to the internals of the Font class. This should only be an issue if deserializing a Font on a headless server process. | |
java.awt.font |
|||
Converter | Supported types | Example | Notes |
TextAttributeConverter | java.awt.font.TextAttribute | <awt-text-attribute>family</awt-text-attribute> | Warning: The AWT toolkit is initialized when a TextAttribute is deserialized due to the internals of the TextAttribute class. This should only be an issue if deserializing a TextAttribute on a headless server process. |
javax.swing |
|||
Converter | Supported types | Example | Notes |
LookAndFeelConverter | javax.swing.LookAndFeel implementations | Explicit mapping to the ReflectionConverter. Although the implementations of the LookAndFeel classes are serializable, they will throw an exception executing writeObject(), therefore the SerializableConverter is not used. | |
javax.security.auth |
|||
Converter | Supported types | Example | Notes |
SubjectConverter | javax.security.auth.Subject |
<auth-subject> <principals> <com.thoughtworks.xstream.Admin/> </principals> <readOnly>false</readOnly> </auth-subject> |
Available under Java 1.4 or greater. This converter does not serialize any credentials but only the principals. |
java.nio |
|||
Converter | Supported types | Example | Notes |
CharsetConverter | java.nio.charset.Charset | <charset>US-ASCII</charset> | Available under Java 1.4 or greater. |
javax.xml.datatype |
|||
Converter | Supported types | Example | Notes |
DurationConverter | javax.xml.datatype.Duration | <duration>PT1H2M</duration> | Available under Java 1.5 or greater. |
Generalized Converters |
|||
Converter | Supported types | Explanation | Notes |
ReflectionConverter | Any type | The Converter is used as fallback. It uses reflection to examine the class and will serialize its fields. | |
AnnotationReflectionConverter | Any type | The Converter is used as fallback. It uses reflection to examine the class and will serialize its fields and supports annotations for local converters. | Needs JDK 1.5, default for JDK 1.5. Deprecated since XStream 1.3 and must be registered now explicitly. |
SerializableConverter | java.io.Serializable or types with one of the serializable methods readObject or writeObject | The Converter is used for any JDK-serializable types, if not handled by a specialized Converter. | |
ExternalizableConverter | java.io.Externalizable | The Converter is used for any JDK-externalizable types, if not handled by a specialized Converter. | |
ToAttributedValueConverter | Any type with all but one fields to be represented by a single string | The Converter is used to write all but one fields of the type as attributes. The left over field is then used as current value. | Must be registered explicitly for the appropriate type |
ToStringConverter | Any type with natural String representation | The Converter must be initialized with a type, that provides a complete String representation with its toString() method and is able to be recreated by a constructor taking a single String argument. | Must be registered explicitly for the appropriate type |
JavaBeanConverter | Java beans | The Converter handles any type as Java bean. It expects the type to convert to have a public default constructor and appropriate getter and setters. | Must be registered explicitly. Should be used as fallback. |
PropertyEditorCapableConverter | Any type with a PropertyEditor implementation | The Converter can handles any type that provides a PropertyEditor implementation. The PropertyEditor's methods getAsText() and setAsText() are used to convert between object and string representation. | Must be registered explicitly for the appropriate type. |
Converters for 3rd party classes |
|||
Converter | Supported types | Explanation | Notes |
CGLIBEnhancedConverter | Proxies generated by the CGLIB Enhancer | The Converter handles proxies generated by the CGLIB Enhancer, if there were not multiple callbacks registered. A proxy with multiple callbacks can currently not be handled at all. | Must be registered explicitly in combination with the CGLIBMapper. See FAQ. |
HibernatePersistentCollectionConverter | org.hibernate.collection.PersistentBag org.hibernate.collection.PersistentList org.hibernate.collection.PersistentSet | The Converter handles Hibernate collections that are not handled by the other Hibernate converters. It will handle any element of the collection. | Must be registered explicitly in combination with the HibernateMapper and all the other Hibernate converters. See package description. |
HibernatePersistentMapConverter | org.hibernate.collection.PersistentMap | The Converter handles unsorted Hibernate maps. It will handle any key and value of the map. | Must be registered explicitly in combination with the HibernateMapper and all the other Hibernate converters. See package description. |
HibernatePersistentSortedMapConverter | org.hibernate.collection.PersistentSortedMap | The Converter handles sorted Hibernate maps. It will handle any key and value of the map. | Must be registered explicitly in combination with the HibernateMapper and all the other Hibernate converters. See package description. |
HibernatePersistentSortedSetConverter | org.hibernate.collection.PersistentSortedSet | The Converter handles sorted Hibernate sets. It will handle any element of the set. | Must be registered explicitly in combination with the HibernateMapper and all the other Hibernate converters. See package description. |
HibernateProxyConverter | org.hibernate.proxy.HibernateProxy | The Converter handle any element that is wrapped by a Hibernate proxy. | Must be registered explicitly in combination with the HibernateMapper and all the other Hibernate converters. See package description. |