Client-side JSON Serialization?

845 views
Skip to first unread message

clint....@childrens.harvard.edu

unread,
Dec 16, 2008, 9:34:58 PM12/16/08
to Google Web Toolkit
I'm sorry if this has already been answered, but after searching a bit
I didn't come up with any answers. Does anyone know of a way to
serialize objects to JSON on the client side? Ideally, it would work
similarly to the json2.js lib from json.org that I was using prior to
moving to the GWT, but anything relatively straightforward that
performs well is fine with me.

I tried wrapping the json2.js lib in a native method that calls
$wnd.JSON.stringify():

public abstract class JSONSerializer
{
public static final native String serialize(final Object object)/*-
{
return $wnd.JSON.stringify(object);
}-*/;
}

but it pins my CPU and runs for several minutes when serializing
anything but a trivial object. The problem seemed to be the verbosity
(completeness?) of the object when marshalled across the Java-
toJavascript boundary by the GWT. I'd also like to control the
mapping of Java properties to JSON ones.

Surely there's a better way? Or will I have to roll my own?

Shawn Pearce

unread,
Dec 17, 2008, 1:24:21 AM12/17/08
to Google-We...@googlegroups.com
Roll your own, or look at gwtjsonrpc:

  http://android.git.kernel.org/?p=tools/gwtjsonrpc.git;a=blob;f=README;hb=HEAD

You can also look at JSNI and the JavaScriptObject subclassing tricks in the GWT documentation (under JavaScript Integration) to build Java objects that are more directly JavaScript entities, and thus more easily serialized with JavaScript JSON libraries.

mikeds...@gmail.com

unread,
Dec 17, 2008, 8:15:34 AM12/17/08
to Google Web Toolkit
I haven't done anything non-trivial in JSON & GWT, but what are you
looking to do that you can't do natively with GWT? Like on these
examples:

http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=GettingStartedJSON

I'm trying to figure out your use case and what you can't do with GWT
that you'd need json2.js or something like it. Just wondering most of
all.

Later,

Shaffer


On Dec 16, 11:24 pm, Shawn Pearce <s...@google.com> wrote:
> Roll your own, or look at gwtjsonrpc:
>
> http://android.git.kernel.org/?p=tools/gwtjsonrpc.git;a=blob;f=README...
>
> You can also look at JSNI and the JavaScriptObject subclassing tricks in the
> GWT documentation (under JavaScript Integration) to build Java objects that
> are more directly JavaScript entities, and thus more easily serialized with
> JavaScript JSON libraries.
>
> On Tue, Dec 16, 2008 at 18:34, clint.gilb...@childrens.harvard.edu <

Gilbert, Clint

unread,
Dec 17, 2008, 11:58:07 AM12/17/08
to Google-We...@googlegroups.com
We're making an app that visualizes data in a number of ways. Data sets can be uploaded to and downloaded from the server, as well as merged, subtracted, etc. All the data manipulation happens on the client; the client should be able to store users' data sets on the server after they've been manipulated locally. In the past, we just serialized the client-side data set as JSON and sent it to the server for storage.

In the previous version of our app, we used raw Javascript (and jQuery) on the client. We hit a complexity threshold where new features were taking too much time, and decided to port to GWT. In general, our productivity has improved a lot (we're old Java hands, and Java offers more language features for managing complexity than JS), but this is the last big missing piece.

Another thing I should mention is that we're using Google App Engine as the backend, so we can't use GWT's built-in RPC services. (If we were starting from scratch, we would consider them seriously.) Using GAE has other implications. Due to GAE's fairly tight CPU quotas, we are more or less required to do the data set manipulation on the client. If we had more server-side resources, we might be able do all the data set manipulation on the server and have the client retrieve read-only copies serialized as JSON for display.

Thanks for your interest.
________________________________________
From: Google-We...@googlegroups.com [Google-We...@googlegroups.com] On Behalf Of mikeds...@gmail.com [mikeds...@gmail.com]
Sent: Wednesday, December 17, 2008 8:15 AM
To: Google Web Toolkit
Subject: Re: Client-side JSON Serialization?

Clint Gilbert

unread,
Dec 17, 2008, 2:14:12 PM12/17/08
to Google-We...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Thanks very much, I'll take a look at wgtjsonrpc.

My app needs to serialize a non-trival map of data: String => Date-range class => other
user-defined type => data POJO, currently. I'm familiar with Overlay types, and somewhat
familiar with JSNI, so I think I could write a class that wraps a JavaScriptObject and
uses that handle as the backing map. Then I could just serialize the wrapped JSO. But
then I'd lose some useful features of a Java Map, like being able to use non-primitive
types for keys, nice methods like keySet(), etc, no?

Shawn Pearce wrote:
> Roll your own, or look at gwtjsonrpc:
>
> http://android.git.kernel.org/?p=tools/gwtjsonrpc.git;a=blob;f=README;hb=HEAD
>
> You can also look at JSNI and the JavaScriptObject subclassing tricks in the GWT documentation (under JavaScript Integration) to build Java objects that are more directly JavaScript entities, and thus more easily serialized with JavaScript JSON libraries.
>
> On Tue, Dec 16, 2008 at 18:34, clint....@childrens.harvard.edu<mailto:clint....@childrens.harvard.edu> <clint....@childrens.harvard.edu<mailto:clint....@childrens.harvard.edu>> wrote:
>
> I'm sorry if this has already been answered, but after searching a bit
> I didn't come up with any answers. Does anyone know of a way to
> serialize objects to JSON on the client side? Ideally, it would work
> similarly to the json2.js lib from json.org<http://json.org> that I was using prior to
> moving to the GWT, but anything relatively straightforward that
> performs well is fine with me.
>
> I tried wrapping the json2.js lib in a native method that calls
> $wnd.JSON.stringify():
>
> public abstract class JSONSerializer
> {
> public static final native String serialize(final Object object)/*-
> {
> return $wnd.JSON.stringify(object);
> }-*/;
> }
>
> but it pins my CPU and runs for several minutes when serializing
> anything but a trivial object. The problem seemed to be the verbosity
> (completeness?) of the object when marshalled across the Java-
> toJavascript boundary by the GWT. I'd also like to control the
> mapping of Java properties to JSON ones.
>
> Surely there's a better way? Or will I have to roll my own?
>
>
>
>
> >
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFJSU+ErZoE3ArapxERCFm6AKCBhbEn83de4ivjGGgBAagpEAFGaQCeIDJL
3xJzW9oU0UXdC9WuiFUbm3M=
=/zOi
-----END PGP SIGNATURE-----

Shawn Pearce

unread,
Dec 18, 2008, 2:00:51 AM12/18/08
to Google-We...@googlegroups.com

Right.  Its harder with the overlay types to define a map or something like that.  You could try to wrap the JavaScriptObject with some class that knows to convert a list in native JS into a Java Map, but at that point you have about half of GWT's own RPC system, or gwtjsonrpc.  This case of wanting rich Java types but a clear JSON encoding is why I wrote gwtjsonrpc.

A thought just occured to me though; a JSON "map" would typically be an object with the keys being the property names...  but JSON only permits strings for the property names and you mention wanting some other types.  gwtjsonrpc should support any Object type in the key (so long as it is declared to be a concrete type in the Map<> type parameter), but for non string typed key maps gwtjsonrpc uses a JSON array of pairs (even index is key, odd is value).  I'm not sure what sort of encoding you were looking for or needing here...  (for Map<String,...> it uses the standard object notation).

On Dec 17, 2008 11:12 AM, "Clint Gilbert" <clint....@childrens.harvard.edu> wrote:


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Thanks very much, I'll take a look at wgtjsonrpc.

My app needs to serialize a non-trival map of data: String => Date-range class => other
user-defined type => data POJO, currently.  I'm familiar with Overlay types, and somewhat
familiar with JSNI, so I think I could write a class that wraps a JavaScriptObject and
uses that handle as the backing map.  Then I could just serialize the wrapped JSO.  But
then I'd lose some useful features of a Java Map, like being able to use non-primitive
types for keys, nice methods like keySet(), etc, no?

Shawn Pearce wrote: > Roll your own, or look at gwtjsonrpc: > > http://android.git.kernel.org/?p...

> On Tue, Dec 16, 2008 at 18:34, clint....@childrens.harvard.edu<mailto:clint.gilbert@childrens....

> similarly to the json2.js lib from json.org<http://json.org> that I was using prior to

> moving to the GWT, but anything relatively straightforward that > performs well is fine with me. >...

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFJSU+ErZoE3ArapxERCFm6AKCBhbEn83de4ivjGGgBAagpEAFGaQCeIDJL
3xJzW9oU0UXdC9WuiFUbm3M=
=/zOi
-----END PGP SIGNATURE-----

--~--~---------~--~----~------------~-------~--~----~ You received this message because you are su...

Reply all
Reply to author
Forward
0 new messages