Is pretty XStream serialization feasible with Scala?
Question
I'm trying out XStream as a way to quickly serialize objects to Xml or JSON to send over the wire, and deserialize. I do want the XML/JSON to be simple/clean.
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:
<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? I'd prefer to get:
<list>
<int>1</int>
<int>2</int>
<int>3</int>
</list>
Solution 2
I figured out how to write a Converter for Scala's List to get xml as shown above, see:
How can I get XStream to output Scala lists nicely? Can I write a custom converter?
OTHER TIPS
The "coloncolon" class, or ::
, which is actually called cons
, is a subclass of Scala's List
. It is used to store the actual elements of a List
. The only other List
subclass is the class of the singleton object Nil
, which represents the empty list.
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
.