Skip to content

Commit 17030f9

Browse files
committed
Allowed other key types for the map deserialization helper
1 parent ddd3a3b commit 17030f9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/main/java/org/javawebstack/abstractdata/util/GsonMapDeserializer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9-
public abstract class GsonMapDeserializer<T> implements JsonDeserializer<Map<String, T>> {
10-
protected abstract Class<T> getType();
9+
public abstract class GsonMapDeserializer<K,V> implements JsonDeserializer<Map<K, V>> {
1110

12-
public Map<String, T> deserialize(JsonElement json, Type type, JsonDeserializationContext deserializationContext) throws JsonParseException {
11+
protected abstract Class<K> getKeyType();
12+
protected abstract Class<V> getValueType();
13+
14+
public Map<K, V> deserialize(JsonElement json, Type type, JsonDeserializationContext deserializationContext) throws JsonParseException {
1315
if (json == null || !json.isJsonObject())
1416
return null;
1517
JsonObject jsonObject = json.getAsJsonObject();
16-
Map<String, T> map = new HashMap<>();
17-
Class<?> t = getType();
18+
Map<K, V> map = new HashMap<>();
19+
Class<?> t = getValueType();
1820
for (String k : jsonObject.keySet())
19-
map.put(k, deserializationContext.deserialize(jsonObject.get(k), t));
21+
map.put(deserializationContext.deserialize(new JsonPrimitive(k), getKeyType()), deserializationContext.deserialize(jsonObject.get(k), t));
2022
return map;
2123
}
2224
}

0 commit comments

Comments
 (0)