3

I'm trying to serialize and deserialize a guava's multimap using XStream and Jettison. Here's a simple test to illustrate:

    final XStream xstream = new XStream(new JettisonMappedXmlDriver());
    final Multimap<TestEnum, String> test = HashMultimap.create();
    test.put(TestEnum.E1, "test");
    final String json = xstream.toXML(test);
    final Multimap<TestEnum, String> result = (Multimap<TestEnum, String>)xstream.fromXML(json);

It gives the following error:

com.thoughtworks.xstream.converters.ConversionException: Could not call com.google.common.collect.HashMultimap.readObject() : com.test.Test$TestEnum cannot be cast to java.lang.Integer
---- Debugging information ----
message             : Could not call com.google.common.collect.HashMultimap.readObject()
cause-exception     : java.lang.ClassCastException
cause-message       : com.test.Test$TestEnum cannot be cast to java.lang.Integer
class               : com.google.common.collect.HashMultimap
required-type       : com.google.common.collect.HashMultimap
converter-type      : com.thoughtworks.xstream.converters.reflection.SerializableConverter
path                : /com.google.common.collect.HashMultimap/com.google.common.collect.HashMultimap
line number         : -1
version             : 1.4.7
-------------------------------

Note that this error especially focus on Multimap when used with an enum key. If I use Map instead of multimap, there's no error. If I use String instead of Enum as key, there's no error. Also, if I serialize to XML instead of JSON (that is, without "JettisonMappedXmlDriver" in constructor), it works perfectly.

Is there a good solution for this? I'm currently using a workaround, replacing my multimap with a map of collection, but I would prefer to find a way to keep multimap.

Joel
  • 2,374
  • 17
  • 26

1 Answers1

0

The solution serialized a Multimap with XStream is to use Multimap converter and register to XStream, like this:

public class MultimapConverter extends AbstractCollectionConverter {

    public MultimapConverter(Mapper mapper) {
        super(mapper);
    }

    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
        return Multimap.class.isAssignableFrom(clazz);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void marshal(
            Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        Multimap map = (Multimap) value;
        for (Object key : map.keySet()) {
            for (Object v : map.get(key)) {
                if (v != null) {
                    writer.startNode("entry");
                    writer.addAttribute("key", key.toString());
                    writeCompleteItem(v, context, writer);
                    writer.endNode();
                }
            }
        }
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        ArrayListMultimap<Object, Object> map = ArrayListMultimap.create();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String key = reader.getAttribute("key");
            Object value = null;
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                value = readBareItem(reader, context, map);
                reader.moveUp();
            }
            reader.moveUp();

            if (value != null) {
                map.put(key, value);
            }
        }
        return map;
    }

}

Then register it to the XStream serializer you have, for example:

XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.registerConverter(new MultimapConverter(xstream.getMapper()));
xstream.allowTypeHierarchy(Multimap.class);
xstream.addDefaultImplementation(ArrayListMultimap.class, Multimap.class);

As for the error on deserializing the above converter works as expected:

@Test
public void testSerializeWithEnum() {
   XStream xstream = new XStream(new JettisonMappedXmlDriver());
   xstream.registerConverter(new MultimapConverter(xstream.getMapper()));
   xstream.allowTypeHierarchy(Multimap.class);
   xstream.addDefaultImplementation(ArrayListMultimap.class, Multimap.class);
   Multimap<TestEnum, String> test = HashMultimap.create();
   test.put(TestEnum.E1, "test");
   String json = xstream.toXML(test);
   final Multimap<TestEnum, String> result = (Multimap<TestEnum, String>)xstream.fromXML(json);
}

public enum TestEnum {
    E1
}

If you put a breakpoint and debug the result variable here you would see that the JSON was deserialized with the value of the enum.

quarks
  • 33,478
  • 73
  • 290
  • 513