Hi,
Json in Play2 is still a moving target so use it carefully for the time being.
Yes, it's based on Jerkson but you don't have to know that as you don't use Jerkson directly. The important to remind is that the Json parser is Jackson and it's good and fast.
Then the aim of current Json Api in Play2/Scala is to provide TypeClass Json Serialization inspired by SJSON (read more on this subject there:
http://debasishg.blogspot.com/2010/07/sjson-now-offers-type-class-based-json.html).
So yes, it seems a bit fastidious to have to write a formatter to de/serialize a Json case class but it's also brings the ability to customize very easily the way you manage your Json and removes Json de/serialization using Reflection which brings other problems, imply annotation usage and lessen performances too.
We are discussing on ways to make it easier on another thread. So this is a work in progress...
Yet, if you use current Play20 trunk, you can already do some interesting things.
case class Blah(blah: String)
// if you want to directly serialize/deserialize, you need to write yourself a formatter right now
implicit object BlahFormat extends Format[Blah] {
def reads(json: JsValue): Blah = Blah((json \ "blah").as[String])
def writes(p: Blah): JsValue = JsObject(List("blah" -> JsString(p.blah)))
}
def act = Action { implicit request =>
// to get a Blah object from request content
val blah = Json.parse(request.body.asText.get).as[Blah]
// to return Blah as application/json, you just have to convert your Blah to a JsValue and give it to Ok()
Ok(toJson(blah))
}
You can also do the extraction in the code directly:
def act = Action { implicit request =>
// gets a JsValue from content
val json = Json.parse(request.body.asText.get)
// extract what you need directly
val blah:String = (json\"blah").as[String]
// to return Blah as application/json, you just have to create a JsValue and give it to Ok()
Ok(JsObject(List("blah" -> JsString(blah))))
}
regards
Pascal