Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Gson library in GWT client code

Tags:

java

gson

gwt

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!

like image 483
Rubinsh Avatar asked Feb 06 '10 16:02

Rubinsh


2 Answers

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.

like image 91
Lauri Avatar answered Nov 15 '22 19:11

Lauri


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...

like image 20
Boris Daich Avatar answered Nov 15 '22 20:11

Boris Daich