Search in sources :

Example 1 with TypedAudience

use of com.optimizely.ab.config.audience.TypedAudience in project java-sdk by optimizely.

the class DatafileGsonDeserializer method deserialize.

@Override
public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String accountId = jsonObject.get("accountId").getAsString();
    String projectId = jsonObject.get("projectId").getAsString();
    String revision = jsonObject.get("revision").getAsString();
    String version = jsonObject.get("version").getAsString();
    int datafileVersion = Integer.parseInt(version);
    // generic list type tokens
    Type groupsType = new TypeToken<List<Group>>() {
    }.getType();
    Type experimentsType = new TypeToken<List<Experiment>>() {
    }.getType();
    Type attributesType = new TypeToken<List<Attribute>>() {
    }.getType();
    Type eventsType = new TypeToken<List<EventType>>() {
    }.getType();
    Type audienceType = new TypeToken<List<Audience>>() {
    }.getType();
    Type typedAudienceType = new TypeToken<List<TypedAudience>>() {
    }.getType();
    List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType);
    List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType);
    List<Attribute> attributes;
    attributes = context.deserialize(jsonObject.get("attributes"), attributesType);
    List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType);
    List<Audience> audiences = Collections.emptyList();
    if (jsonObject.has("audiences")) {
        audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType);
    }
    List<Audience> typedAudiences = null;
    if (jsonObject.has("typedAudiences")) {
        typedAudiences = context.deserialize(jsonObject.get("typedAudiences").getAsJsonArray(), typedAudienceType);
    }
    boolean anonymizeIP = false;
    if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V3.toString())) {
        anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    Boolean botFiltering = null;
    String sdkKey = null;
    String environmentKey = null;
    boolean sendFlagDecisions = false;
    if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V4.toString())) {
        Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
        }.getType();
        featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType);
        Type rolloutsType = new TypeToken<List<Rollout>>() {
        }.getType();
        rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
        if (jsonObject.has("sdkKey"))
            sdkKey = jsonObject.get("sdkKey").getAsString();
        if (jsonObject.has("environmentKey"))
            environmentKey = jsonObject.get("environmentKey").getAsString();
        if (jsonObject.has("botFiltering"))
            botFiltering = jsonObject.get("botFiltering").getAsBoolean();
        if (jsonObject.has("sendFlagDecisions"))
            sendFlagDecisions = jsonObject.get("sendFlagDecisions").getAsBoolean();
    }
    return new DatafileProjectConfig(accountId, anonymizeIP, sendFlagDecisions, botFiltering, projectId, revision, sdkKey, environmentKey, version, attributes, audiences, typedAudiences, events, experiments, featureFlags, groups, rollouts);
}
Also used : JsonObject(com.google.gson.JsonObject) List(java.util.List) TypedAudience(com.optimizely.ab.config.audience.TypedAudience) Audience(com.optimizely.ab.config.audience.Audience) Type(java.lang.reflect.Type)

Example 2 with TypedAudience

use of com.optimizely.ab.config.audience.TypedAudience in project java-sdk by optimizely.

the class TypedAudienceJacksonDeserializer method deserialize.

@Override
public TypedAudience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectCodec codec = parser.getCodec();
    JsonNode node = codec.readTree(parser);
    String id = node.get("id").textValue();
    String name = node.get("name").textValue();
    JsonNode conditionsJson = node.get("conditions");
    Condition conditions = ConditionJacksonDeserializer.<UserAttribute>parseCondition(UserAttribute.class, objectMapper, conditionsJson);
    return new TypedAudience(id, name, conditions);
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) TypedAudience(com.optimizely.ab.config.audience.TypedAudience) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec)

Example 3 with TypedAudience

use of com.optimizely.ab.config.audience.TypedAudience in project java-sdk by optimizely.

the class GsonConfigParserTest method parseTypedAudienceLeaf.

@Test
public void parseTypedAudienceLeaf() throws Exception {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("id", "123");
    jsonObject.addProperty("name", "blah");
    JsonObject userAttribute = new JsonObject();
    userAttribute.addProperty("name", "doubleKey");
    userAttribute.addProperty("type", "custom_attribute");
    userAttribute.addProperty("match", "lt");
    userAttribute.addProperty("value", 100.0);
    jsonObject.add("conditions", userAttribute);
    AudienceGsonDeserializer deserializer = new AudienceGsonDeserializer();
    Type audienceType = new TypeToken<List<TypedAudience>>() {
    }.getType();
    Audience audience = deserializer.deserialize(jsonObject, audienceType, null);
    assertNotNull(audience);
    assertNotNull(audience.getConditions());
}
Also used : Type(java.lang.reflect.Type) TypedAudience(com.optimizely.ab.config.audience.TypedAudience) Audience(com.optimizely.ab.config.audience.Audience) JsonObject(com.google.gson.JsonObject) List(java.util.List) Test(org.junit.Test)

Example 4 with TypedAudience

use of com.optimizely.ab.config.audience.TypedAudience in project java-sdk by optimizely.

the class DatafileJacksonDeserializer method deserialize.

@Override
public DatafileProjectConfig deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectCodec codec = parser.getCodec();
    JsonNode node = codec.readTree(parser);
    String accountId = node.get("accountId").textValue();
    String projectId = node.get("projectId").textValue();
    String revision = node.get("revision").textValue();
    String version = node.get("version").textValue();
    int datafileVersion = Integer.parseInt(version);
    List<Group> groups = JacksonHelpers.arrayNodeToList(node.get("groups"), Group.class, codec);
    List<Experiment> experiments = JacksonHelpers.arrayNodeToList(node.get("experiments"), Experiment.class, codec);
    List<Attribute> attributes = JacksonHelpers.arrayNodeToList(node.get("attributes"), Attribute.class, codec);
    List<EventType> events = JacksonHelpers.arrayNodeToList(node.get("events"), EventType.class, codec);
    List<Audience> audiences = Collections.emptyList();
    if (node.has("audiences")) {
        audiences = JacksonHelpers.arrayNodeToList(node.get("audiences"), Audience.class, codec);
    }
    List<TypedAudience> typedAudiences = null;
    if (node.has("typedAudiences")) {
        typedAudiences = JacksonHelpers.arrayNodeToList(node.get("typedAudiences"), TypedAudience.class, codec);
    }
    boolean anonymizeIP = false;
    if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V3.toString())) {
        anonymizeIP = node.get("anonymizeIP").asBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    String sdkKey = null;
    String environmentKey = null;
    Boolean botFiltering = null;
    boolean sendFlagDecisions = false;
    if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V4.toString())) {
        featureFlags = JacksonHelpers.arrayNodeToList(node.get("featureFlags"), FeatureFlag.class, codec);
        rollouts = JacksonHelpers.arrayNodeToList(node.get("rollouts"), Rollout.class, codec);
        if (node.hasNonNull("sdkKey")) {
            sdkKey = node.get("sdkKey").textValue();
        }
        if (node.hasNonNull("environmentKey")) {
            environmentKey = node.get("environmentKey").textValue();
        }
        if (node.hasNonNull("botFiltering")) {
            botFiltering = node.get("botFiltering").asBoolean();
        }
        if (node.hasNonNull("sendFlagDecisions")) {
            sendFlagDecisions = node.get("sendFlagDecisions").asBoolean();
        }
    }
    return new DatafileProjectConfig(accountId, anonymizeIP, sendFlagDecisions, botFiltering, projectId, revision, sdkKey, environmentKey, version, attributes, audiences, (List<Audience>) (List<? extends Audience>) typedAudiences, events, experiments, featureFlags, groups, rollouts);
}
Also used : TypedAudience(com.optimizely.ab.config.audience.TypedAudience) Audience(com.optimizely.ab.config.audience.Audience) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) TypedAudience(com.optimizely.ab.config.audience.TypedAudience) List(java.util.List)

Aggregations

TypedAudience (com.optimizely.ab.config.audience.TypedAudience)4 Audience (com.optimizely.ab.config.audience.Audience)3 List (java.util.List)3 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonObject (com.google.gson.JsonObject)2 Type (java.lang.reflect.Type)2 Condition (com.optimizely.ab.config.audience.Condition)1 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)1 Test (org.junit.Test)1