using Gson library in GWT client code

前端 未结 5 766
佛祖请我去吃肉
佛祖请我去吃肉 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: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 bean = AutoBeanUtils.getAutoBean(person);
        return AutoBeanCodex.encode(bean).getPayload();
    }
    
    Person deserializeFromJson(String json) 
    {     
        AutoBean 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...

提交回复
热议问题