在 Gson 中使用泛型类型
我正在尝试创建一个与 Google Gson 一起使用的通用类。我创建了类 GsonJsonConverterImplementation
。此类具有以下方法:
public T deserialize(String jsonString) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("MM/dd/yy HH:mm:ss");
Gson gson = builder.create();
return gson.fromJson(jsonString, T); // T.class etc. what goes here
}
目标是该方法应该能够与我设置的 GsonJsonConverterImplementation 所使用的任何类型一起使用。不幸的是, gson.fromJson(jsonString, T) 不起作用,使用 T.class 代替 T 也不起作用。我确信问题源于我缺乏理解Java 泛型类型。使用 Gson 泛型的正确方法是什么?
编辑
使用 Kris 的答案 我认为这应该可行。不幸的是,clazz 不能以这种方式使用,并且会导致编译器错误。使用 Gson 处理集合和泛型类型时,我有哪些选择?
public List<T> deserializeList(String jsonString, Class<T> clazz) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("MM/dd/yy HH:mm:ss");
Gson gson = builder.create();
Type listType = new TypeToken<clazz>(){}.getType(); // compiler error
return gson.fromJson(jsonString, listType);
}
I am trying to create a generic class for use with Google Gson. I've created the class GsonJsonConverterImplementation<T>
. This class has the following method:
public T deserialize(String jsonString) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("MM/dd/yy HH:mm:ss");
Gson gson = builder.create();
return gson.fromJson(jsonString, T); // T.class etc. what goes here
}
The goal is that this method should be able to work with whatever Type I have set my GsonJsonConverterImplementation to work with. Unfortunately, gson.fromJson(jsonString, T)
does not work, nor does using T.class
in place of T. I am sure the issue stems from my lack of understanding of Java generic types. What is the correct way of using a generic with Gson?
Edit
Using Kris's answer I would assume that this should work. Unfortunately, clazz cannot be used in this manner and causes a compiler error. What are my options for working with a collection and a generic type with Gson?
public List<T> deserializeList(String jsonString, Class<T> clazz) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("MM/dd/yy HH:mm:ss");
Gson gson = builder.create();
Type listType = new TypeToken<clazz>(){}.getType(); // compiler error
return gson.fromJson(jsonString, listType);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您尝试的最简单的方法是定义要在方法签名本身中返回的类的类型。您可以通过将“T”的实例传递给方法或要返回值的类来实现此目的。在您希望生成返回值的情况下,第二种方法更为典型。以下是使用 Gson 的此方法的示例:
用法:
The simplest approach for what you are attempting is to define the type of class which is to be returned in the method signature itself. You can do so by either passing an instance of 'T' to the method or the Class of the value to be returned. The second approach is the more typical in cases where you expect to generate a return value. Here is an example of this approach using Gson:
Usage:
要获取
List
的运行时类型,给定T
类,这会很糟糕,因为 JDK 认为我们不需要它,所以没有支持。您可以对 ParameterizedType 进行子类化,但这确实很糟糕。To get the runtime type of
List<T>
, given the class ofT
, it would suck because JDK didn't think we need it so there's no support. You can subclassParameterizedType
, but it really sucks.阅读它
并保存它
read it like
and save it like
如果您序列化了一个通用对象,例如
GenericClass
在反序列化时,您应该提供一个具有确切类型的包装类,例如:
然后您可以调用:
If you have serialized a generic object like
GenericClass<T>
While deserializing you should provide a wrapper class with exact type for example:
then you can call: