用gson反序列化泛型 [英] deserializing generics with gson
问题描述
我正在使用GSON 1.4并使用两个通用的 arraylist< myObject>
序列化一个对象,如下所示:
String data = Gson.toJson (object,object.class)
。当我desirialize它我做 gson.fromJson(json,类型);
可悲的是我得到
java.lang.IllegalArgumentException:无法将java.util.ArrayList
字段...设置为java.util.LinkedList
这是为什么? GSON doc指出,如果我使用object.class参数序列化,它支持泛型。任何想法?
我的课程是: b
$ b公共列表< IndexParams> indicesParams;
public List< WeightParams> weightsParams;
public IndicesAndWeightsParams(){
indicesParams = new ArrayList< IndexParams>();
weightsParams = new ArrayList< WeightParams>();
}
public IndicesAndWeightsParams(ArrayList< IndexParams> indicesParams,ArrayList< WeightParams> weightsParams){
this.indicesParams = indicesParams;
this.weightsParams = weightsParams;
$ b $ public class IndexParams {
public IndexParams(){
}
public IndexParams(String key,float value,String名称){
this.key = key;
this.value = value;
this.name = name;
}
public String key;
公共浮动值;
公共字符串名称;
由于Java的类型擦除。您可以阅读更多有关此处的信息。
从你的问题中我看到你正在使用 ArrayList
和 LinkedList
。你确定你不是只想使用 List
,接口?
这段代码有效: p>
列表< String> listOfStrings = new ArrayList< String>();
listOfStrings.add(one);
listOfStrings.add(two);
Gson gson = new Gson();
String json = gson.toJson(listOfStrings);
System.out.println(json);
Type type = new TypeToken< Collection< String>>(){}。getType();
列表< String> fromJson = gson.fromJson(json,type);
System.out.println(fromJson);
更新:我将您的课程更改为此,所以我不不得不乱用其他类:
class IndicesAndWeightsParams {
public List< Integer> indicesParams;
public List< String> weightsParams;
public IndicesAndWeightsParams(){
indicesParams = new ArrayList< Integer>();
weightsParams = new ArrayList< String>();
}
public IndicesAndWeightsParams(ArrayList< Integer> indicesParams,ArrayList< String> weightsParams){
this.indicesParams = indicesParams;
this.weightsParams = weightsParams;
$ / code>
使用这段代码,一切都适用于我: / p>
ArrayList< Integer> indices = new ArrayList< Integer>();
ArrayList< String> weights = new ArrayList< String>();
indices.add(2);
indices.add(5);
weights.add(five);
weights.add(twenty);
IndicesAndWeightsParams iaw =新的IndicesAndWeightsParams(指数,权重);
Gson gson = new Gson();
String string = gson.toJson(iaw);
System.out.println(string);
IndicesAndWeightsParams fromJson = gson.fromJson(string,IndicesAndWeightsParams.class);
System.out.println(fromJson.indicesParams);
System.out.println(fromJson.weightsParams);
I am using GSON 1.4 and serializing an object with two generic arraylist<myObject>
as follows
String data = Gson.toJson(object, object.class)
. When I desirialize it I do gson.fromJson(json, type);
sadly I get
java.lang.IllegalArgumentException: Can not set java.util.ArrayList field ... to java.util.LinkedList
Why is that ? GSON doc notes that if I serialize with object.class parameter it supports generics. any idea? thanks.
my class is :
public class IndicesAndWeightsParams {
public List<IndexParams> indicesParams;
public List<WeightParams> weightsParams;
public IndicesAndWeightsParams() {
indicesParams = new ArrayList<IndexParams>();
weightsParams = new ArrayList<WeightParams>();
}
public IndicesAndWeightsParams(ArrayList<IndexParams> indicesParams, ArrayList<WeightParams> weightsParams) {
this.indicesParams = indicesParams;
this.weightsParams = weightsParams;
}
}
public class IndexParams {
public IndexParams() {
}
public IndexParams(String key, float value, String name) {
this.key = key;
this.value = value;
this.name = name;
}
public String key;
public float value;
public String name;
}
Gson has some limitations regarding collections because of Java's type erasure. You can read more about it here.
From your question I see you're using both ArrayList
and LinkedList
. Are you sure you didn't mean to use just List
, the interface?
This code works:
List<String> listOfStrings = new ArrayList<String>();
listOfStrings.add("one");
listOfStrings.add("two");
Gson gson = new Gson();
String json = gson.toJson(listOfStrings);
System.out.println(json);
Type type = new TypeToken<Collection<String>>(){}.getType();
List<String> fromJson = gson.fromJson(json, type);
System.out.println(fromJson);
Update: I changed your class to this, so I don't have to mess around with other classes:
class IndicesAndWeightsParams {
public List<Integer> indicesParams;
public List<String> weightsParams;
public IndicesAndWeightsParams() {
indicesParams = new ArrayList<Integer>();
weightsParams = new ArrayList<String>();
}
public IndicesAndWeightsParams(ArrayList<Integer> indicesParams, ArrayList<String> weightsParams) {
this.indicesParams = indicesParams;
this.weightsParams = weightsParams;
}
}
And using this code, everything works for me:
ArrayList<Integer> indices = new ArrayList<Integer>();
ArrayList<String> weights = new ArrayList<String>();
indices.add(2);
indices.add(5);
weights.add("fifty");
weights.add("twenty");
IndicesAndWeightsParams iaw = new IndicesAndWeightsParams(indices, weights);
Gson gson = new Gson();
String string = gson.toJson(iaw);
System.out.println(string);
IndicesAndWeightsParams fromJson = gson.fromJson(string, IndicesAndWeightsParams.class);
System.out.println(fromJson.indicesParams);
System.out.println(fromJson.weightsParams);
这篇关于用gson反序列化泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!