Question

Hello all :) I'm about 16 hours new to Scala and Play!Framework (version 2.1). I'm following this Play!2.0 tutorial with Anorm, which uses Jerkson. From what I understand, in 2.1 you can do that out of the box, as long as you have the right JSON Formatters.

So here is the JSON service:

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

And here is 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] = {...}
}

I'm using Json.format[Bar], but it tells me he needs another formatter for anorm.Pk[Long]. I don't need the reads method, for the moment, I only want to serve the values; But the compiler needs a reads method. I'm totally at a loss at how to make it compile, let alone at how to write a good reads.

Best regards

Was it helpful?

Solution

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()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top