2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.plugins.restapicall;
24 import static com.google.common.base.Preconditions.checkNotNull;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
31 import org.codehaus.jettison.json.JSONArray;
32 import org.codehaus.jettison.json.JSONException;
33 import org.codehaus.jettison.json.JSONObject;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class JsonParser {
40 private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
42 private JsonParser() {
43 // Preventing instantiation of the same.
46 @SuppressWarnings("unchecked")
47 public static Map<String, String> convertToProperties(String s)
48 throws SvcLogicException {
50 checkNotNull(s, "Input should not be null.");
53 JSONObject json = new JSONObject(s);
54 Map<String, Object> wm = new HashMap<>();
55 Iterator<String> ii = json.keys();
56 while (ii.hasNext()) {
57 String key1 = ii.next();
58 String[] subKey = key1.split(":");
59 if (subKey.length == 2) {
60 wm.put(subKey[1], json.get(key1));
62 wm.put(key1, json.get(key1));
66 Map<String, String> mm = new HashMap<>();
68 while (!wm.isEmpty()) {
69 for (String key : new ArrayList<>(wm.keySet())) {
70 Object o = wm.get(key);
73 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
74 mm.put(key, o.toString());
76 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 String[] subKey = key1.split(":");
83 if (subKey.length == 2) {
84 wm.put(key + "." + subKey[1], jo.get(key1));
86 wm.put(key + "." + key1, jo.get(key1));
89 } else if (o instanceof JSONArray) {
90 JSONArray ja = (JSONArray) o;
91 mm.put(key + "_length", String.valueOf(ja.length()));
93 log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
95 for (int i = 0; i < ja.length(); i++) {
96 wm.put(key + '[' + i + ']', ja.get(i));
102 } catch (JSONException e) {
103 throw new SvcLogicException("Unable to convert JSON to properties" + e.getLocalizedMessage(), e);