java 在 GWT 客户端代码中使用 Gson 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2213734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using Gson library in GWT client code
提问by Rubinsh
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 libraryin a GWT application's clientcode.
我目前正在使用 Eclipse 中的 GWT 2.0 用 Java 编写 Web 应用程序。我想知道是否有办法在 GWT 应用程序的客户端代码中使用Gson 库。
and if there is a way - please tell me how...
如果有办法 - 请告诉我如何......
Thanks!
谢谢!
采纳答案by Boris Daich
Not exactly what you wrote but I guess that what you meant was how to serialize/deserialize JSON in GWT code?
不完全是你写的,但我猜你的意思是如何在 GWT 代码中序列化/反序列化 JSON?
In GWT 2.1.1 you can use GWT AutoBean framework
在 GWT 2.1.1 中,您可以使用GWT AutoBean 框架
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...
即使使用继承 Person 的实例, serializeToJson() 对我来说也很好用,但我没有尝试 deserializeFromJson ...
回答by Lauri
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.
Gson 使用了 GWT 不支持的 Java 特性,例如反射。因此,不可能在 GWT 客户端代码中使用 Gson。
回答by Andreas Dietrich
(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):
目前 (2015-02-07) 虽然我非常喜欢 Gson 并且希望只有一个共享代码的解决方案 :-/ ,但这是不可能的,但是还有一些其他可用的库(我自己只知道 AutoBeans 和 Gson 并且有快速浏览 Piriti):
(some support both or only one of XML and JSON (de)serialization)
(有些支持 XML 和 JSON(反)序列化两者或仅其中之一)
- 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
- AutoBeans(*): http://code.google.com/p/google-web-toolkit/wiki/AutoBean
- 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)
- 客户端和服务器端
- AutoBeans(*):http: //code.google.com/p/google-web-toolkit/wiki/AutoBean
- 我在那里 (2015-02-07) 遇到了与此类似的泛型问题:RequestFactory: Proxy implementation interface with generics
- AutoBeans(*):http: //code.google.com/p/google-web-toolkit/wiki/AutoBean
- 仅客户端
- 皮里蒂
- RestyGWT: http://restygwt.fusesource.org/documentation/restygwt-user-guide.html#JSON_Encoder_Decoders
- RocketGWT:http: //code.google.com/p/rocket-gwt/wiki/JsonSerialization
- 阿克里斯:http: //code.google.com/p/acris/wiki/GWTJsonizer
- JavaScript 覆盖类型(*)
- 仅服务器端
- Gson(来自谷歌)
(*) from GWT project itself
(*) 来自 GWT 项目本身
Comparisons:
比较:
回答by Tom Teman
In our GWT project we use piriti: http://code.google.com/p/piriti/
在我们的 GWT 项目中,我们使用piriti:http: //code.google.com/p/piriti/
Works like a charm :-)
奇迹般有效 :-)

