首先,我对Play 2 Scala并不是很熟悉。我正在尝试编写一个将我的模型对象转换为/从JSON的方法。
根据这篇博客http://mandubian.com/2012/10/01/unveiling-play-2-dot-1-json-api-part2-writes-format-combinators/,这是我尝试过的方法:
根据这篇博客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)
}
我不太明白具体发生了什么,也找不到一个有关如何序列化/反序列化anorm的例子。