Trouble with Gson serializing an ArrayList of POJO’s

You need to give Gson information on the specific generic type of List you’re using (or any generic type you use with it). Particularly when deserializing JSON, it needs that information to be able to determine what type of object it should deserialize each array element to.

Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();
String s = gson.toJson(list, listOfTestObject);
List<TestObject> list2 = gson.fromJson(s, listOfTestObject);

This is documented in the Gson user guide.

Leave a Comment