Escape quotes in json strings 87/85987/5
authorPatrick Brady <patrick.brady@att.com>
Mon, 22 Apr 2019 20:12:14 +0000 (13:12 -0700)
committerPatrick Brady <patrick.brady@att.com>
Mon, 22 Apr 2019 20:24:35 +0000 (13:24 -0700)
The JSONObject parser strips all text of backslash escape characters, even if it is
treaing the object as a string and not a series of json keys. If a string contains a
json object within it, the removal of these escape characters causes problems. Adding
the JSONObject.quote method on all strings to automatically add escape characters to
any strings which contain characters requiring escapes.

Change-Id: Ifac68844dd132971ba35803f1b9c03a5372bd500
Signed-off-by: Patrick Brady <patrick.brady@att.com>
Issue-ID: APPC-1576

appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java

index 6507161..fa1c0fe 100644 (file)
@@ -63,8 +63,17 @@ public class JSONTool {
                 wm.remove(key);
                 tryAddBlockKeys(blockKeys, mm, key, o);
                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
-                    mm.put(key, o.toString());
-                    log.info("Added property: " + key + ": " + o.toString());
+                    String oString = o.toString();
+                    //Add escape characters to the string in case it is a string representation
+                    //of a json object.
+                    oString = JSONObject.quote(oString);
+                    //Remove the surrouding quotes added by the JSONObject.quote() method.
+                    //JSONObject.quote() will always return, at minimum, a string with two quotes,
+                    //even if a null string is passed to it. So this substring method does not
+                    //need any checks.
+                    oString.substring(1, oString.length() - 1);
+                    mm.put(key, oString);
+                    log.info("Added property: " + key + ": " + oString);
                 } else if (o instanceof JSONObject) {
                     fill(wm, key, (JSONObject) o);
                 } else if (o instanceof JSONArray) {