summaryrefslogtreecommitdiffstats
path: root/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtil.java
blob: 73442158e26cc01a95ad16e7b4f4f98d09c5e50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright 2016 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.multivimproxy.common.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.onap.vfc.nfvo.multivimproxy.common.constant.ParamConstant;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 *
 * Json Utility Class.<br/>
 * <p>
 * </p>
 *
 * @author
 * @version     VFC 1.0  Sep 10, 2016
 */
public final class JsonUtil {

    private static final Logger LOG = LogManager.getLogger(StringUtil.class);

    private static final int TYPE_STRING = 0;

    private static final int TYPE_INT = 1;

    private static final int TYPE_JSONA = 2;

    private static final int TYPE_JSONO = 3;

    private static final int TYPE_LONG = 4;

    private JsonUtil() {
    }

    /**
     *
     * Get Json Field String.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @return
     * @since  VFC 1.0
     */
    public static String getJsonFieldStr(JSONObject jsonObj, String fieldName) {
        return (String)getJsonFieldObject(jsonObj, fieldName, TYPE_STRING);
    }

    /**
     *
     * Get Json Field Integer.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @return
     * @since  VFC 1.0
     */
    public static Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
        return (Integer)getJsonFieldObject(jsonObj, fieldName, TYPE_INT);
    }

    /**
     *
     * Get Json Field array.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @return
     * @since  VFC 1.0
     */
    public static JSONArray getJsonFieldArr(JSONObject jsonObj, String fieldName) {
        return (JSONArray)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONA);
    }

    /**
     *
     * Get Json Field Json.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @return
     * @since  VFC 1.0
     */
    public static JSONObject getJsonFieldJson(JSONObject jsonObj, String fieldName) {
        return (JSONObject)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONO);
    }

    /**
     *
     * Get Json Field Long.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @return
     * @since  VFC 1.0
     */
    public static Long getJsonFieldLong(JSONObject jsonObj, String fieldName) {
        return (Long)getJsonFieldObject(jsonObj, fieldName, TYPE_LONG);
    }

    /**
     *
     * Get Json Field Object.<br>
     *
     * @param jsonObj
     * @param fieldName
     * @param classType
     * @return
     * @since  VFC 1.0
     */
    private static Object getJsonFieldObject(JSONObject jsonObj, String fieldName, int classType) {
        try {
            if(null != jsonObj && jsonObj.has(fieldName)) {
                Object result;
                switch(classType) {
                    case TYPE_STRING:
                        result = "null".equals(jsonObj.getString(fieldName)) ? "" : jsonObj.getString(fieldName);
                        break;
                    case TYPE_INT:
                        result = jsonObj.getInt(fieldName);
                        break;
                    case TYPE_JSONA:
                        result = jsonObj.getJSONArray(fieldName);
                        break;
                    case TYPE_JSONO:
                        result = jsonObj.getJSONObject(fieldName);
                        break;
                    case TYPE_LONG:
                        result = jsonObj.getLong(fieldName);
                        break;
                    default:
                        result = null;
                        break;
                }
                return result;
            }
        } catch(JSONException e) {
            LOG.error("function=getJsonFieldLong, exception: {} ", e);
            return null;
        }
        return null;
    }

    /**
     *
     * Check whether the Json Object is empty.<br>
     *
     * @param jsonObject
     * @return
     * @since  VFC 1.0
     */
    public static boolean isNullJson(JSONObject jsonObject) {
        if(null == jsonObject || jsonObject.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     *
     * Get String value by Json.<br>
     *
     * @param json
     * @param key
     * @return
     * @since  VFC 1.0
     */
    public static String getStrValueByjson(JSONObject json, String key) {
        JSONArray names = json.names();
        String result = null;
        for(int i = 0; i < names.size(); i++) {
            String nodeName = names.getString(i);
            if(json.optJSONObject(nodeName) != null) {
                result = getStrValueByjson(json.getJSONObject(nodeName), key);
            }
            if(json.optJSONArray(nodeName) != null) {
                result = getStrValueByJArray(json.getJSONArray(nodeName), key);
            }
            if(nodeName.equals(key)) {
                result = json.getString(nodeName);
                break;
            }
        }
        return result;
    }

    private static String getStrValueByJArray(JSONArray json, String key) {
        String result = null;
        for(int i = 0; i < json.size(); i++) {
            if(json.optJSONObject(i) != null) {
                result = getStrValueByjson(json.getJSONObject(i), key);
            }
            if(json.optJSONArray(i) != null) {
                result = getStrValueByJArray(json.getJSONArray(i), key);
            }
        }
        return result;
    }

    /**
     *
     * Get Json Value by Json object.<br>
     *
     * @param json
     * @param key
     * @return
     * @since  VFC 1.0
     */
    public static JSONObject getJsonValueByjson(JSONObject json, String key) {
        JSONObject resultJson = new JSONObject();
        String result = getStrValueByjson(json, key);
        if(null == result) {
            return null;
        }
        resultJson.element(key, result);
        return resultJson;

    }

    /**
     *
     * Get String Value by Json object.<br>
     *
     * @param json
     * @param parentKey
     * @param key
     * @return
     * @since  VFC 1.0
     */
    public static String getStrValueByParentJson(JSONObject json, String parentKey, String key) {
        if(parentKey.isEmpty()) {
            return getStrValueByjson(json, key);
        }
        JSONObject parentJson = getJsonValueByjson(json, parentKey);
        if(isNullJson(parentJson)) {
            return null;
        }
        return getStrValueByjson(parentJson, key);

    }

    /**
     *
     * Get response Data.<br>
     *
     * @param obj
     * @return
     * @since  VFC 1.0
     */
    public static JSONObject getResponseData(JSONObject obj) {
        JSONObject result = new JSONObject();

        Integer retCode = getJsonFieldInt(obj, ParamConstant.PARAM_RETCODE);
        if(null == retCode || retCode == -1) {
            return null;
        }

        if(obj.optJSONObject(ParamConstant.PARAM_DATA) != null) {
            result = obj.getJSONObject(ParamConstant.PARAM_DATA);
        } else if(obj.optJSONArray(ParamConstant.PARAM_DATA) != null) {
            result = obj.getJSONArray(ParamConstant.PARAM_DATA).getJSONObject(0);
        }
        if(result.isEmpty()) {
            return null;
        }

        if(result.containsKey(ParamConstant.PARAM_RETCODE)) {
            result = getResponseData(result);
        }
        return result;
    }

}