Spray-json反序列化嵌套对象 [英] Spray-json deserializing nested object

查看:77
本文介绍了Spray-json反序列化嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to deserialize nested objects correctly in 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帮助序列化为字段映射(带有嵌套对象的嵌套映射)?

On a side-note, how to spray-json help serializing to a map of fields (with nested map for nested objects)?

推荐答案

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

The example below demonstrates JSON -> Abstract Syntax Tree -> Scala Case Classes and back with custom field names and support for optional case class members. The example is derived from the spray-json documentation at https://github.com/spray/spray-json for version 1.2.5.

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])
}

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

The example outputs the following when executed:

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)

这篇关于Spray-json反序列化嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆