All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getunleash.repository.JsonFeaturesDeserializer Maven / Gradle / Ivy

There is a newer version: 9.2.4
Show newest version
package io.getunleash.repository;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.getunleash.FeatureToggle;
import io.getunleash.Segment;
import io.getunleash.lang.Nullable;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

public class JsonFeaturesDeserializer implements JsonDeserializer {
    private static final Type PARAMS_TYPE = new TypeToken>() {}.getType();

    private static final Type SEGMENT_COLLECTION_TYPE =
            new TypeToken>() {}.getType();
    private static final Type TOGGLE_COLLECTION_TYPE =
            new TypeToken>() {}.getType();

    @Override
    public @Nullable FeatureCollection deserialize(
            JsonElement rootElement, Type type, JsonDeserializationContext context)
            throws JsonParseException {

        return deserializeVersion(rootElement, context);
    }

    static @Nullable FeatureCollection deserializeVersion(
            JsonElement rootElement, JsonDeserializationContext context) {
        if (!rootElement.getAsJsonObject().has("features")) {
            return null;
        }
        JsonObject root = rootElement.getAsJsonObject();
        JsonArray segmentArray = root.getAsJsonArray("segments");
        JsonArray togglesArray = root.getAsJsonArray("features");

        Collection toggles =
                context.deserialize(togglesArray, TOGGLE_COLLECTION_TYPE);
        Collection segments = context.deserialize(segmentArray, SEGMENT_COLLECTION_TYPE);
        return new FeatureCollection(
                new ToggleCollection(toggles), new SegmentCollection(segments));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy