2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.config.generator.tool;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
29 import org.codehaus.jettison.json.JSONArray;
30 import org.codehaus.jettison.json.JSONException;
31 import org.codehaus.jettison.json.JSONObject;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
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) throws JSONException {
49 JSONObject json = new JSONObject(s);
50 Map<String, String> mm = new HashMap<String, String>();
52 Map<String, Object> wm = new HashMap<String, Object>();
53 Iterator<String> ii = json.keys();
54 while (ii.hasNext()) {
55 String key1 = ii.next();
56 wm.put(key1, json.get(key1));
62 for (String key : new ArrayList<>(wm.keySet())) {
63 Object o = wm.get(key);
67 if(blockKeys != null && blockKeys.contains(key) && o != null){
68 //log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
69 mm.put("block_" +key,o.toString());
72 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
73 mm.put(key, o.toString());
74 //log.info("Added property: " + key + ": " + o.toString());
77 else if (o instanceof JSONObject) {
78 JSONObject jo = (JSONObject) o;
79 Iterator<String> i = jo.keys();
81 String key1 = i.next();
82 wm.put(key + "." + key1, jo.get(key1));
86 else if (o instanceof JSONArray) {
87 JSONArray ja = (JSONArray) o;
88 mm.put("size_"+key, String.valueOf(ja.length()));
90 //log.info("Added property: " + key + "_length" + ": " + String.valueOf(ja.length()));
92 for (int i = 0; i < ja.length(); i++)
93 wm.put(key + '[' + i + ']', ja.get(i));
101 public static Map<String, String> convertToProperties1(String s,List<String> blockKeys) throws Exception {
102 ObjectMapper objectMapper = new ObjectMapper();
104 JsonNode rootNode = objectMapper.readTree(s);
106 Map<String, String> mm = new HashMap<String, String>();
108 Map<String, Object> wm = new HashMap<String, Object>();
109 Iterator<String> ii = rootNode.fieldNames();
110 while (ii.hasNext()) {
111 String key1 = ii.next();
112 wm.put(key1, rootNode.get(key1));
117 while (!wm.isEmpty())
118 for (String key : new ArrayList<>(wm.keySet())) {
119 Object o = wm.get(key);
123 if(blockKeys != null && blockKeys.contains(key) && o != null){
124 //log.info("Adding JSON Block Keys : " + key + "=" + o.toString());
125 mm.put("block_" +key,o.toString());
128 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
129 mm.put(key, o.toString());
130 //log.info("Added property: " + key + ": " + o.toString());
133 else if (o instanceof JSONObject) {
134 JSONObject jo = (JSONObject) o;
135 Iterator<String> i = jo.keys();
136 while (i.hasNext()) {
137 String key1 = i.next();
138 wm.put(key + "." + key1, jo.get(key1));
142 else if (o instanceof JSONArray) {
143 JSONArray ja = (JSONArray) o;
144 mm.put("size_"+key, String.valueOf(ja.length()));
146 //log.info("Added property: " + key + "_length" + ": " + String.valueOf(ja.length()));
148 for (int i = 0; i < ja.length(); i++)
149 wm.put(key + '[' + i + ']', ja.get(i));