What is the XML serialization library for Scala?

I am looking for an xml serialization library for scala. For json serialization, I use lift-json and would like my xml serialization library to be similar, which means:

  • automatic serialization of case classes (without format definition)
  • Intelligent serialization of scala types: collections, parameters, etc.
  • the ability to define formats for other data types to customize how they are serialized
  • deserialization is not based on implicits, but rather on a class name (sometimes I only have a class / class type name that needs to be deserialized)

Do you know if such a library exists?

+1
source share
2 answers
+4
source

One of the best alternatives is to use the pure Java XStream library.

This works with case classes out of the box, with some tweaking - I use the XStreamConversions class from mixedbits-webframework - it also works with list, tuple, symbol, ListBuffer and ArrayBuffer. So this is not ideal, but you can fine-tune it to your specific needs.
Here is a small example.

import com.thoughtworks.xstream.XStream import com.thoughtworks.xstream.io.xml.StaxDriver import net.mixedbits.tools.XStreamConversions case class Bar(a:String) case class Foo(a:String,b:Int,bar:Seq[Bar]) object XStreamDemo { def main(args: Array[String]) { val xstream = XStreamConversions(new XStream(new StaxDriver())) xstream.alias("foo", classOf[Foo]) xstream.alias("bar", classOf[Bar]) val f0 = Foo("foo", 1, List(Bar("bar1"),Bar("bar2"))) val xml = xstream.toXML(f0) println(xml) val f1 = xstream.fromXML(xml) println(f1) println(f1 == f0) } } 

This leads to the following conclusion:

  <?xml version="1.0" ?><foo><a>foo</a><b>1</b><bar class="list"><bar><a>bar1</a></bar><bar><a>bar2</a></bar></bar></foo> Foo(foo,1,List(Bar(bar1), Bar(bar2))) true 

For Java 1.6 / Scala 2.9, the dependencies are the xstream.jar file and the XStreamConversions class mentioned.

+4
source

Source: https://habr.com/ru/post/902953/


All Articles