首先,我对Play 2 Scala很新。我试图写一个转换我的模型对象到/从JSON。Anorm的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))
然后,它给了我一个错误说没有JSON解串器发现PK [INT]
所以我用尽这样的事情(经过一些谷歌搜索)
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)
}
我不明白究竟发生了什么,并coudlnt找到一个例子o n如何序列化/反序列化anorm。
感谢您的解释..只好深挖implicits和类型类,但完全感觉值得。 – smk 2013-03-12 23:57:07