import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
// Preventing instantiation of the same.
}
+
+ private static void handleJsonArray(String key, Map<String, Object> jArrayMap, JSONArray jsonArr) throws JSONException {
+ JSONObject jsonObj;
+ JSONArray subJsonArr;
+ boolean stripKey = false;
+
+ for (int i = 0, length = jsonArr.length(); i < length; i++) {
+ if (stripKey)
+ key = key.substring(0, key.length()-3);
+
+ subJsonArr = jsonArr.optJSONArray(i);
+ if (subJsonArr != null) {
+ key = StringUtils.trimToEmpty(key) + "[" + i + "]";
+ jArrayMap.putIfAbsent(key + "_length", String.valueOf(subJsonArr.length()));
+ handleJsonArray(key, jArrayMap, subJsonArr);
+ stripKey = true;
+ continue;
+ }
+
+ jsonObj = jsonArr.optJSONObject(i);
+ if (jsonObj != null) {
+ Iterator<String> ii = jsonObj.keys();
+ while (ii.hasNext()) {
+ String nodeKey = ii.next();
+ String key1 = "[" + i + "]." + nodeKey;
+ String[] subKey = key1.split(":");
+ if (subKey.length == 2) {
+ jArrayMap.putIfAbsent(subKey[1], jsonObj.get(nodeKey));
+ } else {
+ jArrayMap.putIfAbsent(key1, jsonObj.get(nodeKey));
+ }
+ }
+ }
+ else {
+ jArrayMap.putIfAbsent(StringUtils.trimToEmpty(key), jsonArr);
+ break;
+ }
+ }
+ }
+
@SuppressWarnings("unchecked")
public static Map<String, String> convertToProperties(String s)
throws SvcLogicException {
try {
Map<String, Object> wm = new HashMap<>();
- String topLvlArrLength = null;
JSONObject json;
JSONArray jsonArr;
//support top level list in json response
if (s.startsWith("[")) {
jsonArr = new JSONArray(s);
- topLvlArrLength = String.valueOf(jsonArr.length());
- for (int i = 0, length = jsonArr.length(); i < length; i++) {
- json = jsonArr.getJSONObject(i);
- Iterator<String> ii = json.keys();
- while (ii.hasNext()) {
- String key = ii.next();
- String key1 = "[" + i + "]." + key;
- String[] subKey = key1.split(":");
- if (subKey.length == 2) {
- wm.put(subKey[1], json.get(key));
- } else {
- wm.put(key1, json.get(key));
- }
- }
- }
+ wm.put("_length", String.valueOf(jsonArr.length()));
+ handleJsonArray(null, wm, jsonArr);
} else {
json = new JSONObject(s);
Iterator<String> ii = json.keys();
}
Map<String, String> mm = new HashMap<>();
- if (topLvlArrLength != null) {
- mm.put("_length", topLvlArrLength);
- }
while (!wm.isEmpty()) {
for (String key : new ArrayList<>(wm.keySet())) {
Object o = wm.get(key);
if (o instanceof Boolean || o instanceof Number || o instanceof String) {
mm.put(key, o.toString());
-
log.info("Added property: {} : {}", key, o.toString());
} else if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
} else if (o instanceof JSONArray) {
JSONArray ja = (JSONArray) o;
mm.put(key + "_length", String.valueOf(ja.length()));
-
- log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
+ log.info("Added property: {}_length: {}", key, ja.length());
for (int i = 0; i < ja.length(); i++) {
wm.put(key + '[' + i + ']', ja.get(i));
assertEquals("3", mm.get("menu_length"));
}
- @Test(expected = SvcLogicException.class) // current behavior is multidimensional arrays are not supported
+ @Test
+ public void test1dJsonStringToCtx() throws Exception {
+ String path = "src/test/resources/1dArray.json";
+ String content = new String(Files.readAllBytes(Paths.get(path)));
+ Map<String, String> mm = JsonParser.convertToProperties(content);
+
+ System.out.println(mm);
+ assertEquals("6", mm.get("_length"));
+ assertEquals("apple", mm.get("[0]"));
+ assertEquals("orange", mm.get("[1]"));
+ assertEquals("banana", mm.get("[2]"));
+ assertEquals("squash", mm.get("[3]"));
+ assertEquals("broccoli", mm.get("[4]"));
+ assertEquals("cauliflower", mm.get("[5]"));
+ }
+
+ @Test
public void test2dJsonStringToCtx() throws Exception {
String path = "src/test/resources/2dArray.json";
String content = new String(Files.readAllBytes(Paths.get(path)));
assertEquals("orange", mm.get("[0][1]"));
assertEquals("banana", mm.get("[0][2]"));
assertEquals("3", mm.get("[0]_length"));
+
assertEquals("squash", mm.get("[1][0]"));
assertEquals("broccoli", mm.get("[1][1]"));
assertEquals("cauliflower", mm.get("[1][2]"));
assertEquals("2", mm.get("_length"));
}
- @Test(expected = SvcLogicException.class) // current behavior is multidimensional arrays are not supported
+ @Test
public void test3dJsonStringToCtx() throws Exception {
String path = "src/test/resources/3dArray.json";
String content = new String(Files.readAllBytes(Paths.get(path)));
Map<String, String> mm = JsonParser.convertToProperties(content);
- // code will crash before these tests
- assertEquals("a", mm.get("[0][0][0]"));
- assertEquals("b", mm.get("[0][0][1]"));
- assertEquals("c", mm.get("[0][0][2]"));
+ assertEquals("3", mm.get("_length"));
+ assertEquals("1", mm.get("[0]_length"));
assertEquals("3", mm.get("[0][0]_length"));
- assertEquals("d", mm.get("[0][1][0]"));
- assertEquals("e", mm.get("[0][1][1]"));
- assertEquals("f", mm.get("[0][1][2]"));
- assertEquals("3", mm.get("[0][1]_length"));
- assertEquals("2", mm.get("[0]_length"));
- assertEquals("x", mm.get("[1][0][0]"));
- assertEquals("y", mm.get("[1][0][1]"));
- assertEquals("z", mm.get("[1][0][2]"));
+
+ assertEquals("2", mm.get("[1]_length"));
assertEquals("3", mm.get("[1][0]_length"));
- assertEquals("1", mm.get("[1]_length"));
- assertEquals("2", mm.get("_length"));
+ assertEquals("3", mm.get("[1][1]_length"));
+
+ assertEquals("3", mm.get("[2]_length"));
+ assertEquals("3", mm.get("[2][0]_length"));
+ assertEquals("3", mm.get("[2][1]_length"));
+ assertEquals("3", mm.get("[2][2]_length"));
+
+ assertEquals("x", mm.get("[0][0][0]"));
+ assertEquals("y", mm.get("[0][0][1]"));
+ assertEquals("z", mm.get("[0][0][2]"));
+
+ assertEquals("abc", mm.get("[1][0][0]"));
+ assertEquals("def", mm.get("[1][0][1]"));
+ assertEquals("xyz", mm.get("[1][0][2]"));
+
+ assertEquals("123", mm.get("[1][1][0]"));
+ assertEquals("456", mm.get("[1][1][1]"));
+ assertEquals("789", mm.get("[1][1][2]"));
+
+ assertEquals("a", mm.get("[2][0][0]"));
+ assertEquals("b", mm.get("[2][0][1]"));
+ assertEquals("c", mm.get("[2][0][2]"));
+
+ assertEquals("d", mm.get("[2][1][0]"));
+ assertEquals("e", mm.get("[2][1][1]"));
+ assertEquals("f", mm.get("[2][1][2]"));
+
+ assertEquals("1", mm.get("[2][2][0]"));
+ assertEquals("2", mm.get("[2][2][1]"));
+ assertEquals("3", mm.get("[2][2][2]"));
}
@Test
mm = JsonParser.convertToProperties(mm.get("input.parameters[0].value"));
assertEquals("0.2.0.0/16", mm.get("[0].id"));
assertEquals("ge04::/64", mm.get("[1].id"));
+ assertEquals("2", mm.get("_length"));
}
}