2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.generator.tool;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
32 import org.codehaus.jettison.json.JSONArray;
33 import org.codehaus.jettison.json.JSONException;
34 import org.codehaus.jettison.json.JSONObject;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
40 public class JSONTool {
42 private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
44 public static Map<String, String> convertToProperties(String s) throws JSONException {
45 return convertToProperties(s, null);
48 public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
49 throws JSONException {
50 JSONObject json = new JSONObject(s);
51 Map<String, String> mm = new HashMap<String, String>();
53 Map<String, Object> wm = new HashMap<String, Object>();
54 Iterator<String> ii = json.keys();
55 while (ii.hasNext()) {
56 String key1 = ii.next();
57 wm.put(key1, json.get(key1));
63 for (String key : new ArrayList<>(wm.keySet())) {
64 Object o = wm.get(key);
68 if (blockKeys != null && blockKeys.contains(key) && o != null) {
69 // log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
70 mm.put("block_" + key, o.toString());
73 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
74 mm.put(key, o.toString());
75 // log.info("Added property: " + key + ": " + o.toString());
78 else if (o instanceof JSONObject) {
79 JSONObject jo = (JSONObject) o;
80 Iterator<String> i = jo.keys();
82 String key1 = i.next();
83 wm.put(key + "." + key1, jo.get(key1));
87 else if (o instanceof JSONArray) {
88 JSONArray ja = (JSONArray) o;
89 mm.put("size_" + key, String.valueOf(ja.length()));
91 // log.info("Added property: " + key + "_length" + ": " +
92 // String.valueOf(ja.length()));
94 for (int i = 0; i < ja.length(); i++)
95 wm.put(key + '[' + i + ']', ja.get(i));