4

I am using Square's JSON parsing library.i want to warp a common library. just like

 public synchronized <T> boolean put(String key, Class<T> clzz) {
    JsonAdapter<T> tJsonAdapter = mMoshi.adapter(clzz);
    String jsonString = tJsonAdapter.toJson(t);
    DiskLruCache.Editor editor;
    try {
        editor = mDiskLruCache.edit(key);
        editor.set(0, jsonString);
        editor.commit();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

 public synchronized <T> T get(String key, Type type) {
    try {
        String data = mDiskLruCache.get(key).getString(0);
        JsonAdapter<T> tJsonAdapter = mMoshi.adapter(type);
        return tJsonAdapter.fromJson(data);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

I can't get Generic class type.

  • Are you asking how to get the super class of your `clzz` object? If you want the **super class**, you can use: [Class.getSuperClass()](https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getSuperclass()) – DataDino Sep 28 '16 at 03:43
  • no,i just want that code support parse generic type in Moshi, – 欧阳祖翔 Sep 28 '16 at 08:13
  • Any news on this ? Could not find a good way to do this – Sn0wfreeze Jun 07 '17 at 11:40

0 Answers0