问题描述
我需要为某些对象实现JSON序列化,并且在与通用集合集成时,我遇到了一个问题.
所有序列化类都实现了此接口(jsonobject来自 this 库):
)interface JSONSerializable{ public JSONObject dump() throws JSONException //serializes object public void load(JSONObject obj) throws JSONException //deserializes object }
基于java.util.list的收藏的代码或多或少地像这样:
class AwesomeList<T extends JSONSerializable> implements JSONSerializable{ private LinkedList<T> items = new LinkedList<T>(); ... ... public JSONObject dump() throws JSONException { JSONObject result = new JSONObject(); JSONArray a = new JSONArray(); for(T i : items){ a.put(i.dump()); } result.put("items", a); return result; } public void load(JSONObject obj) throws JSONException{ //here is my problem } }
我的问题是:当我从jsonobject加载awesomelist时,我需要创建它的元素,但是这是不可能的,因为Java禁止我写
T newItem = new T(); newItem.load(obj);
我应该如何修改我对此任务的方法?
推荐答案
您是否与此库绑在一起? Google gson 非常受欢迎.我本人没有与仿制药一起使用过,但他们的头版说,格森认为对仿制药的支持非常重要.
其他推荐答案
正如其他人所暗示的,您应该考虑倾倒org.json的库.如今,这几乎已经过时了,试图解决问题的问题是浪费时间.
但要具体问题;类型变量t只是没有任何信息可以帮助您,因为它只是编译时间信息. 相反,您需要通过实际类(如"类CLS"参数),然后可以使用'cls.newinstance()'.
创建一个实例.其他推荐答案
好吧,当将其写出文件时,您确实知道T类是什么,因此您可以将其存储在dump中.然后,当阅读回去时,您可以使用反射动态调用它.
public JSONObject dump() throws JSONException { JSONObject result = new JSONObject(); JSONArray a = new JSONArray(); for(T i : items){ a.put(i.dump()); // inside this i.dump(), store "class-name" } result.put("items", a); return result; } public void load(JSONObject obj) throws JSONException { JSONArray arrayItems = obj.getJSONArray("items"); for (int i = 0; i < arrayItems.length(); i++) { JSONObject item = arrayItems.getJSONObject(i); String className = item.getString("class-name"); try { Class<?> clazzy = Class.forName(className); T newItem = (T) clazzy.newInstance(); newItem.load(obj); items.add(newItem); } catch (InstantiationException e) { // whatever } catch (IllegalAccessException e) { // whatever } catch (ClassNotFoundException e) { // whatever } }
问题描述
I need to implement JSON serialization for some objects, and I've encountered a problem when it came to integration with generic collections.
All serializable classes implement this interface (JSONObject comes from this library):
interface JSONSerializable{ public JSONObject dump() throws JSONException //serializes object public void load(JSONObject obj) throws JSONException //deserializes object }
Code for my collection based on java.util.list looks more or less like this:
class AwesomeList<T extends JSONSerializable> implements JSONSerializable{ private LinkedList<T> items = new LinkedList<T>(); ... ... public JSONObject dump() throws JSONException { JSONObject result = new JSONObject(); JSONArray a = new JSONArray(); for(T i : items){ a.put(i.dump()); } result.put("items", a); return result; } public void load(JSONObject obj) throws JSONException{ //here is my problem } }
My problem is: When I load AwesomeList from JSONObject, I need to create its elements but it's impossible since java forbids me to write
T newItem = new T(); newItem.load(obj);
How should I modify my approach to this task?
推荐答案
Are you tied to this library? Google Gson is very popular. I have myself not used it with Generics but their front page says Gson considers support for Generics very important.
其他推荐答案
As others have hinted, you should consider dumping org.json's library. It's pretty much obsolete these days, and trying to work around its problems is waste of time.
But to specific question; type variable T just does not have any information to help you, as it is little more than compile-time information. Instead you need to pass actual class (as 'Class cls' argument), and you can then create an instance with 'cls.newInstance()'.
其他推荐答案
Well, when writing it out to file, you do know what class T is, so you can store that in dump. Then, when reading it back in, you can dynamically call it using reflection.
public JSONObject dump() throws JSONException { JSONObject result = new JSONObject(); JSONArray a = new JSONArray(); for(T i : items){ a.put(i.dump()); // inside this i.dump(), store "class-name" } result.put("items", a); return result; } public void load(JSONObject obj) throws JSONException { JSONArray arrayItems = obj.getJSONArray("items"); for (int i = 0; i < arrayItems.length(); i++) { JSONObject item = arrayItems.getJSONObject(i); String className = item.getString("class-name"); try { Class<?> clazzy = Class.forName(className); T newItem = (T) clazzy.newInstance(); newItem.load(obj); items.add(newItem); } catch (InstantiationException e) { // whatever } catch (IllegalAccessException e) { // whatever } catch (ClassNotFoundException e) { // whatever } }