XStream序列化对象,java.util.Map自定义Converter
2013-05-20 10:50
344 查看
如果你不知道XStream是什么东东,请看我以前的一篇文章:
http://fuliang.iteye.com/blog/379429
今天用XStream序列化对象,其中有Map类型的字段,结果不让人满意:
Xml代码
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property>
<string>region</string>
<string>非洲中东</string>
</property>
<property>
<string>routeReference</string>
<string>首都机场集合,搭乘土耳其航空直飞伊斯坦布尔,夜宿机上。</string>
</property>
<property>
<string>routeDays</string>
<string>10</string>
</property>
<property>
<string>price</string>
<string>¥13500起</string>
</property>
<property>
<string>subject</string>
<string>常规</string>
</property>
<property>
<string>dayOfBookingExpired</string>
<string>2010-02-01</string>
</property>
<property>
<string>departure</string>
<string>北京</string>
</property>
<property>
<string>dayOfDeparture</string>
<string>2010-02-10</string>
</property>
<property>
<string>title</string>
<string>[团队游]埃及土耳其10日千年文明之旅(春节)</string>
</property>
</properties>
</DataRecord>
</dataRecords>
lt;/ExtractResult>
其中的key和value被序列化成:
Xml代码
<string>region</string>
<string>非洲中东</string>
我比较想要的结果是
Java代码
<property key="region" value="非洲中东"/>
查看了XStream的文档,可以通过自定义Converter来转换,经过尝试,只需要很简单的步骤就可以完成:
Java代码
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
/**
* @author fuliang
*
*/
public class MapCustomConverter extends AbstractCollectionConverter {
/**
* @param mapper
*/
public MapCustomConverter(Mapper mapper) {
super(mapper);
}
public boolean canConvert(Class type) {
return type.equals(HashMap.class)
|| type.equals(Hashtable.class)
|| type.getName().equals("java.util.LinkedHashMap")
|| type.getName().equals("sun.font.AttributeMap") // Used by java.awt.Font in JDK 6
;
}
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Map map = (Map) source;
for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry entry = (Entry) iterator.next();
ExtendedHierarchicalStreamWriterHelper.startNode(writer, "property", Entry.class);
writer.addAttribute("key", entry.getKey().toString());
writer.addAttribute("value", entry.getValue().toString());
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map map = (Map) createCollection(context.getRequiredType());
populateMap(reader, context, map);
return map;
}
protected void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
while (reader.hasMoreChildren()) {
reader.moveDown();
Object key = reader.getAttribute("key");
Object value = reader.getAttribute("value");
map.put(key, value);
reader.moveUp();
}
}
}
然后这么使用:
Java代码
XStream xStream = new XStream();
xStream.registerConverter(new MapCustomConverter(new DefaultMapper(XmlOutputFormatter.class.getClassLoader())));
xStream.alias("DataRecord", ExtractDataRecord.class);
xStream.alias("ExtractResult", ExtractResult.class);
xStream.alias("property", Entry.class);
return xStream.toXML(extractResult);
序列化后的结果很漂亮:
Java代码
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property key="region" value="港澳"/>
<property key="routeReference" value="搭乘国际航班直飞桃园国际机场,办理相关手续后,前往用晚餐。前往酒店入住休息。"/>
<property key="routeDays" value="8"/>
<property key="price" value="¥5680起"/>
<property key="subject" value="常规"/>
<property key="dayOfBookingExpired" value="2010-01-15"/>
<property key="departure" value="北京"/>
<property key="dayOfDeparture" value="2010-01-25"/>
<property key="title" value="[团队游]台湾8日宝岛乡情之旅"/>
</properties>
</DataRecord>
</dataRecords>
</ExtractResult>
http://fuliang.iteye.com/blog/379429
今天用XStream序列化对象,其中有Map类型的字段,结果不让人满意:
Xml代码
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property>
<string>region</string>
<string>非洲中东</string>
</property>
<property>
<string>routeReference</string>
<string>首都机场集合,搭乘土耳其航空直飞伊斯坦布尔,夜宿机上。</string>
</property>
<property>
<string>routeDays</string>
<string>10</string>
</property>
<property>
<string>price</string>
<string>¥13500起</string>
</property>
<property>
<string>subject</string>
<string>常规</string>
</property>
<property>
<string>dayOfBookingExpired</string>
<string>2010-02-01</string>
</property>
<property>
<string>departure</string>
<string>北京</string>
</property>
<property>
<string>dayOfDeparture</string>
<string>2010-02-10</string>
</property>
<property>
<string>title</string>
<string>[团队游]埃及土耳其10日千年文明之旅(春节)</string>
</property>
</properties>
</DataRecord>
</dataRecords>
lt;/ExtractResult>
其中的key和value被序列化成:
Xml代码
<string>region</string>
<string>非洲中东</string>
我比较想要的结果是
Java代码
<property key="region" value="非洲中东"/>
查看了XStream的文档,可以通过自定义Converter来转换,经过尝试,只需要很简单的步骤就可以完成:
Java代码
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
/**
* @author fuliang
*
*/
public class MapCustomConverter extends AbstractCollectionConverter {
/**
* @param mapper
*/
public MapCustomConverter(Mapper mapper) {
super(mapper);
}
public boolean canConvert(Class type) {
return type.equals(HashMap.class)
|| type.equals(Hashtable.class)
|| type.getName().equals("java.util.LinkedHashMap")
|| type.getName().equals("sun.font.AttributeMap") // Used by java.awt.Font in JDK 6
;
}
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Map map = (Map) source;
for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry entry = (Entry) iterator.next();
ExtendedHierarchicalStreamWriterHelper.startNode(writer, "property", Entry.class);
writer.addAttribute("key", entry.getKey().toString());
writer.addAttribute("value", entry.getValue().toString());
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map map = (Map) createCollection(context.getRequiredType());
populateMap(reader, context, map);
return map;
}
protected void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
while (reader.hasMoreChildren()) {
reader.moveDown();
Object key = reader.getAttribute("key");
Object value = reader.getAttribute("value");
map.put(key, value);
reader.moveUp();
}
}
}
然后这么使用:
Java代码
XStream xStream = new XStream();
xStream.registerConverter(new MapCustomConverter(new DefaultMapper(XmlOutputFormatter.class.getClassLoader())));
xStream.alias("DataRecord", ExtractDataRecord.class);
xStream.alias("ExtractResult", ExtractResult.class);
xStream.alias("property", Entry.class);
return xStream.toXML(extractResult);
序列化后的结果很漂亮:
Java代码
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property key="region" value="港澳"/>
<property key="routeReference" value="搭乘国际航班直飞桃园国际机场,办理相关手续后,前往用晚餐。前往酒店入住休息。"/>
<property key="routeDays" value="8"/>
<property key="price" value="¥5680起"/>
<property key="subject" value="常规"/>
<property key="dayOfBookingExpired" value="2010-01-15"/>
<property key="departure" value="北京"/>
<property key="dayOfDeparture" value="2010-01-25"/>
<property key="title" value="[团队游]台湾8日宝岛乡情之旅"/>
</properties>
</DataRecord>
</dataRecords>
</ExtractResult>
相关文章推荐
- XStream序列化对象,java.util.Map自定义Converter
- [Java]一则自定义的XStream转换器,主要用于POJO XML反序列化为Map/List
- xstream ---java序列化到xml,xml反序列化到java对象(一)
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- java hasmap对象的深复制实现:字节码复制和对象序列化成字符串复制比较。
- Web Service中Axis与DotNet互操作最佳实践:传递java.util.Map对象给C#(译)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 使用xStream对(Java对象、List集合、Map )到JSON转换、从JSON转换java对象
- 使用XStream对Java对象进行序列化和反序列化
- 使用XStream把Java 对象序列化为XML!!!
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- java 关于Map的key可不可以是自定义对象的学习
- JSON对象反序列化为Java对象的时候自定义Date类型的字符串格式
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- JAXB 序列化 java.util.Map
- XStream java对象和json文件进行序列化和反序列化
- JS自定义对象实现Java中Map对象功能的方法
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- java--对象自定义序列化简单应用,用于性能的优化
- 【redis,1】java操作redis: 将string、list、map、自定义的对象保存到redis中