Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NextcloudArrayDeserializer.java « api « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ece1c1fe31cb247886c7838053b2a44c463568e (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
47
48
49
50
package it.niedermann.nextcloud.deck.api;

import android.util.Log;

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;
    }

    public static final String TAG = NextcloudArrayDeserializer.class.getCanonicalName();

    @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 {
            Log.d("deck", "ArrayDeserializer got an not-array");
        }
        return items;

    }


}