1 5 package com.thoughtworks.xstream.converters.enums; 6 7 import com.thoughtworks.xstream.converters.collections.MapConverter; 8 import com.thoughtworks.xstream.converters.MarshallingContext; 9 import com.thoughtworks.xstream.converters.UnmarshallingContext; 10 import com.thoughtworks.xstream.mapper.Mapper; 11 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 12 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 13 import com.thoughtworks.xstream.core.util.Fields; 14 15 import java.util.EnumMap ; 16 import java.lang.reflect.Field ; 17 18 23 public class EnumMapConverter extends MapConverter { 24 25 private final Field typeField; 26 27 public EnumMapConverter(Mapper mapper) { 28 super(mapper); 29 typeField = Fields.find(EnumMap .class, "keyType"); 30 } 31 32 public boolean canConvert(Class type) { 33 return type == EnumMap .class; 34 } 35 36 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 37 Class type = (Class ) Fields.read(typeField, source); 38 writer.addAttribute(mapper().attributeForEnumType(), mapper().serializedClass(type)); 39 super.marshal(source, writer, context); 40 } 41 42 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 43 Class type = mapper().realClass(reader.getAttribute(mapper().attributeForEnumType())); 44 EnumMap map = new EnumMap (type); 45 populateMap(reader, context, map); 46 return map; 47 } 48 } 49 | Popular Tags |