在小程序中使用 GSON 库时的反射权限问题 [英] Reflection Permission problems when using the GSON library in a applet

查看:15
本文介绍了在小程序中使用 GSON 库时的反射权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个进行 JSON-RPC 调用的 Applet.我正在使用 Google JSON 库 (GSON) 将响应 JSON 转换为一个类.Thsi 似乎工作正常,但是当我在我的 Applet 中使用此代码时,我遇到了 java.lang.reflect.reflectpermission.从我在这个线程上读到的内容,似乎由于 GSON 使用反射,除非我明确修改安全策略,否则我无法在 Applets 中使用它.我怎样才能解决这个问题?我在我的应用程序中创建了一堆类,并使用 Gson.fromJson 方法将它转换到类中.有没有什么方法可以实现相同的功能而不必重新编写我的一半代码.

I'm writing an Applet that makes some JSON-RPC calls. I'm using the Google JSON library (GSON) to cast the response JSON into a class. Thsi seems to work fine as is but when I use this code in my Applet, I'm hit with a java.lang.reflect.reflectpermission. From what I read on this thread on SO, it seems that since GSON uses Reflection, I cannot use it in Applets unless I explicitly modify the security policy. How can I get around this? I've created a bunch of classes in my application and was using the Gson.fromJson method to cast it into the class. Is there any way to achieve the same functionality without having to re-write half my code.

(在 Java 中处理 JSON 的复杂性似乎自成一格!)

(The complexity of dealing with JSON in Java seems to be in a league of its own!)

提前谢谢各位.

推荐答案

Java 的一些 JSON 库确实非常复杂,尤其是考虑到 JSON 本身的简单性.在我看来 http://www.json.org/java/index.html是最好的库,并没有做任何花哨的技巧,只是将 JSON 解析为它自己的特殊结构,仅此而已.这当然意味着您必须编写特定代码才能将 JSON 对象层次结构转换为您使用的内容,但这不应该那么难.

Some JSON libraries for Java are indeed mind numbingly complex especially considering the simplicity of JSON itself. In my opinion http://www.json.org/java/index.html is the best library and doesn't do any fancy tricks, just parses the JSON into its own special structure and that's it. This of course means you have to make specific code to turn the JSON Object hierarchy to something you use but that shouldn't be that hard.

GSON 更多的是基于 XStream(也可以做 JSON)的脉络的基于 JSON 的持久性对于一个 Applet 来说,听起来有点太重量级了.

GSON is more about JSON-based persistence in the vein of XStream (which can do JSON too) and sounds a bit too heavyweight for just an Applet.

这篇关于在小程序中使用 GSON 库时的反射权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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