Parsing generic ArrayList using GSON

删除回忆录丶 提交于 2019-12-25 08:57:20

问题


I'm having some trouble parsing a generic ArrayList with GSON. I've confirmed that this method works if the specific type for the ArrayList is specified (using no generics), but I'd like to write a method which works with generics.

Based on other stack overflow's questions, this should work, and I can't really understand why it doesn't. It fails to parse the ArrayList so all I get is a collection of LinkedTreeMap. The generic method is below:

public <T> List<T> listFromJson(@NonNull Class<T> classOfT ) throws JsonSyntaxException {
    if (data == null)
        return null;
    Gson gsonParser = new Gson();
    TypeToken<ArrayList<T>> token = new TypeToken<ArrayList<T>>() {};
    return gsonParser.fromJson(gsonParser.toJson(data), token.getType());
}

Edit: datais just an Objectwhich contains the json to be parsed. The instruction gsonParser.toJson(data) works just fine.

And I'm calling it like this, where MyClass might be any custom java class I've written:

List<MyClass> resList = result.listFromJson(MyClass.class);

Any help would be appreciated, I don't really know what the problem is but it's related with the templates for sure, as a non generic method would do the work.

Edit2: Just to clarify, I'm also including the non generics method which worked:

public  List<MyClass> listFromJson() throws JsonSyntaxException {
        Type listType = new TypeToken<ArrayList<MyClass>>(){}.getType();
        Gson gsonParser = new Gson();
        return gsonParser.fromJson(gsonParser.toJson(data), listType);
   }

回答1:


When I set data to a String[] then both of your implementations work for me. Are you using the same imports? I am using this gson version 'com.google.code.gson:gson:2.7'

import static org.junit.Assert.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class SomeTest {

  private String[] data = { "Hello" };

  public <T> List<T> listFromJson(Class<T> classOfT) throws JsonSyntaxException {
    if (data == null)
      return null;
    Gson gsonParser = new Gson();
    TypeToken<ArrayList<T>> token = new TypeToken<ArrayList<T>>() {};
    return gsonParser.fromJson(gsonParser.toJson(data), token.getType());
  }

  public List<String> listFromJson() throws JsonSyntaxException {
    Type listType = new TypeToken<ArrayList<String>>(){}.getType();
    Gson gsonParser = new Gson();
    return gsonParser.fromJson(gsonParser.toJson(data), listType);
}

  @Test
  public void parsingGenericArrayListUsingGSON_0() {
    List<String> actual = listFromJson(String.class);
    List<String> expected = new ArrayList<>();
    expected.add("Hello");
    assertEquals(expected, actual);
  }

  @Test
  public void parsingGenericArrayListUsingGSON_1() {
    List<String> actual = listFromJson();
    List<String> expected = new ArrayList<>();
    expected.add("Hello");
    assertEquals(expected, actual);
  }
}

You also do not have to pass the Class<T> classOfT parameter. Define the signature like this

 public <T> List<T> listFromJson() throws JsonSyntaxException


来源:https://stackoverflow.com/questions/39609761/parsing-generic-arraylist-using-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!