19

I'm currently writing a web application in java using GWT 2.0 in eclipse. I wanted to know if there is a way to use Gson library in a GWT application's client code.

and if there is a way - please tell me how...

Thanks!

Rubinsh
  • 4,883
  • 10
  • 34
  • 41
  • This might not be exactly the solution you are looking for, but I found that using JavaScript overlay types for easy handling of JSON objects is the best - for more info see here: http://googlewebtoolkit.blogspot.com/2008/08/getting-to-really-know-gwt-part-2.html (then you could use Gson on server side and JSO on the client side - not the perfect solution but better than "manual" parsing via standard GWT methods). – Igor Klimer Feb 06 '10 at 17:15
  • 1
    The thing is - that writring the overlay type in my opinion is the same amount of work as parsing the JSON object manually. I already have the class written (generated from a wsdl) and I am trying to perform HTTP requests on that wsdl in json format and load the responses to my objects. do you have an idea of what is the best way for doing this? – Rubinsh Feb 07 '10 at 07:44

5 Answers5

26

Gson uses Java features that are not supported in GWT such as reflection. Thus it is not possible to use Gson in GWT client side code.

Guido
  • 46,642
  • 28
  • 120
  • 174
Lauri
  • 4,670
  • 2
  • 24
  • 17
  • so i am only supposed to use GSON on the backend (server) side? – Rubinsh Feb 14 '10 at 16:04
  • 3
    That's correct. On the client side the easiest way is to convert json to objects is to use overlay types. – Lauri Feb 14 '10 at 16:23
  • It is too bad they don't have an option to generate the "overlay" classes from the GSON objects on the backend. This would improve the situation. – GreenKiwi Dec 02 '10 at 07:20
16

Not exactly what you wrote but I guess that what you meant was how to serialize/deserialize JSON in GWT code?

In GWT 2.1.1 you can use GWT AutoBean framework

See there at the bottom of the article it has this magic code ...

String serializeToJson(Person person) 
{
    // Retrieve the AutoBean controller
    AutoBean<Person> bean = AutoBeanUtils.getAutoBean(person);
    return AutoBeanCodex.encode(bean).getPayload();
}

Person deserializeFromJson(String json) 
{     
    AutoBean<Person> bean = AutoBeanCodex.decode(myFactory, Person.class, json);     
    return bean.as();   
} 

the serializeToJson() woks fine for me even with instances that are inherit Person but I did not try the deserializeFromJson...

Community
  • 1
  • 1
Boris Daich
  • 2,431
  • 3
  • 23
  • 27
3

(feel free to enhance my post if you like)

currently (2015-02-07) it is not possible although I like Gson very much and would like to have only one solution for shared code :-/ , but there are some other libraries available (I only know AutoBeans and Gson myself and had a quick look at Piriti):

(some support both or only one of XML and JSON (de)serialization)

(*) from GWT project itself

Comparisons:

Community
  • 1
  • 1
Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96
2

I have write a library that allows using GWT with Gson, you can download here and enjoy it.

heroandtn3
  • 164
  • 1
  • 7
2

In our GWT project we use piriti: http://code.google.com/p/piriti/

Works like a charm :-)

Tom Teman
  • 1,975
  • 3
  • 28
  • 43