3tty0n / play-json-xml   0.4.2

Apache License 2.0 GitHub

Converting between play-json and xml like json4s.

Scala versions: 2.12

play-json-xml

Build Status Maven Central

It converts between play-json and xml like json4s.

Install

Builds are available for 2.12.x and for 2.13.x. The main line of development of play-json-xml is 2.12.4.

libraryDependencies += "org.micchon" %% "play-json-xml" % "(version)"

Useage

If you want to convert xml to json,

import play.api.libs.json.Xml
import play.api.libs.json.implicits._
import play.api.libs.json.Json

import scala.xml._

val xml =
  <money>
    <yen>
      <price>100</price>
    </yen>
    <dol>
      <price>110</price>
    </dol>
  </money>

Xml.toJson(xml) == // or xml.toJson
  JsObject(Seq(
    "money" -> JsObject(Seq(
      "yen" -> JsObject(Seq("price" -> JsNumber(100))),
      "dol" -> JsObject(Seq("price" -> JsNumber(110)))
    ))
  ))

Or, if you want to convert json to xml,

import play.api.libs.json.Xml
import play.api.libs.json.implicits._
import play.api.libs.json.Json

val json = Json.parse(
  """
    |{
    |   "fruits":{
    |      "fruit":[
    |         {
    |            "name":"banana",
    |            "price":1000,
    |            "season":true,
    |            "delicious":true
    |         },
    |         {
    |            "name":"strowberry",
    |            "price":3000,
    |            "season":false,
    |            "delicious":true
    |         }
    |      ]
    |   }
    |}
  """.stripMargin)

Xml.toXml(json) == // or json.toXml
  <fruits><fruit><name>banana</name><price>1000</price><season>true</season><delicious>true</delicious></fruit><fruit><name>strowberry</name><price>3000</price><season>false</season><delicious>true</delicious></fruit></fruits>