14

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;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Bick
  • 17,833
  • 52
  • 146
  • 251

1 Answers1

23

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);
darioo
  • 46,442
  • 10
  • 75
  • 103
  • Hi, Thanks for the help. My object is not a generic but rather contains two arraylists. how should i use the type ?
    Type type = new TypeToken(){}.getTrype doesnt work :-(
    – Bick Dec 06 '10 at 09:31
  • btw : two arraylist with two differnet objects. I just changed it to List and List – Bick Dec 06 '10 at 09:33
  • @user450602: edit your post and add code of your object and your arraylists. I don't want to guess anything. – darioo Dec 06 '10 at 09:36
  • @user450602: not complete. I still don't know what `IndexParams` and `WeightParams` are and how do you initialize your class. – darioo Dec 06 '10 at 09:49
  • They hold just int and floats. they have a constructor that fills them and an empty constructor that does nothing. I will add one in a sec. – Bick Dec 06 '10 at 09:53
  • 1
    @user450602: updated my post. If you still have problems, post some code that constructs an `IndicesAndWeightsParams` object from scratch. – darioo Dec 06 '10 at 10:06
  • Related question: http://stackoverflow.com/questions/5813434/trouble-with-gson-serializing-an-arraylist-of-pojos – Nestor Ledon May 01 '15 at 03:20