Frequently Asked Questions

Compatability

Which JDK is required to use XStream?

1.3 or later.

Does XStream behave differently across different JVMs?

XStream has two modes of operation: Pure Java and Enhanced. In pure Java mode, XStream behaves in the same way across different JVMs, however it's features are limited to what reflection allows, meaning it cannot serialize certain classes or fields. In enhanced mode, XStream does not have these limitations, however this mode of operation is not available to all JVMs.

Which JVMs allow XStream to operate in enhanced mode?

Currently on Sun's 1.4 JVM and onwards. For all other JVMs, XStream should be used in pure Java mode.

What are the advantages of using enhanced mode over pure Java mode?

Feature Pure Java Enhanced (Sun 1.4 only)
Public classes Yes Yes
Non public classes No Yes
Static inner classes Yes Yes
Non-static inner classes No Yes
Anonymous inner classes No Yes
With default constructor Yes Yes
Without default constructor No Yes
Private fields Yes Yes
Final fields No Yes

Are there plans to provide enhanced mode support to other JVMs?

Yes. Let us know which JVM you would like supported.

Serialization

How do I specify that a field should not be serialized?

Make it transient.

What do serialized collections look like?

Example:

class Person {
  private String name;
  private List toys;
  // ...
}

class Computer {
  String type;
}

class Car {
  String color;
}

xstream.alias("person", Person.class);
xstream.alias("computer", Computer.class);
xstream.alias("car", Car.class);

Person joe = new Person("Joe");
person.addToy(new Computer("apple"));
person.addToy(new Computer("spectrum"));
person.addToy(new Car("blue"));

String xml = xstream.toXML(joe);

Results in:

<blockquote><person>
  <name>Joe</name>
  <toys>
    <computer>
      <type>apple</type>
    </computer>
    <computer>
      <type>spectrum</type>
    </computer>
    <car>
      <type>blue</type>
    </car>
  </toys>
</person>

How do I serialize binary data in a byte array?

You need to register a special a Converter:

xstream.registerConverter(new EncodedByteArrayConverter());

This will store binary data in XML by BASE-64 encoding it.

It is not registered by default as the encoding classes are not part of the standard JDK.

Do my classes have to implement Serializable if XStream is to serialize them?

No.

Can dynamic proxies be serialized?

Yes.

Scalability

Is XStream thread safe?

Yes. Once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently.

Document History