2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.generator.tool;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
33 import org.codehaus.jettison.json.JSONArray;
34 import org.codehaus.jettison.json.JSONException;
35 import org.codehaus.jettison.json.JSONObject;
38 public class JSONTool {
40 private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
45 public static Map<String, String> convertToProperties(String s) throws JSONException {
46 return convertToProperties(s, null);
49 public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
50 throws JSONException {
51 JSONObject json = new JSONObject(s);
52 Map<String, String> mm = new HashMap<>();
53 Map<String, Object> wm = new HashMap<>();
55 Iterator<String> ii = json.keys();
56 while (ii.hasNext()) {
57 String key1 = ii.next();
58 wm.put(key1, json.get(key1));
60 while (!wm.isEmpty()) {
61 for (String key : new ArrayList<>(wm.keySet())) {
62 Object o = wm.get(key);
64 tryAddBlockKeys(blockKeys, mm, key, o);
65 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
66 String oString = o.toString();
67 //Add escape characters to the string in case it is a string representation
69 oString = JSONObject.quote(oString);
70 //Remove the surrouding quotes added by the JSONObject.quote() method.
71 //JSONObject.quote() will always return, at minimum, a string with two quotes,
72 //even if a null string is passed to it. So this substring method does not
74 oString = oString.substring(1, oString.length() - 1);
76 log.info("Added property: " + key + ": " + oString);
77 } else if (o instanceof JSONObject) {
78 fill(wm, key, (JSONObject) o);
79 } else if (o instanceof JSONArray) {
80 fill(mm, wm, key, (JSONArray) o);
87 private static void tryAddBlockKeys(List<String> blockKeys, Map<String, String> mm, String key, Object o) {
88 if (blockKeys != null && blockKeys.contains(key) && o != null) {
89 mm.put("block_" + key, o.toString());
90 log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
94 private static void fill(Map<String, String> mm, Map<String, Object> wm, String key, JSONArray array)
95 throws JSONException {
96 mm.put("size_" + key, String.valueOf(array.length()));
97 log.info("Added property: " + key + "_length" + ": " + array.length());
99 for (int i = 0; i < array.length(); i++) {
100 wm.put(key + '[' + i + ']', array.get(i));
104 private static void fill(Map<String, Object> wm, String key, JSONObject object) throws JSONException {
106 Iterator<String> i = object.keys();
107 while (i.hasNext()) {
108 String key1 = i.next();
109 wm.put(key + "." + key1, object.get(key1));