1

大家好 :) 我刚接触 Scala 和 Play!Framework(2.1 版)大约 16 个小时。我正在使用使用 Jerkson 的 Anorm 来关注这个 Play!2.0 教程据我了解,在2.1 中,只要您拥有正确的 JSON 格式化程序,您就可以开箱即用地做到这一点。

所以这里是 JSON 服务:

def listBars() = Action {
  val bars = Bar.findAll()
  Ok(Json.toJson(bars)).as("application/json")
}

这是 Bar.scala:

case class Bar(id: Pk[Long], name: String)
object Bar {
  implicit var anormLongPkFormat = new Format[Pk[Long]] {
    def writes(key: Pk[Long]): JsValue = Json.toJson(key.toString)
    def reads(jv: JsValue): JsResult[Pk[Long]] = JsSuccess( -?- )
  }
  implicit val barFormat = Json.format[Bar]

  def findAll(): Seq[Bar] = {...}
}

我正在使用Json.format[Bar],但它告诉我他需要另一个格式化程序anorm.Pk[Long]。我不需要reads方法,目前我只想服务于价值观;但是编译器需要一个reads方法。我完全不知道如何使它编译,更不用说如何编写一个好的reads.

此致

4

1 回答 1

3

If you don't need reads now, then the easiest way is not to implement it's logic and return an error:

def reads(jv: JsValue): JsResult[Pk[Long]] = JsError()

于 2013-05-11T22:18:00.587 回答