Serializing an Object in GWT

What is the easiest way to serialize a bean for a string using GWT? I prefer not to use GWT.create () calls.

+6
java serialization javabeans gwt
source share
6 answers

Disclaimer: Serializing a bean in a URL is not such a great idea for GWT. I found out that if you need to put data at a URL, it should be as small as possible and only what is needed to restore the state of your page. See how Gmail uses its history tokens and you will see that it is very small.

With this disclaimer:

For the GWT project I was working on, I simply wrote out the bean values ​​separated by a separator. When you read the values ​​back, I used the String.split () method to get the array. With this array, I am returning 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 complex scenarios, you may have to do more complex things. For example, for nested objects, you need to write code to pass values ​​to a child object.

Also, keep in mind that you must ensure that any values ​​you use do not contain a separator. Therefore, if you know that your lines may contain "/", you may need to perform a replace () operation to avoid any nested delimiters.

+4
source share

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

If you really want to send lines back and forth, you really do not need to do anything - you will get it for free using the usual GWT RPC mechanism. Just create a method that returns 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 constructor with a null argument and implements Serializable or IsSerializable .

+1
source share

The closest I could find was the following:

Faster GWT launch with objects embedded in the main HTML page

+1
source share

Perhaps this is what you are looking for?

Json ↔ Java Serialization That Works with GWT

Extended version:

Using the Json-lib library:

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

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

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

0
source share

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

  • Linking / matching XML data like JAXB and XStream can do this
  • In JSON, libraries such as Jackson can perform a flexible full bean binding similar to JAXB, but with even less configuration (and faster speed, if that matters at all).

must work.

0
source share

Ultimately, GWT runs in JavaScript (although it is written in Java). In this sense, "java beans" is not something that you can easily find in the client, but they work fine on the server (in Java).

If you agree that a bean is really just a hassle-free object, you are the main intention to use them to move data. The JSON core is typically used in JavaScript as an extremely flexible data container. On the server side, a Beans array can be converted to JSON using BeanUtils (and a bit of a workaround). JSON can be serialized in GWT as a string, and GWT has a parser for converting JSON into JavaScript objects for the client.

This is probably not the easiest way to do this, but it is very flexible as soon as you earn it.

0
source share

All Articles