java – Gson fromJson反序列化泛型

在使用Android应用程序时,我遇到了一个问题,当我想制作一个最终的通用方法来发送HTTP请求(使用loopj)并反序列化它们(使用Gson).

正如您在使用gson.fromJson时可能知道的那样,您不能这样做:

gson.fromJson(responseBody, new TypeToken<T>() {}.getType());

要么

gson.fromJson(responseBody, new TypeToken<ArrayList<T>>() {}.getType())

您已经作为T传递的实际对象(或第二种情况下的实际对象列表)代替了LinkedTreeMap对象(或LinkedTreeMap对象列表).

如果你想要仍然想要反序列化泛型类型对象(-s),请参阅下面的答案.

 额外奖励:loopj gson的终极通用解决方案

感谢这些帖子的作者和评论者:

http://blog.xebia.com/2009/02/07/acessing-generic-types-at-runtime-in-java/

How to get concrete type of a generic interface

Java Type Generic as Argument for GSON

最佳答案
将JSON对象反序列化为泛型类型java对象

首先,我们需要获得泛型类型T的实际类.

我们可以通过传递类本身(Class< T> cl)或通过从具有泛型类型的对象(SomeObject< T> someObjectWithGenericType)获取类来实现.我将在示例中使用第二种情况.

然后我们需要创建一个Element< T>类的特殊对象.这将告诉Gson用于反序列化的类.

public <T> T getObject(String json, SomeObject<T> someObjectWithGenericType) {
    Class cl = getTypeClassOfObject(someObjWithGenericType);
    T object = gson.fromJson(json, new Element<T>(cl));
    return object;
}

private Class getTypeClassOfObject(Object obj) {
    return (Class) ((ParameterizedType) obj.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

private class Element<T> implements ParameterizedType {

    private Class<T> cl;

    public Element(Class<T> cl) {
        this.cl = cl;
    }

    public Type[] getActualTypeArguments() {
        return new Type[] {cl};
    }

    public Type getRawType() {
        return cl;
    }

    public Type getOwnerType() {
        return null;
    }
}

如果您的SomeObject< T>是一个接口(可能是一个回调函数,稍后将在loopj示例中看到),您可以使用此方法代替getTypeClassOfObject:

private Class getTypeClassOfInterfaceObject(Object obj) {
    return (Class) ((ParameterizedType) obj.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
}

将JSON数组反序列化为泛型类型java对象的列表

同样的想法,但我们有一个不同的特殊类来帮助Gson进行反序列化:

public <T> List<T> getList(String json, SomeObject<T> someObjectWithGenericType) {
    Class cl = getTypeClassOfObject(someObjWithGenericType);
    List<T> list = gson.fromJson(json, new ListWithElements<T>(cl));
    return list;
}

private class ListWithElements<T> implements ParameterizedType {

    private Class<T> elementsClass;

    public ListWithElements(Class<T> elementsClass) {
        this.elementsClass = elementsClass;
    }

    public Type[] getActualTypeArguments() {
        return new Type[] {elementsClass};
    }

    public Type getRawType() {
        return List.class;
    }

    public Type getOwnerType() {
        return null;
    }
}

奖金

正如您将在这里看到的,someObjectWithGenericType将成为具有泛型类型T的回调.
即使我使用loopj,我确信任何其他异步http客户端都可以用来实现相同的结果.

loopj Gson with generics:object

public <T> void getObject(String url, HashMap<String, String> paramsMap, final GetObjectCallback<T> callback) {
    RequestParams params = convertParams(paramsMap);
    client.get(url, params, new TextHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseBody) {
            try {
                Class cl = getTypeClassOfInterfaceObject(callback);
                T object = gson.fromJson(responseBody, new Element<T>(cl));
                if (object != null) {
                    callback.onSuccess(object);
                } else {
                    callback.onFailure();
                }
            } catch (Exception e) {
                e.printStackTrace();
                callback.onFailure();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
            error.printStackTrace();
            callback.onFailure();
        }
    });
}

private RequestParams convertParams(HashMap<String, String> paramsMap) {
    RequestParams params = new RequestParams();
    if (paramsMap != null) {
        for (String key : paramsMap.keySet()) {
            params.put(key, paramsMap.get(key));
        }
    }
    return params;
}

public interface GetObjectCallback<T> {
    void onSuccess(T item);
    void onFailure();
}

loopj Gson with generics:list

public <T> void getList(String url, HashMap<String, String> paramsMap, final GetListCallback<T> callback) {
    RequestParams params = convertParams(paramsMap);
    client.get(url, params, new TextHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseBody) {
            try {
                Class cl = getTypeClassOfInterfaceObject(callback);
                List<T> list  = gson.fromJson(responseBody, new ListWithElements<T>(cl));
                if (list != null) {
                    callback.onSuccess(list);
                } else {
                    callback.onFailure();
                }
            } catch (Exception e) {
                e.printStackTrace();
                callback.onFailure();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
            error.printStackTrace();
            callback.onFailure();
        }
    });
}

public interface GetListCallback<T> {
    void onSuccess(List<T> list);
    void onFailure();
}

用法:对象

api.getObject(URL, paramsMap, new GetObjectCallback<NewsItem>() {
    @Override
    public void onSuccess(NewsItem item) {
        // do something
    }

    @Override
    public void onFailure() {
        // do something
    }
});

用法:清单

api.getList(URL, paramsMap, new GetListCallback<Comment>() {
    @Override
    public void onSuccess(List<Comment> list) {
        // do something
    }

    @Override
    public void onFailure() {
        // do something
    }
});

任何改进都非常受欢迎!

转载注明原文:java – Gson fromJson反序列化泛型 - 代码日志