Change from 1.0.3.2 to 1.1 when Posting a Json file to an Action

185 views
Skip to first unread message

Arthur

unread,
Dec 4, 2010, 8:36:16 PM12/4/10
to play-framework
In 1.0.3.2 I could POST a Json file to an Action. Play would then
marshal the Json into the bean that formed the parameter of the
Action. Or, if I were to renderText(params); then I'd get
{handset.imei=[Ljava.lang.String;@2389b9bd} returned. In this case
the Json being passed in was {"handset":{"imei":"1234555"}}

In 1.1 if I do a POST and use renderText(params); I get {} returned.

What changed between 1.0.3.2 and 1.1 for the Json to no longer be
marshalled, and for nothing to come through in the Params? Why did
this change? Is there a new way of doing this?

Arthur

unread,
Dec 4, 2010, 8:42:24 PM12/4/10
to play-framework
It seems that play.data.parsing.JsonParser has been removed in 1.1.
Why was this removed?

Arthur

unread,
Dec 5, 2010, 7:37:37 AM12/5/10
to play-framework
To all those reading this and wondering how to fix this, you need to
do the following:

*Create a class that extends play.data.parsing.DataParser
*Implement the public Map<String, String[]> parse(InputStream arg0)
method. This is where you write your parser, whether that's for xml
or json or your own proprietary format. Each key is then mapped to
the corresponding name in any method that receives it and the String
array as the value is passed in. If there is only one element in the
array then just that element will go through as a String.
*Add the parser to the map of Parsers when your application starts
up. This is done with the following line DataParser.parsers.put("text/
xml", new XmlParser()); if your new parser class is called XmlParser
and the content-type that you want to parse is "text/xml". You can
change that String to any content-type that you may want to parse.

I'll try and write up something clearer at some point and blog about
it as this is a very useful feature that I can see no documentation
for.

Guillaume Bort

unread,
Dec 5, 2010, 5:06:31 PM12/5/10
to play-fr...@googlegroups.com
Yes we've removed the old JSON binder because it was undocumented an
buggy. But it is easy to add again. I know that basic JSON/XML binding
is missing and we'll provide something built-in in the next release.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> 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

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Arthur

unread,
Dec 6, 2010, 5:01:10 AM12/6/10
to play-framework
Not only should this be added back in, along with XML. But there
should be some documentation on how to write your own Binders as the
ability to create your own formats could be very useful in some
circumstances.
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Gary

unread,
Dec 15, 2010, 10:51:00 AM12/15/10
to play-framework
Hi Arthur -

I am having the same problem handling posted json data - getting nulls
in the action. Did you have a write up, or could you tell me where to
add the new parser "when application starts up"?

I also checked this snippet, which also did not mention how to make
the new GsonBinder effective into the Play framework.

http://www.playframework.org/community/snippets/6

Thanks!

On Dec 5, 7:37 am, Arthur <aemble...@gmail.com> wrote:
> To all those reading this and wondering how to fix this, you need to
> do the following:
>
> *Create a class that extends play.data.parsing.DataParser
> *Implement the public Map<String, String[]> parse(InputStream arg0)
> method.   This is where you write your parser, whether that's for xml
> orjsonor your own proprietary format.  Each key is then mapped to
> the corresponding name in any method that receives it and the String
> array as the value is passed in.  If there is only one element in the
> array then just that element will go through as a String.
> *Add the parser to the map of Parsers when your application starts
> up.  This is done with the following line DataParser.parsers.put("text/
> xml", new XmlParser()); if your new parser class is called XmlParser
> and the content-type that you want to parse is "text/xml".  You can
> change that String to any content-type that you may want to parse.
>
> I'll try and write up something clearer at some point and blog about
> it as this is a very useful feature that I can see no documentation
> for.
>
> On Dec 5, 1:42 am, Arthur <aemble...@gmail.com> wrote:
>
>
>
> > It seems that play.data.parsing.JsonParser has been removed in 1.1.
> > Why was this removed?
>
> > On Dec 5, 1:36 am, Arthur <aemble...@gmail.com> wrote:
>
> > > In 1.0.3.2 I couldPOSTaJsonfile to an Action.  Play would then
> > > marshal theJsoninto the bean that formed the parameter of the
> > > Action.  Or, if I were to renderText(params); then I'd get
> > > {handset.imei=[Ljava.lang.String;@2389b9bd} returned.  In this case
> > > theJsonbeing passed in was {"handset":{"imei":"1234555"}}
>
> > > In 1.1 if I do aPOSTand use renderText(params); I get {} returned.
>
> > > What changed between 1.0.3.2 and 1.1 for theJsonto no longer be

Guillaume Bort

unread,
Dec 16, 2010, 8:45:17 AM12/16/10
to play-fr...@googlegroups.com
> I also checked this snippet, which also did not mention how to make
> the new GsonBinder effective into the Play framework.

What is the problem? The GsonBinder should be automatically discovered.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> 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

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Gary

unread,
Dec 16, 2010, 12:54:12 PM12/16/10
to play-framework
I got null exception in Application's action

My routes:

POST /users/ Application.createUser

My application action

public static void createUser(JsonObject body) {
renderText(body.getAsJsonPrimitive("name").getAsString());
}

Using curl to post data

curl -H "Accept:application/json"-H "Content-Type:application/json" -d
'{"user":{"name":"bob", "password":"secret", "fullname":"bob dunken",
"email":"b...@gmail.com"}}' -X POST http://localhost:9000/users/

Got following error in the renderText function

@64o196987
Internal Server Error (500) for request POST /users/

Execution exception (In /app/controllers/Application.java around line
29)
NullPointerException occured : null

play.exceptions.JavaExecutionException
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:285)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
at controllers.Application.createUser(Application.java:29)
at
play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:408)
at
play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:403)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:176)
... 1 more

GsonBinder is in /app/utils directory
> > For more options, visit this group athttp://groups.google.com/group/play-framework?hl=en.
>
> --
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Guillaume Bort

unread,
Dec 17, 2010, 12:44:37 PM12/17/10
to play-fr...@googlegroups.com
Ok, so your code should be:

renderText(body.getAsJsonObject("user").getAsJsonPrimitive("name").getAsString());

And also, the content-type "application/json" wasn't recognized .
Fixed in this commit:
https://github.com/playframework/play/commit/2d6195752aae57a08f307c862fa34627de31211e

> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

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

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Reply all
Reply to author
Forward
0 new messages