Custom object serialization in GWT

30 views
Skip to first unread message

Christophe

unread,
Sep 13, 2006, 3:17:15 PM9/13/06
to Google Web Toolkit
Hi,

Is it possible to customize object serialization in GWT ?
I want to instantiate a client-side Java object from a JSONObject.

Since there is no client-side reflection it is not possible to
instantiate Java classes.
How can I solve this ?

Is it possible to create and use an implementation of
'SerializationStreamReader' ?
How can I do this ?

Cheers,

Christophe

Miguel Méndez

unread,
Sep 14, 2006, 11:08:08 AM9/14/06
to Google-We...@googlegroups.com
Hello Christophe,

It is theoretically possible but it would require some work.  Independent of the implementation, you will need the following pieces to make this work:
  1. You will need a class that can instantiate every type that the client can receive via JSON.
  2. You will need a class that can set the fields of the new instance based on the contents of the JSONObject.  (One class to handle all or one class per instance is up to you.  You will need to use JSNI to set the fields.)
You have two choices in terms of implementation:
  1. Bootstrap your implementations by using the code that the RPC system generates for you and tweaking it to fit your needs.
  2. Write it yourself using your own concepts.
I know that #2 sounds bad but it may be easier to do depending on your level of GWT proficiency.  From your email, it sounds like you are leaning towards #1.  If you do that you will need to modify the generated proxy code to use whatever type name format you want to use.  You will also need to write an adaptor that exposes an implementation of the SerializationStreamReader interface but whose implementation actually reads the values out of your JSONObject.  Otherwise, you would need to convert an JSONObject into our serialized format so that you could use a stock ClientSerializationStreamReader.

HTH,

claude

unread,
Sep 14, 2006, 11:21:37 AM9/14/06
to Google Web Toolkit
It's ironic, I think, that with JSON, you can do an eval(text) and have
a viable JS object immedialy, and yet it is incredibly difficult to
work with it from GWT. There must be a better way :-(

Miguel Méndez

unread,
Sep 14, 2006, 12:56:50 PM9/14/06
to Google-We...@googlegroups.com
I hear you Claude,

I agree that it would be nice to reduce the impedance mismatch between Java and JavaScript.

If you do not mind mixing Java and JavaScript, you could always create a Java wrapper around your evaled JS object and use JSNI to access it.  The JSON library uses this approach and it can result in a simple solution. 

The issue of taking a plain old JS object and auto-magically mapping it onto a Java class is a slightly trickier problem to get right.
Reply all
Reply to author
Forward
0 new messages