[gwt-contrib] How to serialize client side objects to string?

770 views
Skip to first unread message

dflorey

unread,
Apr 26, 2010, 9:21:33 AM4/26/10
to Google Web Toolkit Contributors
Is there a way to easily serialize GTW-serializable objects to string
on the client side?
I'd like to be able to story arbitrary objects to the html5/gears
local db as string.
Any ideas? Can I hook into the GWT rpc-serialization mechanism?

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Thomas Broyer

unread,
Apr 26, 2010, 10:09:30 AM4/26/10
to Google Web Toolkit Contributors


On Apr 26, 3:21 pm, dflorey <daniel.flo...@gmail.com> wrote:
> Is there a way to easily serialize GTW-serializable objects to string
> on the client side?
> I'd like to be able to story arbitrary objects to the html5/gears
> local db as string.
> Any ideas? Can I hook into the GWT rpc-serialization mechanism?

It would be the exact opposite mechanism as used to embed the GWT-RPC
payload in the initial HTML download, as showed in a presentation from
Chris Ramsdale:
http://www.slideshare.net/cramsdale/gwt-best-practices-devnexus-2010
(slide #44)

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

John Tamplin

unread,
Apr 26, 2010, 10:12:55 AM4/26/10
to google-web-tool...@googlegroups.com
On Mon, Apr 26, 2010 at 9:21 AM, dflorey <daniel...@gmail.com> wrote:
Is there a way to easily serialize GTW-serializable objects to string
on the client side?
I'd like to be able to story arbitrary objects to the html5/gears
local db as string.
Any ideas? Can I hook into the GWT rpc-serialization mechanism?

The problem is for efficiency reasons the GWT RPC serialization is asymmetric -- the client can't deserialize what is serializes and vice-versa.

Also, values stored in a local database will likely need to be used across versions of the app, so GWT RPC isn't really appropriate anyway -- probably better to use protobufs or JSON, though in either case you give up the ability to represent arbitrary Java object graphs.

--
John A. Tamplin
Software Engineer (GWT), Google

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Thomas Broyer

unread,
Apr 26, 2010, 10:49:53 AM4/26/10
to Google Web Toolkit Contributors


On Apr 26, 4:12 pm, John Tamplin <j...@google.com> wrote:
> On Mon, Apr 26, 2010 at 9:21 AM, dflorey <daniel.flo...@gmail.com> wrote:
> > Is there a way to easily serialize GTW-serializable objects to string
> > on the client side?
> > I'd like to be able to story arbitrary objects to the html5/gears
> > local db as string.
> > Any ideas? Can I hook into the GWT rpc-serialization mechanism?
>
> The problem is for efficiency reasons the GWT RPC serialization is
> asymmetric -- the client can't deserialize what is serializes and
> vice-versa.
>
> Also, values stored in a local database will likely need to be used across
> versions of the app, so GWT RPC isn't really appropriate anyway -- probably
> better to use protobufs or JSON, though in either case you give up the
> ability to represent arbitrary Java object graphs.

D'oh, you're right of course, I forgot about the asymmetric encoding!

If anyone's interested in using Protobuf in GWT, (shameless plug) have
a look at my new pet project: http://code.google.com/p/protobuf-gwt/
And if you prefer Thrift to Protobuf, then have a look at gwt-rpc-plus
from DotSpots (and Matt Mastracci in particular): http://code.google.com/p/gwt-rpc-plus/

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

mmoossen

unread,
Apr 26, 2010, 1:55:44 PM4/26/10
to Google Web Toolkit Contributors
Hi, thomas!

good hint that RPC prefetching method... i was using json for that,
but with this method i can skip the java to json conversion, cool ;)

thanks for sharing
Michael

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

mmoossen

unread,
Apr 27, 2010, 8:54:47 AM4/27/10
to Google Web Toolkit Contributors
Dear all!

i tried the described RPC prefetching mechanism and i am facing one
problem:

first, on the server-side to serialize/RPC.encode(...) i need the
serialization policy, but i need the strong name to get it from the
RemoteService. so i am just using a dummy serialization policy that
seems to work fine. is this the right approach? any alternative?

then i have to put the resulting payload into the host page as js
global string variable, i am enclosing it in single quotation marks,
but do i have to escape single quotation marks in the payload or will
they be already escaped?

and finally on the client-side the code fails at this line:
> streamFactory.createStreamReader(data).readObject();

with following exception:
com.google.gwt.user.client.rpc.SerializationException: my.project.Data/
2847679377
at
com.google.gwt.user.client.rpc.impl.SerializerBase.check(SerializerBase.java:
161)
at
com.google.gwt.user.client.rpc.impl.SerializerBase.instantiate(SerializerBase.java:
138)
at
com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader.deserialize(ClientSerializationStreamReader.java:
114)
at
com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamReader.readObject(AbstractSerializationStreamReader.java:
61)

which seems to mean that the client side serializer does not know
about my.project.Data, but this is included in the .gwt.rpc file.
so i have no idea what is wrong nor how to fix it.
any help is really appreciated.

thanks
Michael
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

mmoossen

unread,
Apr 29, 2010, 9:56:45 AM4/29/10
to Google Web Toolkit Contributors
please, somebody help!

thanks
Michael

mmoossen

unread,
May 3, 2010, 4:32:39 AM5/3/10
to Google Web Toolkit Contributors
i got it working!!!!

the problem was that i was using
com.google.gwt.user.RemoteServiceObfuscateTypeNames in my module but
my faked SerializationPolicy does not implements TypeNameObfuscator.

so the next question is:
what is the best approach to get the right SerializationPolicy for
serializing??

thanks
Michael

Thomas Broyer

unread,
May 3, 2010, 5:34:11 AM5/3/10
to Google Web Toolkit Contributors


On May 3, 10:32 am, mmoossen <mmoos...@gmail.com> wrote:
> i got it working!!!!
>
> the problem was that i was using
> com.google.gwt.user.RemoteServiceObfuscateTypeNames in my module but
> my faked SerializationPolicy does not implements TypeNameObfuscator.
>
> so the next question is:
> what is the best approach to get the right SerializationPolicy for
> serializing??

Just in time: Alex Moffat published an update of his blogpost on the
matter for the new deRPC: http://development.lombardi.com/?p=1329
It links to the previous article with downloadable code:
http://development.lombardi.com/?p=1174
based on an article by Pat Niemeyer:
http://www.techhui.com/profiles/blogs/simpler-and-speedier-gwt-with

Really worth the read.

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

mmoossen

unread,
May 3, 2010, 3:13:08 PM5/3/10
to Google Web Toolkit Contributors
i thought i was subscribed to lombardi dev channel, but i was not :(
but now i am, at least i hope so ;)

thanks for the ref
Michael
Reply all
Reply to author
Forward
0 new messages