2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.bpmn.core.json;
\r
23 import java.util.Iterator;
\r
24 import java.util.Map;
\r
25 import java.util.HashMap;
\r
26 import java.util.StringTokenizer;
\r
28 import org.camunda.bpm.engine.runtime.Execution;
\r
29 import org.json.JSONArray;
\r
30 import org.json.JSONException;
\r
31 import org.json.JSONObject;
\r
32 import org.json.XML;
\r
34 //import org.openecomp.mso.bpmn.core.BPMNLogger;
\r
35 import org.openecomp.mso.bpmn.core.xml.XmlTool;
\r
36 import org.openecomp.mso.logger.MsoLogger;
\r
39 * Utility class for JSON processing
\r
44 public class JsonUtils {
\r
46 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
\r
47 private static int MSOJsonIndentFactor = 3;
\r
50 * Uses the JSONObject static method to convert a XML doc to JSON.
\r
52 * @param xml String containing the XML doc
\r
53 * @param pretty flag to determine if the output should be formatted
\r
54 * @return String containing the JSON translation
\r
56 public static String xml2json(String xml, Boolean pretty) {
\r
57 // String isDebugLogEnabled = "true";
\r
59 // name spaces cause problems, so just remove them
\r
60 JSONObject jsonObj = XML.toJSONObject(XmlTool.removeNamespaces(xml));
\r
62 return jsonObj.toString();
\r
64 // add an indent to make it 'pretty'
\r
65 return jsonObj.toString(MSOJsonIndentFactor);
\r
67 } catch (Exception e){
\r
68 msoLogger.debug("xml2json(): unable to parse xml and convert to json. Exception was: " + e.toString());
\r
74 * Invokes xml2json(String, Boolean) defaulting to 'pretty' output.
\r
76 * @param xml String containing the XML doc
\r
77 * @return String containing the JSON translation
\r
79 public static String xml2json(String xml) {
\r
80 return xml2json(xml, true);
\r
84 * Uses the JSONObject static method to convert a JSON doc to XML.
\r
85 * Note: this method may not generate valid XML if the JSONObject
\r
86 * contains JSONArrays which are used to represent XML attributes
\r
89 * @param jsonStr String containing the JSON doc
\r
90 * @param pretty flag to determine if the output should be formatted
\r
91 * @return String containing the XML translation
\r
93 public static String json2xml(String jsonStr, Boolean pretty) {
\r
94 // String isDebugLogEnabled = "true";
\r
96 JSONObject jsonObj = new JSONObject(jsonStr);
\r
98 // use the local class method which properly handles certain JSONArray content
\r
99 return XmlTool.normalize(toXMLString(jsonObj, null));
\r
101 // use the local class method which properly handles certain JSONArray content
\r
102 return toXMLString(jsonObj, null);
\r
104 } catch (Exception e){
\r
105 msoLogger.debug("json2xml(): unable to parse json and convert to xml. Exception was: " + e.toString());
\r
111 * Uses a modified version of the org.json.XML toString() algorithm
\r
112 * to convert a JSONObject to an XML Doc. The intent of this is to
\r
113 * correctly generate XML from JSON including TAGs for JSONArrays
\r
115 * @param jsonObj org.json.JSON object to be converted to XML
\r
116 * @param tagName optional XML tagname supplied primarily during recursive calls
\r
117 * @return String containing the XML translation
\r
119 public static String toXMLString(Object obj, String tagName) throws JSONException {
\r
120 StringBuffer strBuf = new StringBuffer();
\r
123 JSONObject jsonObj;
\r
125 Iterator<String> keys;
\r
129 if (obj instanceof JSONObject) {
\r
130 // msoLogger.debug("toXMLString(): is a JSONObject");
\r
131 // append "<tagName>" to the XML output
\r
132 if (tagName != null) {
\r
133 // msoLogger.debug("toXMLString(): adding opening tagName: " + tagName);
\r
134 strBuf.append("<");
\r
135 strBuf.append(tagName);
\r
136 strBuf.append(">");
\r
138 // iterate thru the keys.
\r
139 jsonObj = (JSONObject) obj;
\r
140 keys = jsonObj.keys();
\r
141 while (keys.hasNext()) {
\r
142 key = keys.next().toString();
\r
143 // msoLogger.debug("toXMLString(): key is " + k);
\r
144 curObj = jsonObj.opt(key);
\r
145 if (curObj == null) {
\r
148 if (curObj instanceof String) {
\r
149 str = (String) curObj;
\r
153 // append the content to the XML output
\r
154 if (key.equals("content")) {
\r
155 if (curObj instanceof JSONArray) {
\r
156 jsonArr = (JSONArray) curObj;
\r
157 len = jsonArr.length();
\r
158 for (i = 0; i < len; i += 1) {
\r
160 strBuf.append('\n');
\r
162 strBuf.append(XML.escape(jsonArr.get(i).toString()));
\r
165 strBuf.append(XML.escape(curObj.toString()));
\r
167 // append an array of similar keys to the XML output
\r
168 } else if (curObj instanceof JSONArray) {
\r
169 jsonArr = (JSONArray) curObj;
\r
170 len = jsonArr.length();
\r
171 // msoLogger.debug("toXMLString(): found JSONArray: " + key + ", size: " + len);
\r
172 for (i = 0; i < len; i += 1) {
\r
173 curObj = jsonArr.get(i);
\r
174 if (curObj instanceof JSONArray) {
\r
175 // The XML tags for the nested array should be generated below when this method
\r
176 // is called recursively and the JSONArray object is passed
\r
177 // strBuf.append("<");
\r
178 // strBuf.append(key);
\r
179 // strBuf.append(">");
\r
180 strBuf.append(toXMLString(curObj, null));
\r
181 // strBuf.append("</");
\r
182 // strBuf.append(key);
\r
183 // strBuf.append(">");
\r
185 // msoLogger.debug("toXMLString(): recursive call toXML() with tagName null");
\r
186 // append the opening tag for the array (before 1st element)
\r
188 strBuf.append("<");
\r
189 strBuf.append(key);
\r
190 strBuf.append(">");
\r
192 // append the opening tag for the array
\r
193 strBuf.append(toXMLString(curObj, null));
\r
194 // append the closing tag for the array (after last element)
\r
195 if (i == (len - 1)) {
\r
196 strBuf.append("</");
\r
197 strBuf.append(key);
\r
198 strBuf.append(">");
\r
202 } else if (curObj.equals("")) {
\r
203 // append a closing tag "<key>" to the XML output
\r
204 strBuf.append("<");
\r
205 strBuf.append(key);
\r
206 strBuf.append("/>");
\r
208 // msoLogger.debug("toXMLString(): recursive call toXMLString() with tagName: " + key);
\r
209 strBuf.append(toXMLString(curObj, key));
\r
211 // msoLogger.debug("toXML(): partial XML: " + strBuf.toString());
\r
213 if (tagName != null) {
\r
214 // append the closing tag "</tagName>" to the XML output
\r
215 // msoLogger.debug("toXMLString(): adding closing tagName: " + tagName);
\r
216 strBuf.append("</");
\r
217 strBuf.append(tagName);
\r
218 strBuf.append(">");
\r
220 return strBuf.toString();
\r
221 // XML does not have good support for arrays. If an array appears in a place
\r
222 // where XML is lacking, synthesize an < array > element.
\r
223 } else if (obj instanceof JSONArray) {
\r
224 jsonArr = (JSONArray) obj;
\r
225 len = jsonArr.length();
\r
226 for (i = 0; i < len; ++i) {
\r
227 curObj = jsonArr.opt(i);
\r
228 strBuf.append(toXMLString(curObj, (tagName == null) ? "array"
\r
231 return strBuf.toString();
\r
233 // msoLogger.debug("toXML(): in else block with tagName: " + tagName);
\r
234 str = (obj == null) ? "null" : XML.escape(obj.toString());
\r
235 return (tagName == null) ? "\"" + str + "\""
\r
236 : (str.length() == 0) ? "<" + tagName + "/>" : "<"
\r
237 + tagName + ">" + str + "</" + tagName + ">";
\r
242 * Formats the JSON String using the value of MSOJsonIndentFactor.
\r
244 * @param jsonStr String containing the JSON doc
\r
245 * @return String containing the formatted JSON doc
\r
247 public static String prettyJson(String jsonStr) {
\r
248 // String isDebugLogEnabled = "true";
\r
250 JSONObject jsonObj = new JSONObject(jsonStr);
\r
251 return jsonObj.toString(MSOJsonIndentFactor);
\r
252 } catch (Exception e){
\r
253 msoLogger.debug("prettyJson(): unable to parse/format json input. Exception was: " + e.toString());
\r
259 * Invokes json2xml(String, Boolean) defaulting to 'pretty' output.
\r
261 * @param jsonStr String containing the XML doc
\r
262 * @return String containing the JSON translation
\r
264 public static String json2xml(String jsonStr) {
\r
265 return json2xml(jsonStr, true);
\r
269 * Returns an Iterator over the JSON keys in the specified JSON doc.
\r
271 * @param jsonStr String containing the JSON doc
\r
272 * @return Iterator over the JSON keys
\r
273 * @throws JSONException if the doc cannot be parsed
\r
275 public static Iterator <String> getJsonIterator(String jsonStr) throws JSONException {
\r
276 return new JSONObject(jsonStr).keys();
\r
280 * Returns the name of the "root" property in the specified JSON doc. The
\r
281 * "root" property is the single top-level property in the JSON doc. An
\r
282 * exception is thrown if the doc is empty or if it contains more than one
\r
283 * top-level property.
\r
285 * @param jsonStr String containing the JSON doc
\r
286 * @return the name of the "root" property
\r
287 * @throws JSONException if the doc cannot be parsed, or if it is empty, or if
\r
288 * it contains more than one top-level property
\r
290 public static String getJsonRootProperty(String jsonStr) throws JSONException {
\r
291 Iterator<String> iter = getJsonIterator(jsonStr);
\r
293 if (!iter.hasNext()) {
\r
294 throw new JSONException("Empty JSON object");
\r
297 String rootPropertyName = iter.next();
\r
299 if (iter.hasNext()) {
\r
300 throw new JSONException("JSON object has more than one root property");
\r
303 return rootPropertyName;
\r
307 * Invokes the getJsonRawValue() method and returns the String equivalent of
\r
308 * the object returned.
\r
310 * TBD: May need separate methods for boolean, float, and integer fields if the
\r
311 * String representation is not sufficient to meet client needs.
\r
313 * @param jsonStr String containing the JSON doc
\r
314 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
315 * @return String field value associated with keys
\r
317 public static String getJsonValue(String jsonStr, String keys) {
\r
318 // String isDebugLogEnabled = "true";
\r
320 Object rawValue = getJsonRawValue(jsonStr, keys);
\r
321 if (rawValue == null) {
\r
324 if (rawValue instanceof String) {
\r
325 msoLogger.debug("getJsonValue(): the raw value is a String Object=" + ((String) rawValue).toString());
\r
326 return (String) rawValue;
\r
328 msoLogger.debug("getJsonValue(): the raw value is NOT a String Object=" + rawValue.toString());
\r
329 return rawValue.toString();
\r
332 } catch (Exception e) {
\r
333 msoLogger.debug("getJsonValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString());
\r
340 * Invokes the getJsonRawValue() method with the wrap flag set to true
\r
341 * and returns the String equivalent of the json node object returned.
\r
343 * @param jsonStr String containing the JSON doc
\r
344 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
345 * @return String field value associated with keys
\r
347 public static String getJsonNodeValue(String jsonStr, String keys) {
\r
348 // String isDebugLogEnabled = "true";
\r
350 Object rawValue = getJsonRawValue(jsonStr, keys, true);
\r
351 if (rawValue == null) {
\r
354 if (rawValue instanceof String) {
\r
355 msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + ((String) rawValue).toString());
\r
356 return (String) rawValue;
\r
358 msoLogger.debug("getJsonNodeValue(): the raw value is NOT a String Object=" + rawValue.toString());
\r
359 return rawValue.toString();
\r
362 } catch (Exception e) {
\r
363 msoLogger.debug("getJsonNodeValue(): unable to parse json to retrieve node for field=" + keys + ". Exception was: " + e.toString());
\r
369 * Invokes the getJsonRawValue() method and returns the String equivalent of
\r
370 * the object returned.
\r
372 * TBD: May need separate methods for boolean, float, and integer fields if the
\r
373 * String representation is not sufficient to meet client needs.
\r
375 * @param jsonStr String containing the JSON doc
\r
376 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
377 * @return String field value associated with keys
\r
379 public static int getJsonIntValue(String jsonStr, String keys) {
\r
380 // String isDebugLogEnabled = "true";
\r
382 Object rawValue = getJsonRawValue(jsonStr, keys);
\r
383 if (rawValue == null) {
\r
386 if (rawValue instanceof Integer) {
\r
387 msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + ((String) rawValue).toString());
\r
388 return (Integer) rawValue;
\r
390 msoLogger.debug("getJsonValue(): the raw value is NOT an Integer Object=" + rawValue.toString());
\r
394 } catch (Exception e) {
\r
395 msoLogger.debug("getJsonValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString());
\r
401 * Invokes the getJsonParamValue() method to obtain the JSONArray associated with
\r
402 * the specified keys. The JSONArray is then walked to retrieve the first array
\r
403 * value associated with the specified field name (index=0).
\r
405 * @param jsonStr String containing the JSON doc
\r
406 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
407 * @param name field name for the param to be retrieved
\r
408 * @return String param value associated with field name
\r
410 public static String getJsonParamValue(String jsonStr, String keys, String name) {
\r
411 return getJsonParamValue(jsonStr, keys, name, 0);
\r
415 * Invokes the getJsonRawValue() method to obtain the JSONArray associated with
\r
416 * the specified keys. The JSONArray is then walked to retrieve the nth array
\r
417 * value associated with the specified field name and index
\r
419 * @param jsonStr String containing the JSON doc
\r
420 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
421 * @param name field name for the param to be retrieved
\r
422 * @param index the nth param associated with name starting at 0
\r
423 * @return String param value associated with field name
\r
425 public static String getJsonParamValue(String jsonStr, String keys, String name, int index) {
\r
426 // String isDebugLogEnabled = "true";
\r
428 Object rawValue = getJsonRawValue(jsonStr, keys);
\r
429 if (rawValue == null) {
\r
432 if (rawValue instanceof JSONArray) {
\r
433 msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + ((JSONArray) rawValue).toString());
\r
434 int arrayLen = ((JSONArray) rawValue).length();
\r
435 if (index < 0 || arrayLen < index+1) {
\r
436 msoLogger.debug("getJsonParamValue(): index: " + index + " is out of bounds for array size of " + arrayLen);
\r
440 for (int i = 0; i < arrayLen; i++) {
\r
441 msoLogger.debug("getJsonParamValue(): index: " + i + ", value: " + ((JSONArray) rawValue).get(i).toString());
\r
442 if (((JSONArray) rawValue).get(i) instanceof JSONObject) {
\r
443 msoLogger.debug("getJsonParamValue(): index: " + i + " is a JSONObject");
\r
444 JSONObject jsonObj = (JSONObject)((JSONArray) rawValue).get(i);
\r
445 String parmValue = jsonObj.get(name).toString();
\r
446 if (parmValue != null) {
\r
447 msoLogger.debug("getJsonParamValue(): found value: " + parmValue + " for name: " + name + " and index: " + i);
\r
448 if (foundCnt == index) {
\r
458 msoLogger.debug("getJsonParamValue(): the JSONArray element is NOT a JSONObject=" + rawValue.toString());
\r
462 msoLogger.debug("getJsonParamValue(): content value NOT found for name: " + name);
\r
465 msoLogger.debug("getJsonParamValue(): the raw value is NOT a JSONArray Object=" + rawValue.toString());
\r
469 } catch (JSONException je) {
\r
470 // JSONObject::get() throws this exception if one of the specified keys is not found
\r
471 msoLogger.debug("getJsonParamValue(): caught JSONException attempting to retrieve param value for keys:" + keys + ", name=" + name);
\r
472 } catch (Exception e) {
\r
473 msoLogger.debug("getJsonParamValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString());
\r
479 * Wrapper to generate the JSONObject to pass to the getJsonValueForKey(JSONObject, String)
\r
480 * method so that recursion over the subobjects can be supported there
\r
482 * @param jsonStr String containing the JSON doc
\r
483 * @param key key to the target value
\r
484 * @return String field value associated with key
\r
486 public static String getJsonValueForKey(String jsonStr, String key) {
\r
487 // String isDebugLogEnabled = "true";
\r
489 JSONObject jsonObj = new JSONObject(jsonStr);
\r
490 if (jsonObj != null) {
\r
491 return getJsonValueForKey(jsonObj, key);
\r
493 } catch (Exception e) {
\r
494 msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString());
\r
500 * Walks the JSONObject (and sub-objects recursively), searching for the first value associated with the
\r
501 * single key/field name specified. Returns the associated value if found or null if the key is not found
\r
503 * @param jsonObj JSONObject representation of the the JSON doc
\r
504 * @param key key to the target value
\r
505 * @return String field value associated with key
\r
507 public static String getJsonValueForKey(JSONObject jsonObj, String key) {
\r
508 // String isDebugLogEnabled = "true";
\r
509 String keyValue = null;
\r
511 if (jsonObj.has(key)) {
\r
512 msoLogger.debug("getJsonValueForKey(): found value for key=" + key);
\r
513 return ((String) jsonObj.get(key));
\r
515 msoLogger.debug("getJsonValueForKey(): iterating over the keys");
\r
516 Iterator <String> itr = jsonObj.keys();
\r
517 while (itr.hasNext()) {
\r
518 String nextKey = (String) itr.next();
\r
519 Object obj = jsonObj.get(nextKey);
\r
520 if (obj instanceof JSONObject) {
\r
521 msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");
\r
522 keyValue = getJsonValueForKey((JSONObject) obj, key);
\r
523 if (keyValue != null) {
\r
524 msoLogger.debug("getJsonValueForKey(): found value=" + keyValue + ", for key=" + key);
\r
528 msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", does not point to a JSONObject, next key");
\r
532 } catch (JSONException je) {
\r
533 // JSONObject::get() throws this exception if one of the specified keys is not found
\r
534 msoLogger.debug("getJsonValueForKey(): caught JSONException attempting to retrieve value for key=" + key);
\r
536 } catch (Exception e) {
\r
537 msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString());
\r
543 * Walks the JSONObject (and sub-objects recursively), searching for the first value associated with the
\r
544 * single key/field name specified. Returns the associated value if found or null if the key is not found
\r
546 * @param jsonObj JSONObject representation of the the JSON doc
\r
547 * @param key key to the target value
\r
548 * @return String field value associated with key
\r
550 public static Integer getJsonIntValueForKey(JSONObject jsonObj, String key) {
\r
551 // String isDebugLogEnabled = "true";
\r
552 Integer keyValue = 0;
\r
554 if (jsonObj.has(key)) {
\r
555 msoLogger.debug("getJsonValueForKey(): found value for key=" + key);
\r
556 return ((Integer) jsonObj.get(key));
\r
558 msoLogger.debug("getJsonValueForKey(): iterating over the keys");
\r
559 Iterator <String> itr = jsonObj.keys();
\r
560 while (itr.hasNext()) {
\r
561 String nextKey = (String) itr.next();
\r
562 Object obj = jsonObj.get(nextKey);
\r
563 if (obj instanceof JSONObject) {
\r
564 msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");
\r
565 keyValue = getJsonIntValueForKey((JSONObject) obj, key);
\r
566 if (keyValue != null) {
\r
567 msoLogger.debug("getJsonValueForKey(): found value=" + keyValue + ", for key=" + key);
\r
571 msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", does not point to a JSONObject, next key");
\r
575 } catch (JSONException je) {
\r
576 // JSONObject::get() throws this exception if one of the specified keys is not found
\r
577 msoLogger.debug("getJsonValueForKey(): caught JSONException attempting to retrieve value for key=" + key);
\r
579 } catch (Exception e) {
\r
580 msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString());
\r
586 * Boolean method to determine if a key path is valid for the JSON doc. Invokes
\r
589 * @param jsonStr String containing the JSON doc
\r
590 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
591 * @return Boolean true if keys points to value in the JSON doc
\r
593 public static Boolean jsonValueExists(String jsonStr, String keys) {
\r
594 if (getJsonRawValue(jsonStr, keys) == null) {
\r
602 * Inserts the new key/value pair at the appropriate location in the JSON
\r
603 * document after first determining if keyed field already exists. If
\r
604 * it does exist, return the JSON unmodified, otherwise return the new JSON
\r
605 * Note: this method currently only supports String value inserts.
\r
607 * @param jsonStr String containing the JSON doc
\r
608 * @param keys full key path to the value to be added in the format of "key1.key2.key3..."
\r
609 * @return String containing the updated JSON doc
\r
611 public static String addJsonValue(String jsonStr, String keys, String value) {
\r
612 // String isDebugLogEnabled = "true";
\r
613 // only attempt to insert the key/value pair if it does not exist
\r
614 if (!jsonValueExists(jsonStr, keys)) {
\r
615 return putJsonValue(jsonStr, keys, value);
\r
617 msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + (String) value + " already exists");
\r
623 * Updates the value for the specified key in the JSON document
\r
624 * after first determining if keyed field exists. If it does
\r
625 * not exist, return the JSON unmodified, otherwise return the updated JSON.
\r
626 * Note: this method currently only supports String value updates.
\r
628 * @param jsonStr String containing the JSON doc
\r
629 * @param keys full key path to the value to be updated in the format of "key1.key2.key3..."
\r
630 * @return String containing the updated JSON doc
\r
632 public static String updJsonValue(String jsonStr, String keys, String newValue) {
\r
633 // String isDebugLogEnabled = "true";
\r
634 // only attempt to modify the key/value pair if it exists
\r
635 if (jsonValueExists(jsonStr, keys)) {
\r
636 return putJsonValue(jsonStr, keys, newValue);
\r
638 msoLogger.debug("updJsonValue(): JSON update failed, no value exists for key=" + keys);
\r
644 * Deletes the value for the specified key in the JSON document
\r
645 * after first determining if keyed field exists. If it does
\r
646 * not exist, return the JSON unmodified, otherwise return the updated JSON
\r
648 * @param jsonStr String containing the JSON doc
\r
649 * @param keys full key path to the value to be deleted in the format of "key1.key2.key3..."
\r
650 * @return String containing the updated JSON doc
\r
652 public static String delJsonValue(String jsonStr, String keys) {
\r
653 // String isDebugLogEnabled = "true";
\r
654 // only attempt to remove the key/value pair if it exists
\r
655 if (jsonValueExists(jsonStr, keys)) {
\r
656 // passing a null value results in a delete
\r
657 return putJsonValue(jsonStr, keys, null);
\r
659 msoLogger.debug("delJsonValue(): JSON delete failed, no value exists for key=" + keys);
\r
665 * Walks the JSON doc using the full key path to retrieve the associated
\r
666 * value. All but the last key points to the 'parent' object name(s) in order
\r
667 * in the JSON hierarchy with the last key pointing to the target value.
\r
668 * The value returned is a Java object.
\r
670 * @param jsonStr String containing the JSON doc
\r
671 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
672 * @return Object field value associated with keys
\r
674 private static Object getJsonRawValue(String jsonStr, String keys) {
\r
675 return getJsonRawValue(jsonStr, keys, false);
\r
679 * Walks the JSON doc using the full key path to retrieve the associated
\r
680 * value. All but the last key points to the 'parent' object name(s) in order
\r
681 * in the JSON hierarchy with the last key pointing to the target value.
\r
682 * The value returned is a Java object.
\r
684 * @param jsonStr String containing the JSON doc
\r
685 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
686 * * @param wrap Boolean which determines if returned JSONObjects sould be "wrapped"
\r
687 * Note: wrap does not apply to returned scalar values
\r
688 * @return Object field value associated with keys
\r
690 private static Object getJsonRawValue(String jsonStr, String keys, Boolean wrap) {
\r
691 // String isDebugLogEnabled = "true";
\r
692 String keyStr = "";
\r
694 JSONObject jsonObj = new JSONObject(jsonStr);
\r
695 StringTokenizer keyTokens = new StringTokenizer(keys, ".");
\r
696 while (keyTokens.hasMoreElements()) {
\r
697 keyStr = keyTokens.nextToken();
\r
698 Object keyValue = jsonObj.get(keyStr);
\r
699 if (keyValue instanceof JSONObject) {
\r
700 msoLogger.debug("getJsonRawValue(): key=" + keyStr + " points to json object");
\r
701 jsonObj = (JSONObject) keyValue;
\r
703 if (keyTokens.hasMoreElements()) {
\r
704 msoLogger.debug("getJsonRawValue(): value found prior to last key for key=" + keyStr);
\r
709 // return the json 'node' that the key points to
\r
710 // note: since this is a json object and not a scalar value,
\r
711 // use the wrap flag to determine if the object should
\r
712 // be wrapped with a root node value
\r
713 // (the last key in the keys String)
\r
715 JSONObject wrappedJsonObj = new JSONObject();
\r
716 wrappedJsonObj.put(keyStr, jsonObj);
\r
717 return wrappedJsonObj.toString();
\r
719 return jsonObj.toString();
\r
722 } catch (JSONException je) {
\r
723 // JSONObject::get() throws this exception if one of the specified keys is not found
\r
724 msoLogger.debug("getJsonRawValue(): caught JSONException attempting to retrieve raw value for key=" + keyStr);
\r
725 } catch (Exception e) {
\r
726 msoLogger.debug("getJsonRawValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString());
\r
732 * Private method invoked by the public add, update, and delete methods.
\r
734 * @param jsonStr String containing the JSON doc
\r
735 * @param keys full key path to the value to be deleted in the format of "key1.key2.key3..."
\r
736 * @return String containing the updated JSON doc
\r
738 private static String putJsonValue(String jsonStr, String keys, String value) {
\r
739 // String isDebugLogEnabled = "true";
\r
740 String keyStr = "";
\r
742 JSONObject jsonObj = new JSONObject(jsonStr);
\r
743 JSONObject jsonObjOut = jsonObj;
\r
744 StringTokenizer keyTokens = new StringTokenizer(keys, ".");
\r
745 while (keyTokens.hasMoreElements()) {
\r
746 keyStr = keyTokens.nextToken();
\r
747 if (keyTokens.hasMoreElements()) {
\r
748 Object keyValue = jsonObj.get(keyStr);
\r
749 if (keyValue instanceof JSONObject) {
\r
750 msoLogger.debug("putJsonValue(): key=" + keyStr + " points to json object");
\r
751 jsonObj = (JSONObject) keyValue;
\r
753 msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + (String) keyValue);
\r
756 } else { // at the last/new key value
\r
757 jsonObj.put(keyStr, value);
\r
758 return jsonObjOut.toString(3);
\r
761 // should not hit this point if the key points to a valid key value
\r
764 } catch (JSONException je) {
\r
765 // JSONObject::get() throws this exception if one of the specified keys is not found
\r
766 msoLogger.debug("putJsonValue(): caught JSONException attempting to retrieve value for key=" + keyStr);
\r
768 } catch (Exception e) {
\r
769 msoLogger.debug("putJsonValue(): unable to parse json to put value for key=" + keys + ". Exception was: " + e.toString());
\r
775 * This json util method converts a json "Key" and "Value"
\r
776 * entry Array to a Java map.
\r
779 * @param entryArray - the json value of the entry Array
\r
781 * @return map - a Map containing the entries
\r
784 public Map<String, String> entryArrayToMap(Execution execution, String entryArray) {
\r
785 msoLogger.debug("Started Entry Array To Map Util Method");
\r
787 Map<String, String> map = new HashMap<String, String>();
\r
790 String entryListJson = "{ \"entry\":" + entryArray + "}";
\r
791 JSONObject obj = new JSONObject(entryListJson);
\r
792 JSONArray arr = obj.getJSONArray("entry");
\r
793 for (int i = 0; i < arr.length(); i++){
\r
794 JSONObject jo = arr.getJSONObject(i);
\r
795 String key = jo.getString("key");
\r
796 String value =jo.getString("value");
\r
797 map.put(key, value);
\r
799 msoLogger.debug("Outgoing Map is: " + map);
\r
800 msoLogger.debug("Completed Entry Array To Map Util Method");
\r
806 * Invokes the getJsonRawValue() method to determine if the
\r
807 * json element/variable exist. Returns true if the
\r
808 * json element exist
\r
810 * @param jsonStr String containing the JSON doc
\r
811 * @param keys full key path to the target value in the format of "key1.key2.key3..."
\r
812 * @return boolean field value associated with keys
\r
814 public static boolean jsonElementExist(String jsonStr, String keys) {
\r
817 Object rawValue = getJsonRawValue(jsonStr, keys);
\r
818 if (rawValue == null) {
\r
823 } catch (Exception e) {
\r
824 msoLogger.debug("jsonElementExist(): unable to determine if json element exist. Exception is: " + e.toString());
\r