Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing ParameterizedType to Parser #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 5 additions & 28 deletions hawk/src/main/java/com/orhanobut/hawk/HawkConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,8 @@ private <T> T toList(String json, Class<?> type) throws Exception {
}
List<T> list = parser.fromJson(
json,
new TypeToken<List<T>>() {
}.getType()
TypeToken.getParameterized(List.class, type).getType()
);

int size = list.size();
for (int i = 0; i < size; i++) {
list.set(i, (T) parser.fromJson(parser.toJson(list.get(i)), type));
}
return (T) list;
}

Expand All @@ -85,15 +79,8 @@ private <T> T toSet(String json, Class<?> type) throws Exception {
if (type == null) {
return (T) resultSet;
}
Set<T> set = parser.fromJson(json, new TypeToken<Set<T>>() {
}.getType());

for (T t : set) {
String valueJson = parser.toJson(t);
T value = parser.fromJson(valueJson, type);
resultSet.add(value);
}
return (T) resultSet;
Set<T> set = parser.fromJson(json, TypeToken.getParameterized(Set.class, type).getType());
return (T) set;
}

@SuppressWarnings("unchecked")
Expand All @@ -102,18 +89,8 @@ private <K, V, T> T toMap(String json, Class<?> keyType, Class<?> valueType) thr
if (keyType == null || valueType == null) {
return (T) resultMap;
}
Map<K, V> map = parser.fromJson(json, new TypeToken<Map<K, V>>() {
}.getType());

for (Map.Entry<K, V> entry : map.entrySet()) {
String keyJson = parser.toJson(entry.getKey());
K k = parser.fromJson(keyJson, keyType);

String valueJson = parser.toJson(entry.getValue());
V v = parser.fromJson(valueJson, valueType);
resultMap.put(k, v);
}
return (T) resultMap;
Map<K, V> map = parser.fromJson(json, TypeToken.getParameterized(Map.class, keyType, valueType).getType());
return (T) map;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public class HawkFacadeIntegrationTest {
assertThat(list1).isNotNull();
}

@Test public void testBeanList() {
List<FooBar> list = new ArrayList<>();
list.add(new FooBar());
Hawk.put("tag", list);

List<FooBar> list1 = Hawk.get("tag");

assertThat(list1).isNotNull();
assertThat(list1.get(0).getName()).isEqualTo("hawk");
}

@Test public void testMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
Expand Down Expand Up @@ -178,5 +189,4 @@ public class HawkFacadeIntegrationTest {
}
assertThat(true).isTrue();
}

}