Spray-json反序列化嵌套对象

时间:2014-02-11 01:50:56

标签: json scala spray-json

如何在spray-json中正确反序列化嵌套对象?

    import spray.json._

    case class Person(name: String)

    case class Color(n: String, r: Int, g: Int, b: Int, p: Person)

    object MyJsonProtocol extends DefaultJsonProtocol {

      implicit object ColorJsonFormat extends RootJsonFormat[Color] {
        def write(c: Color) = JsObject(
          "color-name" -> JsString(c.n),
          "Green" -> JsNumber(c.g),
          "Red" -> JsNumber(c.r),
          "Blue" -> JsNumber(c.b),
          "person-field" -> JsObject("p-name" -> JsString(c.p.name))
        )

        def read(value: JsValue) = {
          value.asJsObject.getFields("color-name", "Red", "Green", "Blue", "person-field") match {
            case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue), JsObject(person)) =>
              Color(name, red.toInt, green.toInt, blue.toInt, null) //gotta replace null with correct deserializer
            case _ => throw new DeserializationException("Color expected")
          }
        }
      }

    }

    import MyJsonProtocol._

    val jsValue = Color("CadetBlue", 95, 158, 160, Person("guest")).toJson

    jsValue.prettyPrint

    val color = jsValue.convertTo[Color] //person is missing of course

另一方面,如何使用spray-json帮助序列化到字段映射(嵌套对象的嵌套映射)?

3 个答案:

答案 0 :(得分:16)

以下示例演示了JSON - >抽象语法树 - > Scala Case Classes返回自定义字段名称并支持可选的案例类成员。该示例源自版本1.2.5的https://github.com/spray/spray-json上的spray-json文档。

package rando

import spray.json._

case class Color(name: String, red: Int, green: Int, blue: Int)

case class Team(name: String, color: Option[Color])

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val colorFormat = jsonFormat(Color, "name", "r", "g", "b")
  implicit val teamFormat = jsonFormat(Team, "name", "jersey")
}
import MyJsonProtocol._

object GoSox extends App {
  val obj = Team("Red Sox", Some(Color("Red", 255, 0, 0)))
  val ast = obj.toJson
  println(obj)
  println(ast.prettyPrint)
  println(ast.convertTo[Team])
  println("""{ "name": "Red Sox", "jersey": null }""".asJson.convertTo[Team])
  println("""{ "name": "Red Sox" }""".asJson.convertTo[Team])
}

该示例在执行时输出以下内容:

Team(Red Sox,Some(Color(Red,255,0,0)))
{
  "name": "Red Sox",
  "jersey": {
    "name": "Red",
    "r": 255,
    "g": 0,
    "b": 0
  }
}
Team(Red Sox,Some(Color(Red,255,0,0)))
Team(Red Sox,None)
Team(Red Sox,None)

答案 1 :(得分:1)

至于您剩下的问题 - 如何在包装类型中重用JSON转换:

"person-field" -> JsObject("p-name" -> JsString(c.p.name))

我会将其更改为:

"person-field" -> p.toJson

这样,你让Person case类JSONify本身,而不是在包装对象中引入另一种方式。 DRY更简单。

case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue), JsObject(person)) => 
  Color(name, red.toInt, green.toInt, blue.toInt, null)

使用此处的.convertTo[Person]

case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue), jsv) => 
  Color(name, red.toInt, green.toInt, blue.toInt, jsv.convertTo[Person])

如果有问题,请寻求更多帮助。我在自己的项目中有类似的结构,但没有尝试在这种情况下运行它们。

答案 2 :(得分:0)

import models.{Address, Patient}
import spray.json._

object Hospital {

  object MyJsonProtocol extends DefaultJsonProtocol {
    implicit val address = jsonFormat(Address, "country", "state", "zip")
    implicit val patient = jsonFormat(Patient, "name", "regNumber", "address")
  }

  import MyJsonProtocol._

  def main(args: Array[String]): Unit = {
    val p1 = Patient(name = "Amar", regNumber = 234, address = Address("IN", "KA", 49))
    println(p1.toJson.sortedPrint)
  }

}

输出

{
  "address": {
    "country": "IN",
    "state": "KA",
    "zip": 49
  },
  "name": "Amar",
  "regNumber": 234
}