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>
Instead I'd like this:
<list>
<int>1</int>
<int>2</int>
<int>3</int>
</list>
Which would be similar to how the generic java collections get serialized. Whats the best way to do this?
I've got most of the way there by implementing my own converter, but I'm stuck on the unmarshal method, its not clear how to instantiate an empty list...
class ListConverter( _mapper : Mapper ) extends AbstractCollectionConverter(_mapper) {
/** Helper method to use x.getClass
*
* See: http://scalide.blogspot.com/2009/06/getanyclass-tip.html
*/
def getAnyClass(x: Any) = x.asInstanceOf[AnyRef].getClass
def canConvert( clazz: Class[_]) = {
classOf[::[_]] == clazz
}
def marshal( value: Any, writer: HierarchicalStreamWriter, context: MarshallingContext) = {
val list = value.asInstanceOf[List[_]]
for ( item <- list ) {
writeItem(item, context, writer)
}
}
def unmarshal( reader: HierarchicalStreamReader, context: UnmarshallingContext ) = {
println(context.getRequiredType())
var list : List[_] = createCollection(context.getRequiredType()).asInstanceOf[List[_]]
while (reader.hasMoreChildren()) {
reader.moveDown();
val item = readItem(reader, context, list);
list = item :: list
reader.moveUp();
}
list
}
}
object ListConverter {
def co开发者_运维百科nfigureXStream( stream: XStream ) = {
stream.alias("list", classOf[::[_]])
stream.registerConverter( new ListConverter(stream.getMapper) )
}
}
Not seconds after posting the question, the answer came to me, here is a working implementation of unmarshal:
def unmarshal( reader: HierarchicalStreamReader, context: UnmarshallingContext ) = {
var list : List[_] = Nil
while (reader.hasMoreChildren()) {
reader.moveDown();
val item = readItem(reader, context, list);
list = list ::: List(item) // be sure to build the list in the same order
reader.moveUp();
}
list
}
There is only one instance of an empty list, which is the object Nil
.
精彩评论