views:

323

answers:

2

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>
A: 

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.

Daniel
I'm trying to find an easy way to generate XML output for a webservice, and the xml generated by by cons doesn't seem appropriate for that type of use. I'm sure I could implement a Converter, I just wonder how many Converter's I'll need to implement :)
Alex Black
A: 

I figured out how to write a Converter for Scala's List to get xml as shown above, see:

http://stackoverflow.com/questions/1753511/how-can-i-get-xstream-to-output-scala-lists-nicely-can-i-write-a-custom-converte

Alex Black