1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /* This file contains functions to support Json related operation (read/write)*/
21 #include <bits/stdc++.h>
22 #include "JsonHelper.hpp"
23 #include "VesUtils.hpp"
27 /*******************************************************************
29 * @brief wraps cJSON_CreateObject cJSON library function
33 * Function : createNode
36 * - wraps cJSON_CreateObject cJSON library function
39 * @return pointer to cJSON object - success
42 * ****************************************************************/
44 cJSON * JsonHelper::createNode()
46 return cJSON_CreateObject();
49 /*******************************************************************
51 * @brief wraps cJSON_AddStringToObject cJSON library function
55 * Function : addNodeToObject
58 * - wraps cJSON_AddStringToObject cJSON library function
60 * @params[in] cJSON * parent, const char * nodeName, const char* value
61 * @return pointer to cJSON object - success
64 * ****************************************************************/
66 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
67 const char * nodeName, \
70 return cJSON_AddStringToObject(parent, nodeName, value);
73 /*******************************************************************
75 * @brief wraps cJSON_AddNumberToObject cJSON library function
79 * Function : addNodeToObject
82 * - wraps cJSON_AddNumberToObject cJSON library function
84 * @params[in] cJSON * parent, const char * nodeName, double value
85 * @return pointer to cJSON object - success
88 * ****************************************************************/
90 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
91 const char * nodeName, double value)
93 return cJSON_AddNumberToObject(parent, nodeName, (double) value);
97 /*******************************************************************
99 * @brief wraps cJSON_AddNumberToObject cJSON library function
103 * Function : addNodeToObject
106 * - wraps cJSON_AddNumberToObject cJSON library function
108 * @params[in] cJSON * parent, const char * nodeName, bool value
109 * @return pointer to cJSON object - success
112 * ****************************************************************/
114 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
115 const char * nodeName, bool value)
117 return cJSON_AddBoolToObject(parent, nodeName, (bool) value);
120 /*******************************************************************
122 * @brief wraps cJSON_AddItemToObject cJSON library function
126 * Function : addJsonNodeToObject
129 * - wraps cJSON_AddItemToObject cJSON library function
131 * @params[in] cJSON * parent, const char * nodeName, cJSON * node
132 * @return cJSON_bool non zero - success
133 * cJSON_bool 0 - failure
135 * ****************************************************************/
137 cJSON_bool JsonHelper::addJsonNodeToObject(cJSON * parent, \
138 const char * nodeName, cJSON * node)
140 return cJSON_AddItemToObject(parent, nodeName, node);
144 /*******************************************************************
146 * @brief wraps cJSON_Delete cJSON library function
150 * Function : deleteNode
153 * - wrapper of cJSON_AddNumberToObject
155 * @params[in] cJSON * node
158 * ****************************************************************/
161 void JsonHelper::deleteNode(cJSON * node)
166 /*******************************************************************
168 * @brief wraps cJSON_PrintUnformatted cJSON library function
172 * Function : addNodeToObject
175 * - wraps cJSON_PrintUnformatted cJSON library function
177 * @params[in] cJSON * node
178 * @return char pointer to string - success
181 * ****************************************************************/
183 char *JsonHelper::printUnformatted(cJSON * node)
185 return cJSON_PrintUnformatted(node);
188 /*******************************************************************
190 * @brief wraps cJSON_PrintUnformatted cJSON library function
194 * Function : addNodeToObject
197 * - wraps cJSON_PrintUnformatted cJSON library function
199 * @params[in] cJSON * node
200 * @return char pointer to json message - success
203 * ****************************************************************/
205 char *JsonHelper::print(cJSON * node)
207 return cJSON_Print(node);
210 /*******************************************************************
212 * @brief wraps cJSON_PrintUnformatted cJSON library function
216 * Function : addNodeToObject
219 * - wraps cJSON_PrintUnformatted cJSON library function
222 * @return char pointer to string - success
225 * ****************************************************************/
227 const char *JsonHelper::getError()
229 return cJSON_GetErrorPtr();
233 /*******************************************************************
235 * @brief get value of the provided node
239 * Function : getValue
242 * - fetch the value of the provided node
245 * @return pointer to string value - success
248 * ****************************************************************/
250 string JsonHelper::getValue(cJSON *json, const char *node)
254 object = cJSON_GetObjectItem(json, node);
257 value = object->valuestring;
258 O1_LOG("O1 VES : [ %s] : [%s]\n",node, value.c_str() );
261 O1_LOG("O1 VES : node [ %s] not found\n",node);
265 /*******************************************************************
267 * @brief wraps cJSON_GetObjectItem cJSON library function
271 * Function : addNodeToObject
274 * - wraps cJSON_GetObjectItem cJSON library function
276 * @params[in] cJSON *json, const char *node
277 * @return pointer to cJSON node - success
280 * ****************************************************************/
282 cJSON * JsonHelper::getNode(cJSON *json, const char *node)
284 return cJSON_GetObjectItem(json, node);
288 /*******************************************************************
290 * @brief reads json file
297 * - opens and reads json file and returns point to that file.
299 * @params[in] const char * fileName
300 * @return point to json file root node - success
303 * ****************************************************************/
305 cJSON* JsonHelper::read(const char * fileName)
307 std::fstream fs(fileName, std::ios::in | std::ios::binary);
310 O1_LOG("\nO1 JsonHelper : Cannot open file %s", fileName);
314 std::stringstream iss;
318 cJSON *json = cJSON_Parse(iss.str().c_str());
322 cJSON* JsonHelper::createArray()
324 return cJSON_CreateArray();
327 cJSON_bool JsonHelper::addJsonNodeToArray(cJSON * array, cJSON* node)
329 return cJSON_AddItemToArray(array, node);
334 /**********************************************************************
336 **********************************************************************/