简体   繁体   中英

How do I, using XStream, serialize/deserialize objects in a type hierarchy?

I am using XStream. At the moment it is not easy to replace XStream with something else.

I have an interface (MyInterface) and several sub-classes which implement that interface (in the sample code below, there is one called MyImplementation).

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:

<myInterfaceElement class="myPackage.MyImplementation">
  <field1>value1</field1>
  <field2>value2</field2>
</myInterfaceElement>

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?

Here is an example of MyInterface, MyImplementation, a JUnit test case trying to make it work. The deserializeWithClassAttribute test passes while the classAttributeSetInResult fails.


package myPackage;

public interface MyInterface {

}

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;
    }
}

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 = 
"<myInterfaceElement class=\"myPackage.MyImplementation\">\n" +
"  <field1>value1</field1>\n" +
"  <field2>value2</field2>\n" +
"</myInterfaceElement>";

        assertEquals(expectedResult, xmlResult);
    }

    @Test
    public void deserializeWithClassAttribute() {
        String inputXmlString = 
"<myInterfaceElement class=\"myPackage.MyImplementation\">\r\n" +
"  <field1>value1</field1>\r\n" +
"  <field2>value2</field2>\r\n" +
"</myInterfaceElement>";

        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);
    }
}

I figured this out by doing the following (thanks to McD on the hint to use a Converter):

  1. Add a custom Converter that extends ReflectionConverter:

     package myPackage; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.reflection.ReflectionConverter; import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.mapper.Mapper; public class MyInterfaceConverter extends ReflectionConverter { public MyInterfaceConverter(Mapper mapper, ReflectionProvider reflectionProvider) { super(mapper, reflectionProvider); } @Override public void marshal(Object original, final HierarchicalStreamWriter writer, final MarshallingContext context) { writer.addAttribute("class", original.getClass().getCanonicalName()); super.marshal(original, writer, context); } @SuppressWarnings("rawtypes") @Override public boolean canConvert(Class type) { return MyInterface.class.isAssignableFrom(type); } } 
  2. Registering the new Converter when I setup xStream:

     @Test public void classAttributeSetInResult() { MyInterface objectToSerialize = new MyImplementation("value1", "value2"); final XStream xStream = new XStream(new DomDriver()); xStream.alias("myInterfaceElement", MyImplementation.class); xStream.registerConverter(new MyInterfaceConverter(xStream.getMapper(), xStream.getReflectionProvider())); String xmlResult = xStream.toXML(objectToSerialize).toString(); String expectedResult = "<myInterfaceElement class=\\"myPackage.MyImplementation\\">\\n" + " <field1>value1</field1>\\n" + " <field2>value2</field2>\\n" + "</myInterfaceElement>"; assertEquals(expectedResult, xmlResult); } 

Hopefully this will help someone else down the road. If anyone has a better idea, please let me know!

I'd use a custom converter to solve this:

Your classes/interfaces:

public static interface MyInterface {
    public String getField1();
    public String getField2();
}

public static class MyImplementation implements MyInterface {
    public String field1;
    public String field2;

    public MyImplementation(String field1, String field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    public String getField1() { return field1; }
    public String getField2() { return field2; }
}

The rather quick & dirty Converter:

public static class MyInterfaceConverter implements Converter {
    private static final String ATTR_NAME_CLASS  = "concrete-class";
    private static final String NODE_NAME_FIELD1 = "field1";
    private static final String NODE_NAME_FIELD2 = "field2";

    public boolean canConvert(Class type)
    {
        return type.equals(MyImplementation.class);
    }

    public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context)
    {
        if (obj == null)
            // no need to save null-objects
            return;

        final String type = obj.getClass().getSimpleName();
        final MyInterface myInterface;
        if (obj instanceof MyImplementation)
            myInterface = (MyInterface) obj;
        // else if (...)
        //      ...
        else
            throw new IllegalArgumentException("Cannot convert objects of type " + obj.getClass());

        writer.addAttribute(ATTR_NAME_CLASS, type);
        marshalAttribute(writer, context, NODE_NAME_FIELD1, myInterface.getField1());
        marshalAttribute(writer, context, NODE_NAME_FIELD2, myInterface.getField2());
    }

    private static void marshalAttribute(HierarchicalStreamWriter writer, MarshallingContext context, String attrName, Object val)
    {
        if (val != null) {
            writer.startNode(attrName);
            context.convertAnother(val);
            writer.endNode();
        }
    }

    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context)
    {
        final String type = reader.getAttribute(ATTR_NAME_CLASS);
        String field1Value = null, field2Value = null;
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            if (NODE_NAME_FIELD1.equals(reader.getNodeName()))
                field1Value = (String)context.convertAnother(null, String.class);
            else if (NODE_NAME_FIELD2.equals(reader.getNodeName()))
                field2Value = (String)context.convertAnother(null, String.class);
            reader.moveUp();
        }

        if (MyImplementation.class.getSimpleName().equals(type)) {
            return new MyImplementation(field1Value, field2Value);
        }
        throw new IllegalArgumentException("Cannot unmarshal objects of type " + type);
    }
}

The test/usage/XStream-Initialization:

@Test
public void classAttributeSetInResult() {
    MyInterface objectToSerialize = new MyImplementation("value1", "value2");

    final XStream xStream = new XStream(new DomDriver());
    xStream.alias("myInterfaceElement", MyImplementation.class);
    // xStream.alias("myInterfaceElement", OtherImplementation.class);
    xStream.registerConverter(new MyInterfaceConverter());

    String xmlResult = xStream.toXML(objectToSerialize).toString();

    String expectedResult = 
"<myInterfaceElement concrete-class=\"MyImplementation\">\r\n" +
"  <field1>value1</field1>\r\n" +
"  <field2>value2</field2>\r\n" +
"</myInterfaceElement>";

    assertEquals(expectedResult, xmlResult);
}

@Test
public void deserializeWithClassAttribute() {
    String inputXmlString = 
"<myInterfaceElement concrete-class=\"MyImplementation\">\r\n" +
"  <field1>value1</field1>\r\n" +
"  <field2>value2</field2>\r\n" +
"</myInterfaceElement>";

    final XStream xStream = new XStream(new DomDriver());
    xStream.alias("myInterfaceElement", MyImplementation.class);
    // xStream.alias("myInterfaceElement", OtherImplementation.class);
    xStream.registerConverter(new MyInterfaceConverter());

    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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM