2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Samsung Electronics. All rights
9 * ================================================================================
10 * Modifications Copyright © 2018 IBM
11 * ================================================================================
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.ccsdk.sli.plugins.sshapicall.model;
28 import static com.google.common.base.Preconditions.checkNotNull;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
35 import org.codehaus.jettison.json.JSONArray;
36 import org.codehaus.jettison.json.JSONException;
37 import org.codehaus.jettison.json.JSONObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public final class JsonParser {
43 private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
45 private JsonParser() {
46 // Preventing instantiation of the same.
49 @SuppressWarnings("unchecked")
50 public static Map<String, String> convertToProperties(String s)
51 throws JSONException {
53 checkNotNull(s, "Input should not be null.");
55 JSONObject json = new JSONObject(s);
56 Map<String, Object> wm = new HashMap<>();
57 Iterator<String> ii = json.keys();
58 while (ii.hasNext()) {
59 String key1 = ii.next();
60 wm.put(key1, json.get(key1));
63 Map<String, String> mm = new HashMap<>();
66 for (String key : new ArrayList<>(wm.keySet())) {
67 Object o = wm.get(key);
70 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
71 mm.put(key, o.toString());
73 log.info("Added property: {} : {}", key, o.toString());
74 } else if (o instanceof JSONObject) {
75 JSONObject jo = (JSONObject) o;
76 Iterator<String> i = jo.keys();
78 String key1 = i.next();
79 wm.put(key + "." + key1, jo.get(key1));
81 } else if (o instanceof JSONArray) {
82 JSONArray ja = (JSONArray) o;
83 mm.put(key + "_length", String.valueOf(ja.length()));
85 log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
87 for (int i = 0; i < ja.length(); i++)
88 wm.put(key + '[' + i + ']', ja.get(i));