340f8690e3adafcb1313134138172a4a7b4ce075
[o-du/l2.git] / src / o1 / ves / JsonHelper.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains functions to support Json related operation (read/write)*/
20
21 #include <bits/stdc++.h>
22 #include "JsonHelper.hpp"
23 #include "VesUtils.hpp"
24
25 using namespace std;
26
27 /*******************************************************************
28  *
29  * @brief wraps cJSON_CreateObject cJSON library function
30  *
31  * @details
32  *
33  *    Function : createNode
34  *
35  *    Functionality:
36  *      - wraps cJSON_CreateObject cJSON library function
37  *
38  * @params[in] void
39  * @return pointer to cJSON object     - success
40  *         NULL                      - failure
41  *
42  * ****************************************************************/
43
44 cJSON * JsonHelper::createNode()
45 {
46    return  cJSON_CreateObject();
47 }
48
49 /*******************************************************************
50  *
51  * @brief wraps cJSON_AddStringToObject cJSON library function
52  *
53  * @details
54  *
55  *    Function : addNodeToObject
56  *
57  *    Functionality:
58  *      - wraps cJSON_AddStringToObject cJSON library function
59  *
60  * @params[in] cJSON * parent, const char * nodeName, const char* value
61  * @return pointer to cJSON object     - success
62  *         NULL                        - failure
63  *
64  * ****************************************************************/
65
66 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
67                                           const char * nodeName, \
68                                           const char* value)
69 {
70    return cJSON_AddStringToObject(parent, nodeName, value);
71 }
72
73 /*******************************************************************
74  *
75  * @brief wraps cJSON_AddNumberToObject cJSON library function
76  *
77  * @details
78  *
79  *    Function : addNodeToObject
80  *
81  *    Functionality:
82  *      - wraps cJSON_AddNumberToObject cJSON library function
83  *
84  * @params[in] cJSON * parent, const char * nodeName, double value
85  * @return pointer to cJSON object     - success
86  *         NULL                        - failure
87  *
88  * ****************************************************************/
89
90 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
91                                           const char * nodeName, double value)
92 {
93    return cJSON_AddNumberToObject(parent, nodeName, (double) value);
94 }
95
96
97 /*******************************************************************
98  *
99  * @brief wraps cJSON_AddItemToObject cJSON library function
100  *
101  * @details
102  *
103  *    Function : addJsonNodeToObject
104  *
105  *    Functionality:
106  *      - wraps cJSON_AddItemToObject cJSON library function
107  *
108  * @params[in] cJSON * parent, const char * nodeName, cJSON * node
109  * @return cJSON_bool non zero  - success
110  *         cJSON_bool 0         - failure
111  *
112  * ****************************************************************/
113
114 cJSON_bool JsonHelper::addJsonNodeToObject(cJSON * parent, \
115                                    const char * nodeName, cJSON * node)
116 {
117    return cJSON_AddItemToObject(parent, nodeName, node);
118 }
119
120 /*******************************************************************
121  *
122  * @brief wraps cJSON_Delete cJSON library function
123  *
124  * @details
125  *
126  *    Function : deleteNode
127  *
128  *    Functionality:
129  *      - wrapper of cJSON_AddNumberToObject
130  *
131  * @params[in] cJSON * node
132  * @return void
133  *
134  * ****************************************************************/
135
136
137 void JsonHelper::deleteNode(cJSON * node)
138 {
139    cJSON_Delete(node);
140 }
141
142 /*******************************************************************
143  *
144  * @brief wraps cJSON_PrintUnformatted cJSON library function
145  *
146  * @details
147  *
148  *    Function : addNodeToObject
149  *
150  *    Functionality:
151  *      - wraps cJSON_PrintUnformatted cJSON library function
152  *
153  * @params[in] cJSON * node
154  * @return char pointer to string  - success
155  *         nullptr                 - failure
156  *
157  * ****************************************************************/
158
159 char *JsonHelper::printUnformatted(cJSON * node)
160 {
161    return cJSON_PrintUnformatted(node);
162 }
163
164 /*******************************************************************
165  *
166  * @brief wraps cJSON_PrintUnformatted cJSON library function
167  *
168  * @details
169  *
170  *    Function : addNodeToObject
171  *
172  *    Functionality:
173  *      - wraps cJSON_PrintUnformatted cJSON library function
174  *
175  * @params[in] cJSON * node
176  * @return char pointer to json message  - success
177  *         nullptr                       - failure
178  *
179  * ****************************************************************/
180
181 char *JsonHelper::print(cJSON * node)
182 {
183    return cJSON_Print(node);
184 }
185
186 /*******************************************************************
187  *
188  * @brief wraps cJSON_PrintUnformatted cJSON library function
189  *
190  * @details
191  *
192  *    Function : addNodeToObject
193  *
194  *    Functionality:
195  *      - wraps cJSON_PrintUnformatted cJSON library function
196  *
197  * @params[in] void
198  * @return char pointer to string  - success
199  *         nullptr                 - failure
200  *
201  * ****************************************************************/
202
203 const char *JsonHelper::getError()
204 {
205    return cJSON_GetErrorPtr();
206 }
207
208
209 /*******************************************************************
210  *
211  * @brief get value of the provided node
212  *
213  * @details
214  *
215  *    Function : getValue
216  *
217  *    Functionality:
218  *      - fetch the value of the provided node
219  *
220  * @params[in] void
221  * @return pointer to string value         - success
222  *         NULL                            - failure
223  *
224  * ****************************************************************/
225
226 string JsonHelper::getValue(cJSON *json, const char *node)
227 {
228    cJSON *object;
229    string value = "";
230    object = cJSON_GetObjectItem(json, node);
231    if(object)
232    {
233       value = object->valuestring;
234       O1_LOG("O1 VES : [ %s] : [%s]\n",node, value.c_str() );
235    }
236    else
237       O1_LOG("O1 VES : node [ %s] not found\n",node);
238    return value;
239 }
240
241 /*******************************************************************
242  *
243  * @brief wraps cJSON_GetObjectItem cJSON library function
244  *
245  * @details
246  *
247  *    Function : addNodeToObject
248  *
249  *    Functionality:
250  *      - wraps cJSON_GetObjectItem cJSON library function
251  *
252  * @params[in] cJSON *json, const char *node
253  * @return pointer to cJSON node   - success
254  *         nullptr                 - failure
255  *
256  * ****************************************************************/
257
258 cJSON * JsonHelper::getNode(cJSON *json, const char *node)
259 {
260    return cJSON_GetObjectItem(json, node);
261 }
262
263
264 /*******************************************************************
265  *
266  * @brief reads json file
267  *
268  * @details
269  *
270  *    Function : read
271  *
272  *    Functionality:
273  *      - opens and reads json file and returns point to that file.
274  *
275  * @params[in] const char * fileName
276  * @return point to json file root node    - success
277  *         NULL                            - failure
278  *
279  * ****************************************************************/
280
281 cJSON* JsonHelper::read(const char * fileName)
282 {
283    std::fstream fs(fileName, std::ios::in | std::ios::binary);
284
285    if (!fs) {
286       O1_LOG("\nO1 JsonHelper : Cannot open file %s", fileName);
287       return NULL;
288    }
289
290    std::stringstream iss;
291
292    iss << fs.rdbuf();
293
294    cJSON *json = cJSON_Parse(iss.str().c_str());
295    return json;
296 }
297
298 cJSON* JsonHelper::createArray() 
299 {
300    return cJSON_CreateArray();
301 }
302
303 cJSON_bool JsonHelper::addJsonNodeToArray(cJSON * array, cJSON* node)
304 {
305    return cJSON_AddItemToArray(array, node);
306 }
307
308
309
310 /**********************************************************************
311   End of file
312  **********************************************************************/