在GWT客户端代码中使用Gson库
我目前正在eclipse中使用GWT 2.0在java中编写Web应用程序。 我想知道是否有一种方法可以在GWT应用程序的客户端代码中使用Gson库 。
如果有办法 – 请告诉我如何……
谢谢!
不完全是你写的,但我想你的意思是如何在GWT代码中序列化/反序列化JSON?
在GWT 2.1.1中,您可以使用GWT AutoBean框架
看到文章底部有这个神奇的代码……
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(); }
serializeToJson()对我来说很好,即使是inheritancePerson的实例但我没有尝试deserializeFromJson ……
Gson使用GWT不支持的Javafunction,例如reflection。 因此,不可能在GWT客户端代码中使用Gson。
(如果你愿意,随时提高我的post)
目前(2015-02-07)虽然我非常喜欢Gson并希望只有一个共享代码解决方案,但是不可能: – /,但是还有一些其他库可用(我自己只知道AutoBeans和Gson快速浏览Piriti):
(有些支持XML或JSON(de)序列化)或仅支持其中一种
- 客户端和服务器端
- AutoBeans (*): http : //code.google.com/p/google-web-toolkit/wiki/AutoBean
- 我在那里遇到了generics问题(2015-02-07),类似于: RequestFactory:代理实现与generics的接口
- AutoBeans (*): http : //code.google.com/p/google-web-toolkit/wiki/AutoBean
- 客户端只
- 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覆盖类型 (*)
- 仅限服务器端
- Gson (来自Google)
(*)来自GWT项目本身
比较:
- 例如https://github.com/hpehl/piriti/wiki/Comparison
在我们的GWT项目中,我们使用piriti : http : //code.google.com/p/piriti/
奇迹般有效 :-)
我已经写了一个允许将GWT与Gson一起使用的库,你可以在这里下载并享受它。