no.finn.unleash.repository.JsonToggleParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
package no.finn.unleash.repository;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.Reader;
final class JsonToggleParser {
private JsonToggleParser() {
}
public static String toJsonString(ToggleCollection toggleCollection) {
Gson gson = new GsonBuilder().create();
return gson.toJson(toggleCollection);
}
public static ToggleCollection fromJson(Reader reader) throws IllegalStateException {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ToggleCollection.class, new JsonToggleCollectionDeserializer())
.create();
ToggleCollection gsonCollection = gson.fromJson(reader, ToggleCollection.class);
if(gsonCollection == null || gsonCollection.getFeatures() == null) {
throw new IllegalStateException("Could not extract toggles from json");
}
return new ToggleCollection(gsonCollection.getFeatures());
}
}