1

I have method like his:

private <E extends RealmObject> E getFromFile(
        Context context,
        String jsonPath) throws IOException {
    InputStream inputStream = context.getAssets().open(jsonPath);
    Gson gson = new Gson();
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    Type typeOfT = new TypeToken<ArrayList<MyClass>>(){}.getType();
    return gson.fromJson(reader, typeOfT);
}

And I want to ask is it possible to get TypeToken of ArrayList of object from given class name. Something like this:

private Type getArrayListTypeFrom(Class clazz) {
    return new TypeToken<ArrayList<clazz>>(){}.getType(); //how to make it work.
}

I read this: Google Gson - deserialize list<class> object? (generic type), but I don't found solution.

Wrobel
  • 1,042
  • 12
  • 21
  • 3
    Second answer of this: https://stackoverflow.com/questions/20773850/gson-typetoken-with-dynamic-arraylist-item-type It looks like the question you linked also has a very similar answer. – Jorn Vernee Jun 06 '17 at 13:02
  • 1
    I used the solution from Java Type Generic as Argument for GSON (TypeToken.getParameterized(List.class, myType).getType() I need to import gson version 2.8.0 not 2.7.0 which was original included with Retrofit2. – Wrobel Jun 06 '17 at 13:33

0 Answers0