Serialization of Objects in Java Using XStream

Serialization is the process of transforming complex objects into a format that can be easily sent over a network or written to persistent storage. Often this format is a text format, such as XML or JSON. Later, the original object can be rebuilt from the serialized data in a process called deserialization. This makes it easier to do useful things like saving the user’s work or using the same class library in a web service that is used in its client.

1. If you have not already done so, download and install the Java Development Kit. Details are given in a previous tutorial. Make a note of the directory to which “javac.exe” is installed.

2. Download XStream, by Thoughtworks. XStream is a free serialization library for Java. As of this writing, the latest version of XStream can be downloaded from the URL ““. Once on the website, click the link named “Binary Distribution” to download the file.

3. In any convenient location, create a new folder named “SerializationTest”.

4. Uncompress the downloaded XStream archive file to any convenient location and locate the main XStream JAR file. As of this writing, the file is contained in the “lib” subdirectory, and its name is “xstream-1.4.jar”. Copy the JAR file to the SerializationTest directory.

5. In the SerializationTest directory, create a new text file named “” containing the text shown below. The code creates and configures an XStream serializer, creates some instances of complex objects, serializes them, displays the serialized xml, and then deserializes the objects.

import com.thoughtworks.xstream.*;

public class SerializationTest
    public static void main(String[] args)
        System.out.println("=========program begins");

        // build the serializer

        XStream serializerXStream = new XStream(new StaxDriver());

        serializerXStream.alias("Human", Person.class);
        serializerXStream.useAttributeFor(Person.class, "firstName");
        serializerXStream.useAttributeFor(Person.class, "middleName");
        serializerXStream.useAttributeFor(Person.class, "lastName");

        serializerXStream.alias("StreetAddress", Address.class);
        serializerXStream.useAttributeFor(Address.class, "numberAndStreet");
        serializerXStream.useAttributeFor(Address.class, "city");
        serializerXStream.useAttributeFor(Address.class, "stateCode");
        serializerXStream.useAttributeFor(Address.class, "zipCode");

        // build a person to test serialization on

        Person personBeforeSerialization = new Person
            new Address
                "123 Main Street",

            + "person before serialization is: " 
            + personBeforeSerialization.toString()
             + "\n"

        // serialize and deserialize

        String personAsXml = serializerXStream.toXML

        System.out.println("personAsXml is: " + personAsXml + "\n");

        Person personAfterDeserialization = (Person)serializerXStream.fromXML

            "person after deserialization is: " 
            + personAfterDeserialization.toString()
            + "\n"

        System.out.println("=========program ends");

class Address
    private String numberAndStreet;
    private String city;
    private String stateCode;
    private int zipCode;

    public Address
        String numberAndStreet,
        String city,
        String stateCode,
        int zipCode
        this.numberAndStreet = numberAndStreet; = city;
        this.stateCode = stateCode;
        this.zipCode = zipCode;

    public String toString()
        return this.numberAndStreet + ","
            + + ", " 
            + this.stateCode + " "
            + this.zipCode;

class Person
    private String firstName;
    private String middleName;
    private String lastName;
    private Address address;

    public Person
        String firstName, 
        String middleName, 
        String lastName,
        Address address    
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.address = address;

    public String toString()
        return this.lastName 
            + ", " 
            + this.firstName 
            + " " 
            + this.middleName
            + " @ "
            + this.address.toString();

6. Still in the SerializationTest directory, create a new text file named “ProgramBuildAndRun.bat” containing the following text. Substitute the path of the directory containing “javac.exe” in the indicated place. Note that the name of the XStream JAR file is assumed to be “xstream-1.4.jar”, which will need to be changed if a later version of XStream is being used.

set javaPath="[path of the directory containing javac.exe]"

%javaPath%\javac.exe -cp xstream-1.4.jar *.java
%javaPath%\java.exe -cp .;xstream-1.4.jar SerializationTest

Double-click the icon for ProgramBuildAndRun.bat to run it. The program will be compiled and executed, and the following output will appear.

=========program begins

person before serialization is: Public, John Quincy @ 123 Main Street,Anytown, WY 12345

personAsXml is:
person after deserialization is: Public, John Quincy @ 123 Main Street,Anytown, WY 12345

=========program ends


  • The code contains calls to XStream.alias() and .useAttributeFor(), which allow the programmer to change the format of the serialized XML to be more logical, concise, and/or readable. These calls are optional. If the default XStream object is used instead, it will simply create an XML Element for every member of the serialized object.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s