未找到类型列表选项的 Json 解串器
No Json deserializer found for type List Option
如何使用 play json OFormat 宏来获取选项列表?
val text = """[{"name": "John", "age": 30}, null, {"name": "Steve", "age": 34}]"""
import play.api.libs.json.Json
case class Person(name: String, age: Int)
implicit val personFormat = Json.format[Person]
val data = Json.parse(text).validate[List[Option[Person]]]
// Error: No Json deserializer found for type List[Option[Person]]. Try to implement an implicit Reads or Format for this type.
作为解决方法,我正在执行以下操作:
val data = Json.parse(text).as[Array[JsValue]].toList.map {
case JsNull => None
case x => Some(x.validate[Person].get)
}
println(data)
// List(Some(Person(John,30)), None, Some(Person(Steve,34)))
如果没有此解决方法,我如何仅使用 OFormat 宏来实现同样的目标?
不确定是否可以直接这样做,但是可以这样做(例如使用 回答):
val text = """[{"name": "John", "age": 30}, null, {"name": "Steve", "age": 34}]"""
import play.api.libs.json._
implicit def optionFormat[T: Format]: Format[Option[T]] = new Format[Option[T]]{
override def reads(json: JsValue): JsResult[Option[T]] = json.validateOpt[T]
override def writes(o: Option[T]) = o match {
case Some(t) ⇒ implicitly[Writes[T]].writes(t)
case None ⇒ JsNull
}
}
case class Person(name: String, age: Int)
implicit val personFormat= {
implicit val f = Json.format[Person]
implicitly[Format[Option[Person]]]
}
val data = Json.parse(text).validate[List[Option[Person]]] // JsSuccess(List(Some(Person(John,30)), None, Some(Person(Steve,34))),)
Json.toJson(data.get) // [{"name":"John","age":30},null,{"name":"Steve","age":34}]
它无法自行翻译 Reads[Person]
-> Reads[Option[Person]]
-> Reads[List[Option[Person]]]
。我确实使用辅助通用方法帮助获得 Reads[Option[Person]]
。可能,模拟方法在播放库中可用..
如何使用 play json OFormat 宏来获取选项列表?
val text = """[{"name": "John", "age": 30}, null, {"name": "Steve", "age": 34}]"""
import play.api.libs.json.Json
case class Person(name: String, age: Int)
implicit val personFormat = Json.format[Person]
val data = Json.parse(text).validate[List[Option[Person]]]
// Error: No Json deserializer found for type List[Option[Person]]. Try to implement an implicit Reads or Format for this type.
作为解决方法,我正在执行以下操作:
val data = Json.parse(text).as[Array[JsValue]].toList.map {
case JsNull => None
case x => Some(x.validate[Person].get)
}
println(data)
// List(Some(Person(John,30)), None, Some(Person(Steve,34)))
如果没有此解决方法,我如何仅使用 OFormat 宏来实现同样的目标?
不确定是否可以直接这样做,但是可以这样做(例如使用
val text = """[{"name": "John", "age": 30}, null, {"name": "Steve", "age": 34}]"""
import play.api.libs.json._
implicit def optionFormat[T: Format]: Format[Option[T]] = new Format[Option[T]]{
override def reads(json: JsValue): JsResult[Option[T]] = json.validateOpt[T]
override def writes(o: Option[T]) = o match {
case Some(t) ⇒ implicitly[Writes[T]].writes(t)
case None ⇒ JsNull
}
}
case class Person(name: String, age: Int)
implicit val personFormat= {
implicit val f = Json.format[Person]
implicitly[Format[Option[Person]]]
}
val data = Json.parse(text).validate[List[Option[Person]]] // JsSuccess(List(Some(Person(John,30)), None, Some(Person(Steve,34))),)
Json.toJson(data.get) // [{"name":"John","age":30},null,{"name":"Steve","age":34}]
它无法自行翻译 Reads[Person]
-> Reads[Option[Person]]
-> Reads[List[Option[Person]]]
。我确实使用辅助通用方法帮助获得 Reads[Option[Person]]
。可能,模拟方法在播放库中可用..