在GWT客户端代码中使用Gson库 [英] using Gson library in GWT client code

查看:118
本文介绍了在GWT客户端代码中使用Gson库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Eclipse中的GWT 2.0编写一个Web应用程序。
我想知道是否有办法在 Gson图书馆中使用一个GWT应用程序的客户端代码。



如果有方法 - 请告诉我如何......



谢谢!

解决方案

不完全是你写的,但我猜你的意思是如何序列化/反序列化GWT代码中的JSON?

在GWT 2.1.1中,您可以使用 GWT AutoBean框架



在文章的底部看到它有这个神奇的代码...

  String serializeToJson(Person person)
{
//检索AutoBean控制器
AutoBean< Person> bean = AutoBeanUtils.getAutoBean(person);
返回AutoBeanCodex.encode(bean).getPayload();
}

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


$


即使对于继承Person的实例,serializeToJson()我没有尝试deserializeFromJson ...


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!

解决方案

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

这篇关于在GWT客户端代码中使用Gson库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆