f2867f5a3093bde14243f07f5b7901aee6e37ccc
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / restapicall / JsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.restapicall;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import org.codehaus.jettison.json.JSONArray;
30 import org.codehaus.jettison.json.JSONException;
31 import org.codehaus.jettison.json.JSONObject;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class JsonParser {
36
37     private static final Logger log = LoggerFactory.getLogger(JsonParser.class);
38
39     @SuppressWarnings("unchecked")
40     public static Map<String, String> convertToProperties(String s) throws JSONException {
41         JSONObject json = new JSONObject(s);
42
43         Map<String, Object> wm = new HashMap<>();
44         Iterator<String> ii = json.keys();
45         while (ii.hasNext()) {
46             String key1 = ii.next();
47             wm.put(key1, json.get(key1));
48         }
49
50         Map<String, String> mm = new HashMap<>();
51
52         while (!wm.isEmpty())
53             for (String key : new ArrayList<>(wm.keySet())) {
54                 Object o = wm.get(key);
55                 wm.remove(key);
56
57                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
58                     mm.put(key, o.toString());
59
60                     log.info("Added property: " + key + ": " + o.toString());
61                 }
62
63                 else if (o instanceof JSONObject) {
64                     JSONObject jo = (JSONObject) o;
65                     Iterator<String> i = jo.keys();
66                     while (i.hasNext()) {
67                         String key1 = i.next();
68                         wm.put(key + "." + key1, jo.get(key1));
69                     }
70                 }
71
72                 else if (o instanceof JSONArray) {
73                     JSONArray ja = (JSONArray) o;
74                     mm.put(key + "_length", String.valueOf(ja.length()));
75
76                     log.info("Added property: " + key + "_length" + ": " + String.valueOf(ja.length()));
77
78                     for (int i = 0; i < ja.length(); i++)
79                         wm.put(key + '[' + i + ']', ja.get(i));
80                 }
81             }
82
83         return mm;
84     }
85 }