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?

FeaturePure JavaEnhanced (Sun 1.4 only)
Public classesYesYes
Non public classesNoYes
Static inner classesYesYes
Non-static inner classesNoYes
Anonymous inner classesNoYes
With default constructorYesYes
Without default constructorNoYes
Private fieldsYesYes
Final fieldsNoYes

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 = new ArrayList();
  // ...
}

class Computer {
  String type;
}

class Car {
  String color;
}

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

Person person = 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:

<person>
  <name>Joe</name>
  <toys>
    <computer>
      <type>apple</type>
    </computer>
    <computer>
      <type>spectrum</type>
    </computer>
    <car>
      <color>blue</color>
    </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.