- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
String response = HttpLoader.loadString(params[0]);
Gson gson = new GsonBuilder().registerTypeAdapter(ArrayList.class,
new JsonCollectionSerializer<ArrayList<MyClass>>()).
create();
ArrayList<MyClass> items_generic = new ArrayList<MyClass>() { };
ArrayList<MyClass> items = gson.fromJson(response, items_generic.getClass().getGenericSuperclass());
return items;
public class JsonCollectionSerializer<E> implements
JsonSerializer<Collection<E>>, JsonDeserializer<Collection<E>> {
@SuppressWarnings("unchecked")
public Collection<E> deserialize(JsonElement element, Type type,
JsonDeserializationContext context) throws JsonParseException {
JsonArray items = element.getAsJsonArray();
ParameterizedType deserializationCollectionType = ((ParameterizedType) type);
Type collectionItemType = deserializationCollectionType.getActualTypeArguments()[0];
Collection<E> list = null;
try {
list = (Collection<E>) ((Class<?>) deserializationCollectionType.getRawType()).newInstance();
for (JsonElement e : items) {
list.add((E) context.deserialize(e, collectionItemType));
}
} catch (InstantiationException e) {
throw new JsonParseException(e);
} catch (IllegalAccessException e) {
throw new JsonParseException(e);
}
return list;
}
}