【发布时间】:2013-03-12 01:29:03
【问题描述】:
首先,我对 Play 2 Scala 还是很陌生。我正在尝试编写一个将我的模型对象转换为 JSON 的方法。
根据这个博客http://mandubian.com/2012/10/01/unveiling-play-2-dot-1-json-api-part2-writes-format-combinators/ 这是我尝试过的
case class Facility(id:Pk[Int],name:String)
object Facility{
implicit val facilityWriter = (
(__ \ "id").write[Pk[Int]] and
(__ \ "name").write[String]
)(unlift(Facility.unapply))
然后它给了我一个错误,说没有为 Pk[Int] 找到 JSON 反序列化器
所以我尝试过这样的事情(经过一番谷歌搜索)
implicit object PkFormat extends Format[Pk[Int]] {
def reads(json:JsValue): Pk[Int] = Id(json.as[Int])
def writes(id:Pk[Int]):JsNumber = JsNumber(id.get)
}
我不明白到底发生了什么,也找不到关于如何序列化/反序列化异常的示例。
【问题讨论】:
标签: json playframework-2.0 anorm