[2.0] Json response

Showing 1-6 of 6 messages
[2.0] Json response andy petrella 1/1/12 3:49 PM
Guyz,

An interesting question posted on stackoverflow, I saw several post related to the subject but the answers seems weird to the dev.


Here it is, where he is saying that creating a formatter for each model seems awkard

Implicit conversion between domain spec model and JsValue should be usable ?

Thanks guyz

PS : I tried my best to answer but I'm now a bit stuck ^^, or don't want to dig deeper in the wrong
Re: [play-framework] [2.0] Json response Ivan Meredith 1/1/12 10:06 PM
I posted a response,

Play 2 comes with Jerkson

case class Blah(blah: String)
import com.codahale.jerksHon.Json._
def act = Action { implicit request =>
    Ok(generate(parse[Blah](request.body.asText.get))).as("application/json")
}
For more information https://github.com/codahale/jerkson

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/HqB0srCKLWsJ.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

Re: [play-framework] [2.0] Json response andy petrella 1/1/12 10:49 PM
Okay thanks, but such lib should be used in the conversion method (wich might be implicit as I proposed) and withdraw from the dev actions the task to call the marshalling.

I mean that the idea would be to have Ok(Json(myObject)), where Json could be something similar to Html, that is a mvc.Content that : 
* serializes the object in json, 
* a use the json string in body, 
* and declares itself the mime type ?

More over, the https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/mvc/ContentTypes.scala already contains an AnyContentAsJson... for the reading part ?

thanks--
Andy Petrella
Belgium (Liège)
********
 Senior Developer at Erdas (http://www.erdas.com)
 IT Consultant as Freelance
********
mails :
 - andy.p...@gmail.com
 - andy.p...@erdas.com

Re: [play-framework] [2.0] Json response Pascal 1/2/12 12:45 AM
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
Re: [play-framework] [2.0] Json response andy petrella 1/2/12 12:48 AM
Oh Great!

Do you have any pointer to the separated thread you mentioned (if it is open, for reading/following at least) ?

thanks again.

andy
Re: [play-framework] [2.0] Json response Pascal 1/2/12 1:01 AM
Don't have direct link right now...
Title was : [play-framework][2.0][scala] Json potential problem? JsObject doesn't keep field order due to usage of immutable.HashMap


It's only discussions... Use first the ideas I wrote in current thread

Pascal