Search code examples
javaxmlguavaxstreambimap

Serialize BiMap with xStream


I`d like to serialize a BiMap with xStream. Since I dont like the auto generated code by xStream for a BiMap, I thought it might be a good Idea to convert a BiMap into a HashMap and serialize just the HashMap and when deserializing it, I just read in the HashMap again and convert it back to a BiMap. So I came up with the following converter strategy:

public class XStreamBiMapConverterExample
{
  public void run()
  {
    XStream xStream = new XStream();
    xStream.setMode( XStream.XPATH_ABSOLUTE_REFERENCES );
    xStream.registerConverter( new BiMapConverter(), XStream.PRIORITY_VERY_HIGH );

    final String xml = xStream.toXML( new ObjectToSerialize() );

    System.out.println( xml );

    xStream.fromXML( xml );//Reading does not work, if the BiMap of ObjectToSerialize is empty
  }

  public static void main( final String[] args )
  {
    new XStreamBiMapConverterExample().run();
  }
}


class ObjectToSerialize
{
  //  Map<String, Integer> serializeMap = new HashMap<>();
  BiMap<String, Integer> serializeMap = HashBiMap.create();

  public ObjectToSerialize()
  {
    //If there is no Values, my Converter fails. With Value there is no Problem.
    //    serializeMap.put( "Hallo", 7 );
  }
}


class BiMapConverter implements Converter
{
  @Override
  public boolean canConvert( @SuppressWarnings( "rawtypes" ) final Class type )
  {
    return BiMap.class.isAssignableFrom( type );
  }

  @Override
  public void marshal( final Object source, final HierarchicalStreamWriter writer,
                       final MarshallingContext context )
  {
    final BiMap<?, ?> biMap = (BiMap<?, ?>) source;

    final HashMap<?, ?> convertBiMapToHashMap = convertMapToHashMap( biMap );

    context.convertAnother( convertBiMapToHashMap );
  }

  private <K, V> HashMap<K, V> convertMapToHashMap( final Map<K, V> map )
  {
    final HashMap<K, V> hashMap = new HashMap<>();

    for ( Entry<K, V> entry : map.entrySet() )
    {
      hashMap.put( entry.getKey(), entry.getValue() );
    }

    return hashMap;
  }

  @Override
  public Object unmarshal( final HierarchicalStreamReader reader, final UnmarshallingContext context )
  {
    final HashMap<?, ?> serializedMap =
        (HashMap<?, ?>) context.convertAnother( reader.getValue(), HashMap.class );

    return convertMapToBiMap( serializedMap );
  }

  private <K, V> BiMap<K, V> convertMapToBiMap( final Map<K, V> map )
  {
    final BiMap<K, V> biMap = HashBiMap.create();

    for ( Entry<K, V> entry : map.entrySet() )
    {
      biMap.put( entry.getKey(), entry.getValue() );
    }

    return biMap;
  }
}

This works perfectly fine, since xStream can already convert HashMaps. Strange thing is, it only works, when there are Values within the BiMap. If the BiMap is empty, I get the following Exception, while unmarshalling the data:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: only START_TAG can have attributes END_TAG seen ...ize>\n  <serializeMap class="com.google.common.collect.HashBiMap"/>... @2:62 : only START_TAG can have attributes END_TAG seen ...ize>\n  <serializeMap class="com.google.common.collect.HashBiMap"/>... @2:62
---- Debugging information ----
message             : only START_TAG can have attributes END_TAG seen ...ize>\n  <serializeMap class="com.google.common.collect.HashBiMap"/>... @2:62
cause-exception     : java.lang.IndexOutOfBoundsException
cause-message       : only START_TAG can have attributes END_TAG seen ...ize>\n  <serializeMap class="com.google.common.collect.HashBiMap"/>... @2:62
class               : com.google.common.collect.HashBiMap
required-type       : com.google.common.collect.HashBiMap
converter-type      : BiMapConverter
path                : /ObjectToSerialize/serializeMap
line number         : 2
class[1]            : ObjectToSerialize
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : 1.4.6
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
...

The generated Output after using the Converter (when the BiMap is empty!) is the following:

<ObjectToSerialize>
  <serializeMap class="com.google.common.collect.HashBiMap"/>
</ObjectToSerialize>

Can anyone tell me, what I am doing wrong?


Solution

  • You don't need to call the reader.getValue() in the unmarshal method.

    public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
        final HashMap<?, ?> serializedMap = (HashMap<?, ?>) context.convertAnother(null, HashMap.class);
        return convertMapToBiMap(serializedMap);
    }
    

    This will work with an empty Map.