6

这是spray-json 示例。这是NullOptions特征。

问题是当我声明一个案例类时说

object MyJsonProtocol extends DefaultJsonProtocol  {
  implicit val some: RootJsonFormat[Some] = jsonFormat2(Some)
}

case class Some (
                name:String,
                age:Int
                )

并且 json 不包含字段,例如:

{
    "name":"John"
}

我得到:java.util.NoSuchElementException: key not found: age

所以我必须添加一个这样OptionNullOption特征:

object MyJsonProtocol extends DefaultJsonProtocol with NullOptions  {
  implicit val some: RootJsonFormat[Some] = jsonFormat2(Some)
}

case class Some (
                name:String,
                age:Option[Int]
                )

一切正常。但我不想有一个所有成员都是的案例类Option。有没有办法将喷雾 json 解组配置为仅设置空值而无需其他Option类型?

附言

我知道通常Option比空检查更好,但在我的情况下它只是猴子代码。

响应处理期间编组的完整示例也在这里

4

2 回答 2

2

我能想到的唯一方法是通过实现自己的协议read/write,这可能很麻烦。下面是一个简化的例子。请注意,我将 更改age为 anInteger而不是Int因为Intis an AnyVal,默认情况下不可为空。此外,我只认为该age字段可以为空,因此您可能需要根据需要采用。希望能帮助到你。

 case class Foo (name:String, age: Integer)

 object MyJsonProtocol extends DefaultJsonProtocol {
    implicit object FooJsonFormat extends RootJsonFormat[Foo] {
      def write(foo: Foo) =
        JsObject("name" -> JsString(foo.name),
                 "age"  -> Option(foo.age).map(JsNumber(_)).getOrElse(JsNull))

      def read(value: JsValue) = value match {
        case JsObject(fields) =>
          val ageOpt: Option[Integer] = fields.get("age").map(_.toString().toInt) // implicit conversion from Int to Integer
          val age: Integer = ageOpt.orNull[Integer]
          Foo(fields.get("name").get.toString(), age)
        case _ => deserializationError("Foo expected")
      }
    }
  }

  import MyJsonProtocol._
  import spray.json._

  val json = """{ "name": "Meh" }""".parseJson
  println(json.convertTo[Foo]) // prints Foo("Meh",null)
于 2015-01-19T11:17:04.280 回答
1

看来你运气不好

从您链接的文档中:

spray-json 将始终读取缺少的可选成员以及null可选成员None

您可以自定义 json 写入,但不能自定义读取。

于 2015-01-19T09:46:22.523 回答