MultiDimension JSON Array Parser 30/99830/2
authorSingal, Kapil (ks220y) <ks220y@att.com>
Thu, 19 Dec 2019 20:19:53 +0000 (15:19 -0500)
committerSingal, Kapil (ks220y) <ks220y@att.com>
Fri, 20 Dec 2019 15:29:54 +0000 (10:29 -0500)
Adding Code to support MutiDimensional json parsing logic

Change-Id: I487f25c9c7eaeb466ac6903bea80175fa4c50728
Issue-ID: CCSDK-2008
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java
restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java
restapi-call-node/provider/src/test/resources/1dArray.json [new file with mode: 0644]
restapi-call-node/provider/src/test/resources/3dArray.json

index 4c3ba7f..60d43df 100644 (file)
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 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;
@@ -42,6 +43,46 @@ public final class JsonParser {
         // 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 {
@@ -50,27 +91,13 @@ public final class JsonParser {
 
         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();
@@ -86,9 +113,6 @@ public final class JsonParser {
             }
 
             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);
@@ -96,7 +120,6 @@ public final class JsonParser {
 
                     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;
@@ -113,8 +136,7 @@ public final class JsonParser {
                     } 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));
index 569719d..cdffd45 100644 (file)
@@ -134,7 +134,23 @@ public class TestJsonParser {
         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)));
@@ -145,6 +161,7 @@ public class TestJsonParser {
         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]"));
@@ -152,28 +169,48 @@ public class TestJsonParser {
         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
@@ -216,6 +253,7 @@ public class TestJsonParser {
         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"));
     }
 
 }
diff --git a/restapi-call-node/provider/src/test/resources/1dArray.json b/restapi-call-node/provider/src/test/resources/1dArray.json
new file mode 100644 (file)
index 0000000..208fb70
--- /dev/null
@@ -0,0 +1,8 @@
+[\r
+  "apple",\r
+  "orange",\r
+  "banana",\r
+  "squash",\r
+  "broccoli",\r
+  "cauliflower"\r
+]
\ No newline at end of file
index 1499555..3f3f4fa 100644 (file)
@@ -1,4 +1,5 @@
 [\r
-       [["a","b","c"], ["d","e","f"]],\r
-       [["x","y","z"]]\r
+       [["x","y","z"]],\r
+       [["abc","def","xyz"], [123, 456, 789]],\r
+       [["a","b","c"], ["d","e","f"], ["1","2","3"]]\r
 ]
\ No newline at end of file