How can I get XStream to output Scala lists nicely? Can I write a custom converter?

前端 未结 2 1115
执念已碎
执念已碎 2021-01-05 15:14

This code:

println(new XStream.toXML(List(1,2,3)))

produces this XML:


  &         


        
相关标签:
2条回答
  • 2021-01-05 15:52

    There is only one instance of an empty list, which is the object Nil.

    0 讨论(0)
  • 2021-01-05 16:11

    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
      }
    
    0 讨论(0)
提交回复
热议问题