Best way to Deserialize JSON String to Java Object

2,568 views
Skip to first unread message

Jose Luis Estrella Campaña

unread,
Jun 16, 2010, 11:21:08 AM6/16/10
to Google Web Toolkit
Hello Folks !

I'm glad to say that I'm a brand new user of GWT, a very happy one by
the way. However, as any newcomer I have questions, One in particular.
Here it goes:

What's the easiest, most straight forward way to deserialize a JSON
String on the Server side and instance a Java Object from it ?

The JSON String will be sent from the client side, an implementation
of the RemoteService for example, and I intend to deserialize it on
the Server side, say inside the RemoteServiceServlet Implementation,
so I can instance a Java Object with the information contained in the
JSON string afterwards. Is there a way this last step automatically
with some GWT API ?

I would like to see some examples if it's possible.

Best Regards,

Sincerely,

Jose.

Chris Boertien

unread,
Jun 17, 2010, 10:01:09 PM6/17/10
to google-we...@googlegroups.com
Is there a particular reason that you _need_ the JSON string? If your
using GWT RPC then you can simply send a Java Object to the server and
the underlying GWT will handle the serialization for you.

If you the JSON string is coming from somewhere else and you really
have no choice, then it may be worthwhile to put together a JSO
Overlay, use the Overlay to instantiate a POJO on the client side
code, and send the POJO to the server via GWT RPC.

Since your new some of that probably makes no sense, and If you can
give a little more detail as to why you need to have JSON originating
from the client I'm sure this can be explained a bit better to fit
your case.

GWT-RPC: http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html
GWT-JSON: http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html#client

2010/6/16 Jose Luis Estrella Campaña <jleca...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

Jan Ehrhardt

unread,
Jun 17, 2010, 11:59:54 PM6/17/10
to google-we...@googlegroups.com
The GWT docs are offering you to use the JavaScript "eval" function directly or through the JSONParser class, which internally uses the "eval" function. A more secure way, that is to use the JSON library http://www.json.org/js.html. You'll place the additional JavaScript library in your host page, as any other JavaScript too. Than take the code from the GWT docs:


private final native JsArray<StockData> asArrayOfStockData(String json) /*-{
  return eval(json);
}-*/;

and modify it like this:


private final native JsArray<StockData> asArrayOfStockData(String json) /*-{
  return $wnd.JSON.parse(json);
}-*/;

This is more secure than using the "eval" function. Additionally the "JSON.parse()" function has become part of the JavaScript standard and is implemented natively in many modern browsers. The above library knows this and delegates the secure JSON parsing to the fast native parser provided by the browser.

Regards
Jan Ehrhardt

2010/6/18 Chris Boertien <chris.b...@gmail.com>

Jose Luis Estrella Campaña

unread,
Jun 18, 2010, 9:15:03 AM6/18/10
to Google Web Toolkit
Hello There !

I believe what Chris says is totally right, I should just use a
regular Java Object, I was mistaken when I thought I had to do JSON
parsing on the server side.
Now, I would like to know if a HashMap containing Strings only is
serializable ? Could I send it from the client to the Server and Back
(specially back)?

Thank you very much in advance !

Jose.

On Jun 17, 10:59 pm, Jan Ehrhardt <jan.ehrha...@googlemail.com> wrote:
> The GWT docs are offering you to use the JavaScript "eval" function directly
> or through the JSONParser class, which internally uses the "eval" function.
> A more secure way, that is to use the JSON libraryhttp://www.json.org/js.html. You'll place the additional JavaScript library
> in your host page, as any other JavaScript too. Than take the code from the
> GWT docs:
>
> private final native JsArray<StockData> asArrayOfStockData(String json) /*-{
>   return *eval(json);*
>
> }-*/;
>
> and modify it like this:
>
> private final native JsArray<StockData> asArrayOfStockData(String json) /*-{
>   return *$wnd.JSON.parse(json);*
>
> }-*/;
>
> This is more secure than using the "eval" function. Additionally the
> "JSON.parse()" function has become part of the JavaScript standard and is
> implemented natively in many modern browsers. The above library knows this
> and delegates the secure JSON parsing to the fast native parser provided by
> the browser.
>
> Regards
> Jan Ehrhardt
>
> 2010/6/18 Chris Boertien <chris.boert...@gmail.com>
>
>
>
> > Is there a particular reason that you _need_ the JSON string? If your
> > using GWT RPC then you can simply send a Java Object to the server and
> > the underlying GWT will handle the serialization for you.
>
> > If you the JSON string is coming from somewhere else and you really
> > have no choice, then it may be worthwhile to put together a JSO
> > Overlay, use the Overlay to instantiate a POJO on the client side
> > code, and send the POJO to the server via GWT RPC.
>
> > Since your new some of that probably makes no sense, and If you can
> > give a little more detail as to why you need to have JSON originating
> > from the client I'm sure this can be explained a bit better to fit
> > your case.
>
> > GWT-RPC:http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html
> > GWT-JSON:
> >http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html#client
>
> > 2010/6/16 Jose Luis Estrella Campaña <jlecamp...@gmail.com>:
> > > Hello Folks !
>
> > > I'm glad to say that I'm a brand new user of GWT, a very happy one by
> > > the way. However, as any newcomer I have questions, One in particular.
> > > Here it goes:
>
> > > What's the easiest, most straight forward way to deserialize a JSON
> > > String on the Server side and instance a Java Object from it ?
>
> > > The JSON String will be sent from the client side, an implementation
> > > of the RemoteService for example, and I intend to deserialize it on
> > > the Server side, say inside the RemoteServiceServlet Implementation,
> > > so I can instance a Java Object with the information contained in the
> > > JSON string afterwards. Is there a way this last step automatically
> > > with some GWT API ?
>
> > > I would like to see some examples if it's possible.
>
> > > Best Regards,
>
> > > Sincerely,
>
> > > Jose.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > > To post to this group, send email to google-we...@googlegroups.com
> > .
> > > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
> > > For more options, visit this group at
> >http://groups.google.com/group/google-web-toolkit?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .

Chris Boertien

unread,
Jun 18, 2010, 9:34:47 AM6/18/10
to google-we...@googlegroups.com
Shouldn't have a problem sending a HashMap back and forth. As far as i
can tell GWT has a fairly complete implementation of the Java
Collections API, and all of which are Serializable, so there shouldn't
be any issues. I haven't used it for much more than Lists so there
might be quirks I haven't come across yet.

2010/6/18 Jose Luis Estrella Campaña <jleca...@gmail.com>:

> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Harald Pehl

unread,
Jun 18, 2010, 10:14:13 AM6/18/10
to Google Web Toolkit
Hi Jose,

you could take a look at Piriti: http://code.google.com/p/piriti/.
It's an XML / JSON mapper for GWT which maps JSON data from the server
to POJO on the client.

- Harald

P.S. At the moment Piriti uses the "eval" function to parse JSON. This
will be changed in the next release, so that the internal JSON parser
is used.

On 18 Jun., 15:34, Chris Boertien <chris.boert...@gmail.com> wrote:
> Shouldn't have a problem sending a HashMap back and forth. As far as i
> can tell GWT has a fairly complete implementation of the Java
> Collections API, and all of which are Serializable, so there shouldn't
> be any issues. I haven't used it for much more than Lists so there
> might be quirks I haven't come across yet.
>
> 2010/6/18 Jose Luis Estrella Campaña <jlecamp...@gmail.com>:

Peter Simun

unread,
Jun 18, 2010, 11:14:42 AM6/18/10
to Google Web Toolkit
Hi Jose,

Piriti project looks great for your needs. If you would like to
customize deserialization process (with custom instantiators and
deserializers) you can also use acris-json project. You can find
details here:
http://code.google.com/p/acris/wiki/GWTJsonizer

(Project is based on JSONParser from GWT which internally use eval
function)

Only with using annotations you can get powerfull piece of
functionality in JSON parsing mechanism. For example this project was
used for deserialize JSON reponses from Google Youtube API in gwt-
youtube-api.googlecode.com project.

Peter

Gal Dolber

unread,
Jun 18, 2010, 11:26:47 AM6/18/10
to google-we...@googlegroups.com
I think the best you can do is a generator that creates an overlay for that pojo and a utility class to convert a json string to the pojo.

The interface will look like this:

public interface Json2Pojo<POJO> {
POJO fromJson(String json);
}

And the steps will be:
  1. Create an POJOOverlay from the json string
  2. Instantiate a POJO
  3. Call all POJO setters with POJOOverlay getter
  4. Return the POJO
And the gwt compiler will inline that for you.

Best

2010/6/18 Peter Simun <si...@seges.sk>

Chris Boertien

unread,
Jun 18, 2010, 11:44:05 AM6/18/10
to google-we...@googlegroups.com
All good points, but I think you guys are totally missing the point.
He was taking a POJO and converting it to JSON for transmission using
GWT-RPC, which is completely redundant since GWT-RPC can simply send
the POJO and handle all the messy bits for him.

Simple is better...

Gal Dolber

unread,
Jun 18, 2010, 2:14:02 PM6/18/10
to google-we...@googlegroups.com
Sure, there is no doubt that the best way to go its the gwt-rpc

2010/6/18 Chris Boertien <chris.b...@gmail.com>

Dean S. Jones

unread,
Jun 19, 2010, 10:09:00 AM6/19/10
to Google Web Toolkit
This is not totally true, gwt-rpc is OK for most data transfers, but
it causes a bit of overhead in deserialization. GWT's client side JSON
library is SLOW,
it constructs a parallel data structure of concrete types on parse,
upfront, and by default uses eval('''), which isn't always the best or
fastest way to go.

Since your asking the JSON data-structure "isArray()", etc, most of
the time, I find lazy evaluation to be much faster. Also, you can
tweak your custom deserializer to use the JSON
native parser if it is available.

On Jun 18, 2:14 pm, Gal Dolber <gal.dol...@gmail.com> wrote:
> Sure, there is no doubt that the best way to go its the gwt-rpc
>
> 2010/6/18 Chris Boertien <chris.boert...@gmail.com>
>
>
>
> > All good points, but I think you guys are totally missing the point.
> > He was taking a POJO and converting it to JSON for transmission using
> > GWT-RPC, which is completely redundant since GWT-RPC can simply send
> > the POJO and handle all the messy bits for him.
>
> > Simple is better...
>
> > >> > > > athttp://groups.google.com/group/google-web-toolkit?hl=en.
>
> > >> --
> > >> You received this message because you are subscribed to the Google
> > Groups
> > >> "Google Web Toolkit" group.
> > >> To post to this group, send email to
> > google-we...@googlegroups.com.
> > >> To unsubscribe from this group, send email to
> > >> google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/google-web-toolkit?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Google Web Toolkit" group.
> > > To post to this group, send email to google-we...@googlegroups.com
> > .
> > > To unsubscribe from this group, send email to
> > > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
> > > For more options, visit this group at
>
> ...
>
> read more »

Jose Luis Estrella Campaña

unread,
Jun 19, 2010, 2:17:35 PM6/19/10
to Google Web Toolkit
Wow, It's amazing to see this forum has so much activity. I feel so
welcome in here, too bad I can't see my question on the main
discussions page. :(

Ok, based on your replies, I've now analyzed all the options you've
given me, and sincerely I now honestly gotta say that it was my own
lack of GWT knowledge which led me to believe I had to do something
more complex than necessary. I think
if I stick with Chris' suggestion I'll be just fine. Now, I still have
one more fundamental question:

If I use my own classes (POJOs), say like a Person POJO and I create
it on the .client package of my gwt application. Is that ALL I gotta
do in order to work with it between the client and the server ? I mean
sending it from the client to the RemoteServiceService Impl.

If that's not all that I should do to work with my personal classes,
could you please tell me what else is required ? I'm still confused
with the <inherits> thing. or wheter a personal class should be
defined on the client as well as in the server side. I just would like
to know.

Best Regards, and thanks everyone for the kind replies !.

Jose.
> ...
>
> read more »

Chris Boertien

unread,
Jun 19, 2010, 8:32:32 PM6/19/10
to google-we...@googlegroups.com
Essentially that's about it. Although you probably want to put them
into a different package. The default Eclipse GWT Project contains a
'shared' package that is for this purpose. Its also by default in your
[project].gwt.xml file with the tag <source path="shared">.

Adding those source tags to your gwt.xml file tells GWT that those
classes need to be available client side. Hence why you wont find the
'server' package in that file, as it's server side code only. By
accessing a class in shared from the server side code, it will get
compiled and be available for you.

The rest is all done for you when GWT creates the MyServiceAsync class
which you define the interface for, and within the
RemoteServiceServlet which your Servlet extends.

I think it would be a worthwhile experiment to see if and how much of
a difference there is between using GWT-RPC and using JSON with
Overlay Types.

Just to throw another option into the mix you could roll your own RPC
interface using GWT Generators like GWT does.

While we're on the subject of GWT-RPC, are you locked into using a
Servlet on the server side? Is there some binary format that GWT
converts to for serialization, or is it simply producing a JSON string
and decoding it in RemoteServiceServlet.

2010/6/19 Jose Luis Estrella Campaña <jleca...@gmail.com>:

> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages