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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations