I want to transfer a list object via Google Gson, but I don\'t know how to deserialize generic types.
What I tried after looking at this (BalusC\'s answer):
I want to add for one more possibility. If you don't want to use TypeToken and want to convert json objects array to an ArrayList, then you can proceed like this:
If your json structure is like:
{
"results": [
{
"a": 100,
"b": "value1",
"c": true
},
{
"a": 200,
"b": "value2",
"c": false
},
{
"a": 300,
"b": "value3",
"c": true
}
]
}
and your class structure is like:
public class ClassName implements Parcelable {
public ArrayList<InnerClassName> results = new ArrayList<InnerClassName>();
public static class InnerClassName {
int a;
String b;
boolean c;
}
}
then you can parse it like:
Gson gson = new Gson();
final ClassName className = gson.fromJson(data, ClassName.class);
int currentTotal = className.results.size();
Now you can access each element of className object.
Refer to this post. Java Type Generic as Argument for GSON
I have better solution for this. Here's the wrapper class for list so the wrapper can store the exactly type of list.
public class ListOfJson<T> implements ParameterizedType
{
private Class<?> wrapped;
public ListOfJson(Class<T> wrapper)
{
this.wrapped = wrapper;
}
@Override
public Type[] getActualTypeArguments()
{
return new Type[] { wrapped };
}
@Override
public Type getRawType()
{
return List.class;
}
@Override
public Type getOwnerType()
{
return null;
}
}
And then, the code can be simple:
public static <T> List<T> toList(String json, Class<T> typeClass)
{
return sGson.fromJson(json, new ListOfJson<T>(typeClass));
}
In My case @uncaught_exceptions's answer didn't work, I had to use List.class
instead of java.lang.reflect.Type
:
String jsonDuplicatedItems = request.getSession().getAttribute("jsonDuplicatedItems").toString();
List<Map.Entry<Product, Integer>> entries = gson.fromJson(jsonDuplicatedItems, List.class);
As it answers my original question, I have accepted doc_180's answer, but if someone runs into this problem again, I will answer the 2nd half of my question as well:
The NullPointerError I described had nothing to do with the List itself, but with its content!
The "MyClass" class didn't have a "no args" constructor, and neither had its superclass one. Once I added a simple "MyClass()" constructor to MyClass and its superclass, everything worked fine, including the List serialization and deserialization as suggested by doc_180.
Wep, another way to achieve the same result. We use it for its readability.
Instead of doing this hard-to-read sentence:
Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> list = new Gson().fromJson(jsonArray, listType);
Create a empty class that extends a List of your object:
public class YourClassList extends ArrayList<YourClass> {}
And use it when parsing the JSON:
List<YourClass> list = new Gson().fromJson(jsonArray, YourClassList.class);
Refer to example 2 for 'Type' class understanding of Gson.
Example 1: In this deserilizeResturant we used Employee[] array and get the details
public static void deserializeResturant(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
Employee[] emp = gson.fromJson(empList, Employee[].class);
int numberOfElementInJson = emp.length();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}
Example 2:
//Above deserilizeResturant used Employee[] array but what if we need to use List<Employee>
public static void deserializeResturantUsingList(){
String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
Gson gson = new Gson();
// Additionally we need to se the Type then only it accepts List<Employee> which we sent here empTypeList
Type empTypeList = new TypeToken<ArrayList<Employee>>(){}.getType();
List<Employee> emp = gson.fromJson(empList, empTypeList);
int numberOfElementInJson = emp.size();
System.out.println("Total JSON Elements" + numberOfElementInJson);
for(Employee e: emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}