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

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

问题描述

我目前正在 Eclipse 中使用 GWT 2.0 用 Ja​​va 编写 Web 应用程序.我想知道是否有办法在 GWT 应用程序的 Gson 库em>客户端代码.

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

谢谢!

解决方案

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

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

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

String serializeToJson(Person person){//检索 AutoBean 控制器AutoBean<人>bean = AutoBeanUtils.getAutoBean(person);返回 AutoBeanCodex.encode(bean).getPayload();}人 deserializeFromJson(String json){AutoBean<人>bean = AutoBeanCodex.decode(myFactory, Person.class, json);返回 bean.as();}

serializeToJson() 对我来说很好,即使是继承 Person 的实例,但我没有尝试 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天全站免登陆