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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
34 import org.codehaus.jettison.json.JSONArray;
35 import org.codehaus.jettison.json.JSONException;
36 import org.codehaus.jettison.json.JSONObject;
39 public class JSONTool {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
46 public static Map<String, String> convertToProperties(String s) throws JSONException {
47 return convertToProperties(s, null);
50 public static Map<String, String> convertToProperties(String s, List<String> blockKeys)
51 throws JSONException {
52 JSONObject json = new JSONObject(s);
53 Map<String, String> mm = new HashMap<>();
54 Map<String, Object> wm = new HashMap<>();
56 Iterator<String> ii = json.keys();
57 while (ii.hasNext()) {
58 String key1 = ii.next();
59 wm.put(key1, json.get(key1));
61 while (!wm.isEmpty()) {
62 for (String key : new ArrayList<>(wm.keySet())) {
63 Object o = wm.get(key);
65 tryAddBlockKeys(blockKeys, mm, key, o);
66 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
67 mm.put(key, o.toString());
68 log.info("Added property: " + key + ": " + o.toString());
69 } else if (o instanceof JSONObject) {
70 fill(wm, key, (JSONObject) o);
71 } else if (o instanceof JSONArray) {
72 fill(mm, wm, key, (JSONArray) o);
79 private static void tryAddBlockKeys(List<String> blockKeys, Map<String, String> mm, String key, Object o) {
80 if (blockKeys != null && blockKeys.contains(key) && o != null) {
81 mm.put("block_" + key, o.toString());
82 log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
86 private static void fill(Map<String, String> mm, Map<String, Object> wm, String key, JSONArray array)
87 throws JSONException {
88 mm.put("size_" + key, String.valueOf(array.length()));
89 log.info("Added property: " + key + "_length" + ": " + array.length());
91 for (int i = 0; i < array.length(); i++) {
92 wm.put(key + '[' + i + ']', array.get(i));
96 private static void fill(Map<String, Object> wm, String key, JSONObject object) throws JSONException {
98 Iterator<String> i = object.keys();
100 String key1 = i.next();
101 wm.put(key + "." + key1, object.get(key1));