using Gson library in GWT client code

前端 未结 5 737
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 05:44

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 cod

相关标签:
5条回答
  • 2020-12-05 05:44

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

    0 讨论(0)
  • 2020-12-05 05:46

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

    0 讨论(0)
  • 2020-12-05 05:48

    (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)

    • client- and server-side
      • AutoBeans (*): http://code.google.com/p/google-web-toolkit/wiki/AutoBean
        • I had problems with generics there (2015-02-07) similar to this: RequestFactory: Proxy implementing interface with generics
    • client-side-only
      • Piriti
      • RestyGWT: http://restygwt.fusesource.org/documentation/restygwt-user-guide.html#JSON_Encoder_Decoders
      • RocketGWT: http://code.google.com/p/rocket-gwt/wiki/JsonSerialization
      • Acris: http://code.google.com/p/acris/wiki/GWTJsonizer
      • JavaScript overlay types (*)
    • server-side only
      • Gson (from Google)

    (*) from GWT project itself

    Comparisons:

    • e.g. https://github.com/hpehl/piriti/wiki/Comparison
    0 讨论(0)
  • 2020-12-05 05:51

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

    Works like a charm :-)

    0 讨论(0)
  • 2020-12-05 05:52

    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.

    0 讨论(0)
提交回复
热议问题