有没有可以序列化Proxy对象的JSON库?
使用 ActiveObjects 作为我的 ORM,使用 Gson 作为我的 JSON 处理器。
从以下位置转到 Json 时遇到问题 持久化的对象。问题是我的持久类实际上是 接口和 AO 在底层代理该对象。这是 一些示例代码:
Venue venue = manager.get(Venue.class, id);
gson.toJson(venue);
出现此异常:
java.lang.UnsupportedOperationException: Expecting parameterized type,
got interface java.lang.reflect.InvocationHandler.
Are you missing the use of TypeToken idiom?
See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...
因为venue.getClass().getName() 给出:
$Proxy228
我已经尝试了各种组合的一些解决方案:
gsonBuilder.registerTypeAdapter(Venue.class, newVenueSerializer());
Type listType = new TypeToken<Venue>() {}.getType();
到目前为止没有任何效果,我正在使用一个不稳定的字段- 现场解决方法。有什么建议吗?我没有与 Gson 结婚,所以如果有一个替代库可以做到这一点,我很乐意使用它。
Using ActiveObjects as my ORM and Gson as my JSON processor.
Ran into a problem going toJson from
persisted objects. The problem is that my persisted class is actually
an Interface and AO is proxying that object under the hood. Here's
some sample code:
Venue venue = manager.get(Venue.class, id);
gson.toJson(venue);
Comes up with this exception:
java.lang.UnsupportedOperationException: Expecting parameterized type,
got interface java.lang.reflect.InvocationHandler.
Are you missing the use of TypeToken idiom?
See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...
Because venue.getClass().getName() gives:
$Proxy228
I've tried a few solutions in various combinations:
gsonBuilder.registerTypeAdapter(Venue.class, newVenueSerializer());
Type listType = new TypeToken<Venue>() {}.getType();
Nothing has worked so far and I'm using a wonky field-by-field workaround. Any suggestions? I'm not married to Gson, so if there's an alternative library that can do this I'd be happy to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Flex JSON 应该可以工作 - 它将使用 bean 属性内省器来拉取对象,我假设代理类正确地实现了这些。
Flex JSON should work - it will use the bean property introspector to pull the object, and I assume the proxy class implements those properly.
另请查看杰克逊。
Also check out the Jackson.