blob: 820cfc6538df54000da39cc6e0c822b2944ba1ab (
plain)
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
|
package it.niedermann.nextcloud.deck.api;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* Created by david on 24.05.17.
*/
public class NextcloudArrayDeserializer<T> implements JsonDeserializer<List<T>> {
protected final String mKey;
protected final Class<T> mType;
public NextcloudArrayDeserializer(String key, Class<T> type) {
this.mKey = key;
this.mType = type;
}
@Override
public List<T> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
List<T> items = new ArrayList<>();
if (json.isJsonArray()) {
JsonArray jArr = json.getAsJsonArray();
for (int i = 0; i < jArr.size(); i++) {
JsonObject obj = jArr.get(i).getAsJsonObject();
items.add(JsonToEntityParser.parseJsonObject(obj, mType));
}
} else {
throw new IllegalArgumentException("NextcloudArrayDeserializer got an non-array Json Object: " + json);
}
return items;
}
}
|