[英]Is pretty XStream serialization feasible with Scala?
I'm trying out XStream as a way to quickly serialize objects to Xml or JSON to send over the wire, and deserialize. 我正在尝试使用XStream作为一种快速序列化对象到Xml或JSON以通过线路发送的方法,并反序列化。 I do want the XML/JSON to be simple/clean. 我确实希望XML / JSON简单/干净。
It seems to work well, I've added a few aliases, but now I've hit a problem, this code: 它似乎工作得很好,我添加了一些别名,但现在我遇到了一个问题,这段代码:
println(new XStream.toXML(List(1,2,3)))
produces this XML: 生成这个XML:
<scala.coloncolon serialization="custom">
<unserializable-parents/>
<scala.coloncolon>
<int>1</int>
<int>2</int>
<int>3</int>
<scala.ListSerializeEnd/>
</scala.coloncolon>
</scala.coloncolon>
I think what is going on is that the Scala List class has its own custom serialization... I wonder if there is a way to override that? 我认为发生的事情是Scala List类有自己的自定义序列化......我想知道是否有办法覆盖它? I'd prefer to get: 我宁愿得到:
<list>
<int>1</int>
<int>2</int>
<int>3</int>
</list>
The "coloncolon" class, or ::
, which is actually called cons
, is a subclass of Scala's List
. “coloncolon”类或::
,实际上称为cons
,是Scala List
的子类。 It is used to store the actual elements of a List
. 它用于存储List
的实际元素。 The only other List
subclass is the class of the singleton object Nil
, which represents the empty list. 唯一的另一个List
子类是单例对象Nil
的类,它表示空列表。
This is actually doing a reasonable job of serializing it, though it is storing the subclass name -- perhaps a problem when you desserialize it. 这实际上做了一个合理的序列化工作,虽然它存储子类名称 - 当你反序列化它时可能是一个问题。
I wonder how does it serialize Nil
. 我想知道它如何序列化Nil
。
I figured out how to write a Converter for Scala's List to get xml as shown above, see: 我想出了如何为Scala的List编写一个转换器来获取xml,如上所示,请参阅:
How can I get XStream to output Scala lists nicely? 如何让XStream很好地输出Scala列表? Can I write a custom converter? 我可以写自定义转换器吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.