使用xstream定制java集合的序列化 [英] customising serialisation of java collections using xstream

查看:208
本文介绍了使用xstream定制java集合的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象需要序列化为XML,其中包含以下字段:

  List< String& tags = new List< String>(); 

XStream序列化正好(在某些别名之后),如下所示:

 < tags> 
< string> tagOne< / string>
< string> tagTwo< / string>
< string> tagThree< / string>
< string> tagFour< / string>
< / tags>

这样没问题,但我想能够重命名< string> 元素,例如< tag> 。我看不到一个明显的方法来做这个从XStream网站上的别名文档。 我建议修改 List< String> List< Tag> ,其中Tag是一个基本上只包含字符串的域对象。然后你说:

  xstream.alias(tag,org.goring.Tag.class); 

,你会得到你想要的。这避免了滚动自己的Converter。


I have an object that needs to be serialised as XML, which contains the following field:

List<String> tags = new List<String>();

XStream serialises it just fine (after some aliases) like this:

<tags>
  <string>tagOne</string>
  <string>tagTwo</string>
  <string>tagThree</string>
  <string>tagFour</string>
</tags>

That's OK as far as it goes, but I'd like to be able to rename the <string> elements to, say, <tag>. I can't see an obvious way to do that from the alias documentation on the XStream site. Am I missing something obvious?

解决方案

I'd suggest changing the List<String> to a List<Tag>, where Tag is a domain object that essentially just contains a String. Then you say:

xstream.alias("tag", org.goring.Tag.class);

and you get exactly what you want. This avoids having to roll your own Converter.

这篇关于使用xstream定制java集合的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆