【发布时间】:2011-08-15 15:43:06
【问题描述】:
以下两种方法用于使用 Google Gson 包装反序列化:
public static <T> T Deserialize(String jsonData, Type t) {
T obj = null;
try {
obj = new Gson().fromJson(jsonData, t);
} catch (Exception e) {
Log.e(DEBUG_TAG, e.getMessage());
}
return obj;
}
public static <T> T Deserialize(String jsonData, Class<T> toClass) {
T obj = null;
try {
obj = new Gson().fromJson(jsonData, toClass);
} catch (Exception e) {
Log.e(DEBUG_TAG, e.getMessage());
}
return obj;
}
它们几乎相同,但我想不出一个聪明的方法来摆脱重复的代码。
有什么建议吗?
【问题讨论】:
-
我会小心删除其中一种方法。查看 JavaDoc:google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/…, java.lang.Class)。我很确定他们有充分的理由提供两种方法,一种用于泛型,另一种用于非泛型。
-
@home 感谢您指出这一点,我已经编辑了我的答案
标签: java refactoring gson code-duplication