views:

2292

answers:

6

What's the simplest way to serialize a bean to a string using GWT? I prefer not to use GWT.create() invocations.

A: 

Maybe this is what you're looking for?

http://stackoverflow.com/questions/683123/json-java-serialization-that-works-with-gwt

Extended Version:

Using the Json-lib library:

http://json-lib.sourceforge.net/

You can do this (going right from bean to json string):

http://json-lib.sourceforge.net/snippets.html#Creating_a_JSONObject_from_a_JavaBean

Organiccat
Something like that, but I don't want to write Overlay Types for all my classes (40+). I'd like a simple, non-intrusive way to serialize an object to a String.
Miguel Ping
Non-intrusive is relative, because I consider making Overlay Types for all data I'm going to pass, BUT if you want a way to hack your way directly from a Bean to a JSON string object, check out my update.
Organiccat
I'm confident sure that the json-lib won't work in GWT (client-side), because it probably uses reflection through BeanInfo. I can't check the sources right now.
Miguel Ping
+1  A: 

I'm not sure I understand what you're ultimately trying to accomplish.

If you really just want to send strings back and forth, you don't really need to do anything- you get that for free using the normal GWT RPC mechanism. Just create a method that returns a java.lang.String, create the accompanying implementation and Async interface, and you're done.

If on the other hand you really want to send a bean, just make sure it has a zero-arg constructor and implements Serializable or IsSerializable.

Limbic System
I want to spit a bean out to an URL bar, the easiest possible way.
Miguel Ping
As Miguel said above, there is no reflection in GWT. You might be better off just writing a custom "toUrl()" method.
Limbic System
A: 

There may be some nuances of GWT that complicate things, but generally things like:

  • XML data binding/mapping libs like JAXB and XStream can do this
  • With JSON, libraries like Jackson can do flexible full bean binding, similar to JAXB, but with even less configuration (and faster speed if that matters at all).

should work.

StaxMan
This won't cut it since these libs probably use reflection, which is not available on GWT client side.
Miguel Ping
A: 

Ultimately GWT is running in JavaScript (even though it is written in Java). In that sense "java beans" are not things you easily find in the client, but they work fine in the server (in Java).

If you accept that a bean is really just a methodless object, you're underlying intent in using them is for moving data around. Natively in JavaScript, JSON acts as an extremely flexible container for data as well. On the server-side an array of Beans can be converted into JSON using BeanUtils (and a bit of traversal). JSON can be serialized in GWT as a string, and GWT has a parser to convert JSON back into JavaScript objects for the client.

It's probably not the easiest way to do this, but it is very flexible once you get it working.

Paul W Homer
A: 

Disclaimer: Serializing a bean on the URL isn't such a great idea for GWT. I've learned that if need to put data on the URL, it should be as little as possible and only what is necessary to restore the state of your page. Look at how Gmail uses its history tokens and you'll see it is pretty minimal.

With that disclaimer out of the way:

For a GWT project I worked on I simply wrote out values of the bean separated by a delimiter. When reading the values back in, I used the String.split() method to get an array. With that array I assign the values back to the right bean properties. In code:

public class Sample {

    private int a;
    private boolean b;
    private String c;
    //getters and setters for fields not shown

    public String toHistoryToken(){
        return a+"/"+b+"/"+c;
    }
    public void fromHistoryToken(String token){
        String[] values=token.split("/");
        a=Integer.parseInt(values[0]);
        b=Boolean.parseBoolean(values[1]);
        c=values[2];
    }
}

For more complicate scenarios you may have to do more complicated things. For example, for nested objects, you have to write the code to pass the values to the child object(s).

Also, be aware that you have to make sure that any values you use don't contain the delimiter. So if you know your Strings might contain "/" then you might have to do a replace() operation of them to escape any nested delimiters.

Peter Dolberg
+1  A: 

The closest that I could find was this:

Faster GWT startup with objects embedded in the HTML host page

jsight