[2.0-scala] How can I prevent Play! from parsing the body as JSON when Content-Type=application/json is passed as header?

105 views
Skip to first unread message

diegovarese

unread,
Sep 10, 2012, 4:19:51 PM9/10/12
to play-fr...@googlegroups.com
I would like to use the Jerkson library to automatically deserialize the body string from Json to a case class, but I've found that Play! detects the Content-Type header and generates a JsValue (more specifically, an AnyContentAsJson) as the request body. I would like Play! to not do this so I can use Jerkson to deserialize the body string to a case class without incurring the overhead of double Json parsing. Is this possible?

Guillaume Bort

unread,
Sep 10, 2012, 5:36:11 PM9/10/12
to play-fr...@googlegroups.com
Yes, just specify your own body parser.
> --
> 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/-/UtuEGAVSfG4J.
> 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.



--
Guillaume Bort, http://guillaume.bort.fr

William Barksdale

unread,
Sep 10, 2012, 11:28:32 PM9/10/12
to play-fr...@googlegroups.com
Like Monsieur Bort Says, You want to look at body parsers. I just got done puzzling these things out, and they are pretty nifty once you grok.

Here is basically what you need to know:

an Action is a trait that looks like this:

trait Action[A] extends (Request[A] => Result) {
  def parser: BodyParser[A]
}
(oooh, its formatted!)

So an Action takes a Request[A], parses the body of the request using a BodyParser[A], and then returns a result. (makes sense, thats the nature of the web after all)

The Action trait comes with a companion object, which is what you are used to using for your actions. The apply method of the Action object is what you typically use to construct a basic action. for example

def okpage = Action { request => Ok("ok") } //Action constructor takes a Function with the type Request => Result as its only argument

Here the body has already been parse according to some magical defaults that I don't know yet, but if we want to parse it using a different parser, so that the request is formatted differently, we can use a different apply method from the Action object, which is a curried function accepting first a body parser, and then the Request => Result function. It basically looks like this


def okpage = Action (parse.text) { request => Ok("body as text: " + request.body ) }

If you are wondering, as I did, where the heck `parse` came from, it is a singleton object that you picked up with the import of BodyParsers. Check out the link to see some other useful predefined body parsers.

I hope that was helpful.

diegovarese

unread,
Sep 11, 2012, 1:49:50 PM9/11/12
to play-fr...@googlegroups.com
Cool! I'll create a bodyparser then :)
Reply all
Reply to author
Forward
0 new messages