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

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

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

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import io.getunleash.FeatureToggle;
import io.getunleash.Segment;
import java.lang.reflect.Type;
import java.util.Collection;

public class JsonFeatureSerializer implements JsonSerializer {

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

    @Override
    public JsonElement serialize(
            FeatureCollection featureCollection,
            Type type,
            JsonSerializationContext jsonSerializationContext) {
        JsonObject json = new JsonObject();

        json.addProperty("version", 2);
        json.add(
                "features",
                jsonSerializationContext
                        .serialize(
                                featureCollection.getToggleCollection().getFeatures(),
                                TOGGLE_COLLECTION_TYPE)
                        .getAsJsonArray());
        json.add(
                "segments",
                jsonSerializationContext
                        .serialize(
                                featureCollection.getSegmentCollection().getSegments(),
                                SEGMENT_COLLECTION_TYPE)
                        .getAsJsonArray());

        return json;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy