Search in sources :

Example 1 with Condition

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

the class JsonSimpleConfigParser method parseAudiences.

private List<Audience> parseAudiences(JSONArray audienceJson) throws ParseException {
    JSONParser parser = new JSONParser();
    List<Audience> audiences = new ArrayList<Audience>(audienceJson.size());
    for (Object obj : audienceJson) {
        JSONObject audienceObject = (JSONObject) obj;
        String id = (String) audienceObject.get("id");
        String key = (String) audienceObject.get("name");
        String conditionString = (String) audienceObject.get("conditions");
        JSONArray conditionJson = (JSONArray) parser.parse(conditionString);
        Condition conditions = parseConditions(conditionJson);
        audiences.add(new Audience(id, key, conditions));
    }
    return audiences;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) JSONObject(org.json.simple.JSONObject) Audience(com.optimizely.ab.config.audience.Audience) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 2 with Condition

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

the class AudienceGsonDeserializer method deserialize.

@Override
public Audience deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonObject jsonObject = json.getAsJsonObject();
    String id = jsonObject.get("id").getAsString();
    String name = jsonObject.get("name").getAsString();
    JsonElement conditionsElement = parser.parse(jsonObject.get("conditions").getAsString());
    List<Object> rawObjectList = gson.fromJson(conditionsElement, List.class);
    Condition conditions = parseConditions(rawObjectList);
    return new Audience(id, name, conditions);
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) Audience(com.optimizely.ab.config.audience.Audience) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 3 with Condition

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

the class AudienceGsonDeserializer method parseConditions.

private Condition parseConditions(List<Object> rawObjectList) {
    List<Condition> conditions = new ArrayList<Condition>();
    String operand = (String) rawObjectList.get(0);
    for (int i = 1; i < rawObjectList.size(); i++) {
        Object obj = rawObjectList.get(i);
        if (obj instanceof List) {
            List<Object> objectList = (List<Object>) rawObjectList.get(i);
            conditions.add(parseConditions(objectList));
        } else {
            LinkedTreeMap<String, String> conditionMap = (LinkedTreeMap<String, String>) rawObjectList.get(i);
            conditions.add(new UserAttribute(conditionMap.get("name"), conditionMap.get("type"), conditionMap.get("value")));
        }
    }
    Condition condition;
    if (operand.equals("and")) {
        condition = new AndCondition(conditions);
    } else if (operand.equals("or")) {
        condition = new OrCondition(conditions);
    } else {
        condition = new NotCondition(conditions.get(0));
    }
    return condition;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) NotCondition(com.optimizely.ab.config.audience.NotCondition) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) ArrayList(java.util.ArrayList) AndCondition(com.optimizely.ab.config.audience.AndCondition) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) OrCondition(com.optimizely.ab.config.audience.OrCondition)

Example 4 with Condition

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

the class JsonConfigParser method parseConditions.

private Condition parseConditions(JSONArray conditionJson) {
    List<Condition> conditions = new ArrayList<Condition>();
    String operand = (String) conditionJson.get(0);
    for (int i = 1; i < conditionJson.length(); i++) {
        Object obj = conditionJson.get(i);
        if (obj instanceof JSONArray) {
            conditions.add(parseConditions(conditionJson.getJSONArray(i)));
        } else {
            JSONObject conditionMap = (JSONObject) obj;
            String value = null;
            if (conditionMap.has("value")) {
                value = conditionMap.getString("value");
            }
            conditions.add(new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), value));
        }
    }
    Condition condition;
    if (operand.equals("and")) {
        condition = new AndCondition(conditions);
    } else if (operand.equals("or")) {
        condition = new OrCondition(conditions);
    } else {
        condition = new NotCondition(conditions.get(0));
    }
    return condition;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) JSONObject(org.json.JSONObject) NotCondition(com.optimizely.ab.config.audience.NotCondition) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) OrCondition(com.optimizely.ab.config.audience.OrCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition)

Example 5 with Condition

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

the class AudienceJacksonDeserializer method parseConditions.

private Condition parseConditions(List<Object> rawObjectList) {
    List<Condition> conditions = new ArrayList<Condition>();
    String operand = (String) rawObjectList.get(0);
    for (int i = 1; i < rawObjectList.size(); i++) {
        Object obj = rawObjectList.get(i);
        if (obj instanceof List) {
            List<Object> objectList = (List<Object>) rawObjectList.get(i);
            conditions.add(parseConditions(objectList));
        } else {
            HashMap<String, String> conditionMap = (HashMap<String, String>) rawObjectList.get(i);
            conditions.add(new UserAttribute(conditionMap.get("name"), conditionMap.get("type"), conditionMap.get("value")));
        }
    }
    Condition condition;
    if (operand.equals("and")) {
        condition = new AndCondition(conditions);
    } else if (operand.equals("or")) {
        condition = new OrCondition(conditions);
    } else {
        condition = new NotCondition(conditions.get(0));
    }
    return condition;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) HashMap(java.util.HashMap) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) ArrayList(java.util.ArrayList) AndCondition(com.optimizely.ab.config.audience.AndCondition) ArrayList(java.util.ArrayList) List(java.util.List) OrCondition(com.optimizely.ab.config.audience.OrCondition)

Aggregations

AndCondition (com.optimizely.ab.config.audience.AndCondition)11 Condition (com.optimizely.ab.config.audience.Condition)11 NotCondition (com.optimizely.ab.config.audience.NotCondition)11 OrCondition (com.optimizely.ab.config.audience.OrCondition)11 ArrayList (java.util.ArrayList)10 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)7 Audience (com.optimizely.ab.config.audience.Audience)6 HashMap (java.util.HashMap)3 List (java.util.List)3 JsonObject (com.google.gson.JsonObject)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1