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

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

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

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

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

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

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

        int version = getVersion(rootElement);

        switch (version) {
            case 0:
                return deserializeVersion0(rootElement, context);
            case 1:
            default:
                return deserializeVersion1(rootElement, context);
        }
    }

    static @Nullable ToggleCollection deserializeVersion0(
            JsonElement rootElement, JsonDeserializationContext context) {
        if (!rootElement.getAsJsonObject().has("features")) {
            return null;
        }

        Collection featureToggles = new ArrayList<>();

        JsonArray features = rootElement.getAsJsonObject().getAsJsonArray("features");

        features.forEach(
                elm -> {
                    JsonObject featureObj = elm.getAsJsonObject();

                    String name = featureObj.get("name").getAsString();
                    boolean enabled = featureObj.get("enabled").getAsBoolean();
                    String strategyName = featureObj.get("strategy").getAsString();
                    Map strategyParams =
                            context.deserialize(featureObj.get("parameters"), PARAMS_TYPE);

                    ActivationStrategy strategy =
                            new ActivationStrategy(strategyName, strategyParams);
                    featureToggles.add(
                            new FeatureToggle(name, enabled, singletonList(strategy), emptyList()));
                });

        return new ToggleCollection(featureToggles);
    }

    static @Nullable ToggleCollection deserializeVersion1(
            JsonElement rootElement, JsonDeserializationContext context) {
        if (!rootElement.getAsJsonObject().has("features")) {
            return null;
        }

        JsonArray featureArray = rootElement.getAsJsonObject().getAsJsonArray("features");

        Collection featureTgggles =
                context.deserialize(featureArray, FEATURE_COLLECTION_TYPE);
        return new ToggleCollection(featureTgggles);
    }

    private int getVersion(JsonElement rootElement) {
        if (!rootElement.getAsJsonObject().has("version")) {
            return 0;
        } else {
            return rootElement.getAsJsonObject().get("version").getAsInt();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy