* to convert a JSONObject to an XML Doc. The intent of this is to\r
* correctly generate XML from JSON including TAGs for JSONArrays\r
*\r
- * @param jsonObj org.json.JSON object to be converted to XML\r
+ * @param obj object to be converted to XML\r
* @param tagName optional XML tagname supplied primarily during recursive calls\r
* @return String containing the XML translation\r
*/\r
public static String toXMLString(Object obj, String tagName) throws JSONException {\r
- StringBuffer strBuf = new StringBuffer();\r
+ StringBuilder strBuf = new StringBuilder();\r
int i;\r
JSONArray jsonArr;\r
JSONObject jsonObj;\r
jsonObj = (JSONObject) obj;\r
keys = jsonObj.keys();\r
while (keys.hasNext()) {\r
- key = keys.next().toString();\r
+ key = keys.next();\r
// msoLogger.debug("toXMLString(): key is " + k);\r
curObj = jsonObj.opt(key);\r
if (curObj == null) {\r
str = null;\r
}\r
// append the content to the XML output\r
- if (key.equals("content")) {\r
+ if ("content".equals(key)) {\r
if (curObj instanceof JSONArray) {\r
jsonArr = (JSONArray) curObj;\r
len = jsonArr.length();\r
return null;\r
} else {\r
if (rawValue instanceof String) {\r
- msoLogger.debug("getJsonValue(): the raw value is a String Object=" + ((String) rawValue).toString());\r
+ msoLogger.debug("getJsonValue(): the raw value is a String Object=" + rawValue);\r
return (String) rawValue;\r
} else {\r
msoLogger.debug("getJsonValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
return null;\r
} else {\r
if (rawValue instanceof String) {\r
- msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + ((String) rawValue).toString());\r
+ msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + rawValue);\r
return (String) rawValue;\r
} else {\r
msoLogger.debug("getJsonNodeValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
return 0;\r
} else {\r
if (rawValue instanceof Integer) {\r
- msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + ((String) rawValue).toString());\r
+ msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + rawValue);\r
return (Integer) rawValue;\r
} else {\r
msoLogger.debug("getJsonValue(): the raw value is NOT an Integer Object=" + rawValue.toString());\r
* @return boolean field value associated with keys - default is false\r
*/\r
public static boolean getJsonBooleanValue(String jsonStr, String keys) {\r
- String isDebugLogEnabled = "true";\r
try {\r
Object rawValue = getJsonRawValue(jsonStr, keys);\r
if (rawValue == null) {\r
return false;\r
} else {\r
if (rawValue instanceof Boolean) {\r
- msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + ((String) rawValue).toString());\r
+ msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + rawValue);\r
return (Boolean) rawValue;\r
} else {\r
msoLogger.debug("getJsonValue(): the raw value is NOT an Boolean Object=" + rawValue.toString());\r
return null;\r
} else {\r
if (rawValue instanceof JSONArray) {\r
- msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + ((JSONArray) rawValue).toString());\r
+ msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + rawValue.toString());\r
int arrayLen = ((JSONArray) rawValue).length();\r
if (index < 0 || arrayLen < index+1) {\r
msoLogger.debug("getJsonParamValue(): index: " + index + " is out of bounds for array size of " + arrayLen);\r
// String isDebugLogEnabled = "true";\r
try {\r
JSONObject jsonObj = new JSONObject(jsonStr);\r
- if (jsonObj != null) {\r
- return getJsonValueForKey(jsonObj, key);\r
- }\r
+ return getJsonValueForKey(jsonObj, key);\r
} catch (Exception e) {\r
msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(), e);\r
}\r
msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
try {\r
if (jsonObj.has(key)) {\r
msoLogger.debug("getJsonValueForKey(): found value for key=" + key);\r
- return ((Integer) jsonObj.get(key));\r
+ return (Integer) jsonObj.get(key);\r
} else {\r
msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
* @return String field value associated with key\r
*/\r
public static Boolean getJsonBooleanValueForKey(JSONObject jsonObj, String key) {\r
- String isDebugLogEnabled = "true";\r
Boolean keyValue = false;\r
try {\r
if (jsonObj.has(key)) {\r
msoLogger.debug("getJsonBooleanValueForKey(): found value for key=" + key);\r
- return ((Boolean) jsonObj.get(key));\r
+ return (Boolean) jsonObj.get(key);\r
} else {\r
msoLogger.debug("getJsonBooleanValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
if (!jsonValueExists(jsonStr, keys)) {\r
return putJsonValue(jsonStr, keys, value);\r
} else {\r
- msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + (String) value + " already exists");\r
+ msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + value + " already exists");\r
return jsonStr;\r
}\r
}\r
msoLogger.debug("putJsonValue(): key=" + keyStr + " points to json object");\r
jsonObj = (JSONObject) keyValue;\r
} else {\r
- msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + (String) keyValue);\r
+ msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + keyValue);\r
return null;\r
}\r
} else { // at the last/new key value\r
public Map<String, String> entryArrayToMap(Execution execution, String entryArray) {\r
msoLogger.debug("Started Entry Array To Map Util Method");\r
\r
- Map<String, String> map = new HashMap<String, String>();\r
+ Map<String, String> map = new HashMap<>();\r
\r
//Populate Map\r
String entryListJson = "{ \"entry\":" + entryArray + "}";\r
public Map<String, String> entryArrayToMap(Execution execution, String entryArray, String keyNode, String valueNode) {\r
msoLogger.debug("Started Entry Array To Map Util Method");\r
\r
- Map<String, String> map = new HashMap<String, String>();\r
+ Map<String, String> map = new HashMap<>();\r
//Populate Map\r
String entryListJson = "{ \"entry\":" + entryArray + "}";\r
JSONObject obj = new JSONObject(entryListJson);\r
* and puts it in a Java List<String>.\r
*\r
* @param execution\r
- * @param stringArray - the getJsonValue of a json array of strings\r
+ * @param jsonArrayOfStrings - the getJsonValue of a json array of strings\r
*\r
* @return List - a java list containing the strings\r
*\r
public List<String> StringArrayToList(Execution execution, String jsonArrayOfStrings) {\r
msoLogger.debug("Started String Array To List Util Method");\r
\r
- List<String> list = new ArrayList<String>();\r
+ List<String> list = new ArrayList<>();\r
//Populate List\r
String stringListJson = "{ \"strings\":" + jsonArrayOfStrings + "}";\r
JSONObject obj = new JSONObject(stringListJson);\r
JSONArray arr = obj.getJSONArray("strings");\r
for (int i = 0; i < arr.length(); i++){\r
- String s = arr.getString(i);\r
+ String s = arr.get(i).toString();\r
list.add(s);\r
}\r
msoLogger.debug("Outgoing List is: " + list);\r
\r
try {\r
Object rawValue = getJsonRawValue(jsonStr, keys);\r
- if (rawValue == null) {\r
- return false;\r
- } else {\r
- return true;\r
- }\r
+\r
+ return !(rawValue == null);\r
+\r
} catch (Exception e) {\r
msoLogger.debug("jsonElementExist(): unable to determine if json element exist. Exception is: " + e.toString(),e);\r
}\r