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.