Phobos is an XML data-binding library based on stream parsing. It depends only on aalto-xml for parsing.
Scala 2.12 and 2.13 are supported.
Add phobos-core to your dependencies:
libraryDependencies += "ru.tinkoff" %% "phobos-core" % "0.13.2"
Then try this code out in sbt console or in a separate source file:
import ru.tinkoff.phobos.decoding._
import ru.tinkoff.phobos.encoding._
import ru.tinkoff.phobos.syntax._
import ru.tinkoff.phobos.derivation.semiauto._
case class TravelPoint(country: String, city: String)
object TravelPoint {
implicit val travelPointElementEncoder: ElementEncoder[TravelPoint] = deriveElementEncoder
implicit val travelPointElementDecoder: ElementDecoder[TravelPoint] = deriveElementDecoder
}
case class Price(@attr currency: String, @text value: Double)
object Price {
implicit val priceElementEncoder: ElementEncoder[Price] = deriveElementEncoder
implicit val priceElementDecoder: ElementDecoder[Price] = deriveElementDecoder
}
case class Journey(price: Price, departure: TravelPoint, arrival: TravelPoint)
object Journey {
implicit val journeyXmlEncoder: XmlEncoder[Journey] = deriveXmlEncoder("journey")
implicit val journeyXmlDecoder: XmlDecoder[Journey] = deriveXmlDecoder("journey")
}
val journey =
Journey(
price = Price("EUR", 1000.0),
departure = TravelPoint("France", "Marcelle"),
arrival = TravelPoint("Germany", "Munich")
)
val xml: String = XmlEncoder[Journey].encode(journey)
println(xml)
val decodedJourney = XmlDecoder[Journey].decode(xml)
println(decodedJourney)
assert(Right(journey) == decodedJourney)Please see phobos wiki for explanation of the syntax and more details.
Performance details can be found out in phobos-benchmark repository.
There are several additional modules for some specific cases. These modules could be added with command below:
libraryDependencies += "ru.tinkoff" %% "phobos-<module>" % "0.13.2
Where <module> is module name.
| Module name | Functionality |
|---|---|
| akka-http | Marshallers and unmarshallers for akka-http |
| akka-stream | Streaming decoding support for akka-stream |
| cats | Cats instances |
| derevo | Separate derivation of encoders and decoders separately using derevo annotations (e.g. @derive(xmlEncoder("foo"))) |
| enumeratum | Support for enumeratum enums |
| fs2 | Streaming decoding support (Stream[F, Array[Byte]] => G[A]). Latest fs2 version (fs2 3.x, cats effect 3.x) |
| fs2-ce2 | Streaming decoding support (Stream[F, Array[Byte]] => G[A]). Same as fs2 module, but for fs2 version 2.x using cats effect 2.x |
| monix | Streaming decoding support (Observable[Array[Byte]] => Task[A]) |
| refined | Support for refined |
It will be soon implemented in deimos library.