title: How do I, using XStream, serialize/deserialize objects in a type hierarchy?
body: <p>I am using XStream. At the moment it is not easy to replace XStream with something else.</p>
  
  <p>I have an interface (MyInterface) and several sub-classes which implement that interface (in the sample code below, there is one called MyImplementation). </p>
  
  <p>I want to serialize and deserialize instances of the sub-classes. I found that I can deserialize just fine if I put the class attribute into the XML:</p>
  
  <pre><code>&lt;myInterfaceElement class="myPackage.MyImplementation"&gt;
    &lt;field1&gt;value1&lt;/field1&gt;
    &lt;field2&gt;value2&lt;/field2&gt;
  &lt;/myInterfaceElement&gt;
  </code></pre>
  
  <p>However, I do not know how to get XStream to write the class attribute. How can I get XStream to include the class attribute when serializing? Or is there another way to serialize/deserialize a class hierarchy so that the element name is the same for all implementations and each subclass can have their own fields defined?</p>
  
  <p>Here is an example of MyInterface, MyImplementation, a JUnit test case trying to make it work. The deserializeWithClassAttribute test passes while the classAttributeSetInResult fails.</p>
  
  <hr>
  
  <pre><code>package myPackage;
  
  public interface MyInterface {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package myPackage;
  
  public class MyImplementation implements MyInterface {
      public String field1;
      public String field2;
  
      public MyImplementation(String field1, String field2) {
          this.field1 = field1;
          this.field2 = field2;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package myPackage;
  import org.junit.Test;
  
  import com.thoughtworks.xstream.XStream;
  import com.thoughtworks.xstream.io.xml.DomDriver;
  
  import static org.junit.Assert.*;
  
  
  public class xstreamTest {
      @Test
      public void classAttributeSetInResult() {
          MyInterface objectToSerialize = new MyImplementation("value1", "value2");
  
          final XStream xStream = new XStream(new DomDriver());
          xStream.alias("myInterfaceElement", MyInterface.class);
  
          String xmlResult = xStream.toXML(objectToSerialize).toString();
  
          String expectedResult = 
  "&lt;myInterfaceElement class=\"myPackage.MyImplementation\"&gt;\n" +
  "  &lt;field1&gt;value1&lt;/field1&gt;\n" +
  "  &lt;field2&gt;value2&lt;/field2&gt;\n" +
  "&lt;/myInterfaceElement&gt;";
  
          assertEquals(expectedResult, xmlResult);
      }
  
      @Test
      public void deserializeWithClassAttribute() {
          String inputXmlString = 
  "&lt;myInterfaceElement class=\"myPackage.MyImplementation\"&gt;\r\n" +
  "  &lt;field1&gt;value1&lt;/field1&gt;\r\n" +
  "  &lt;field2&gt;value2&lt;/field2&gt;\r\n" +
  "&lt;/myInterfaceElement&gt;";
  
          final XStream xStream = new XStream(new DomDriver());
  
          MyInterface result = (MyInterface)xStream.fromXML(inputXmlString);
          assertTrue("Instance of MyImplementation returned", result instanceof MyImplementation);
  
          MyImplementation resultAsMyImplementation = (MyImplementation)result;
          assertEquals("Field 1 deserialized", "value1", resultAsMyImplementation.field1);
          assertEquals("Field 2 deserialized", "value2", resultAsMyImplementation.field2);
      }
  }
  </code></pre>