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_AddItemToObject cJSON library function
103 * Function : addJsonNodeToObject
106 * - wraps cJSON_AddItemToObject cJSON library function
108 * @params[in] cJSON * parent, const char * nodeName, cJSON * node
109 * @return cJSON_bool non zero - success
110 * cJSON_bool 0 - failure
112 * ****************************************************************/
114 cJSON_bool JsonHelper::addJsonNodeToObject(cJSON * parent, \
115 const char * nodeName, cJSON * node)
117 return cJSON_AddItemToObject(parent, nodeName, node);
120 /*******************************************************************
122 * @brief wraps cJSON_Delete cJSON library function
126 * Function : deleteNode
129 * - wrapper of cJSON_AddNumberToObject
131 * @params[in] cJSON * node
134 * ****************************************************************/
137 void JsonHelper::deleteNode(cJSON * node)
142 /*******************************************************************
144 * @brief wraps cJSON_PrintUnformatted cJSON library function
148 * Function : addNodeToObject
151 * - wraps cJSON_PrintUnformatted cJSON library function
153 * @params[in] cJSON * node
154 * @return char pointer to string - success
157 * ****************************************************************/
159 char *JsonHelper::printUnformatted(cJSON * node)
161 return cJSON_PrintUnformatted(node);
164 /*******************************************************************
166 * @brief wraps cJSON_PrintUnformatted cJSON library function
170 * Function : addNodeToObject
173 * - wraps cJSON_PrintUnformatted cJSON library function
175 * @params[in] cJSON * node
176 * @return char pointer to json message - success
179 * ****************************************************************/
181 char *JsonHelper::print(cJSON * node)
183 return cJSON_Print(node);
186 /*******************************************************************
188 * @brief wraps cJSON_PrintUnformatted cJSON library function
192 * Function : addNodeToObject
195 * - wraps cJSON_PrintUnformatted cJSON library function
198 * @return char pointer to string - success
201 * ****************************************************************/
203 const char *JsonHelper::getError()
205 return cJSON_GetErrorPtr();
209 /*******************************************************************
211 * @brief get value of the provided node
215 * Function : getValue
218 * - fetch the value of the provided node
221 * @return pointer to string value - success
224 * ****************************************************************/
226 string JsonHelper::getValue(cJSON *json, const char *node)
230 object = cJSON_GetObjectItem(json, node);
233 value = object->valuestring;
234 O1_LOG("O1 VES : [ %s] : [%s]\n",node, value.c_str() );
237 O1_LOG("O1 VES : node [ %s] not found\n",node);
241 /*******************************************************************
243 * @brief wraps cJSON_GetObjectItem cJSON library function
247 * Function : addNodeToObject
250 * - wraps cJSON_GetObjectItem cJSON library function
252 * @params[in] cJSON *json, const char *node
253 * @return pointer to cJSON node - success
256 * ****************************************************************/
258 cJSON * JsonHelper::getNode(cJSON *json, const char *node)
260 return cJSON_GetObjectItem(json, node);
264 /*******************************************************************
266 * @brief reads json file
273 * - opens and reads json file and returns point to that file.
275 * @params[in] const char * fileName
276 * @return point to json file root node - success
279 * ****************************************************************/
281 cJSON* JsonHelper::read(const char * fileName)
283 std::fstream fs(fileName, std::ios::in | std::ios::binary);
286 O1_LOG("\nO1 JsonHelper : Cannot open file %s", fileName);
290 std::stringstream iss;
294 cJSON *json = cJSON_Parse(iss.str().c_str());
298 cJSON* JsonHelper::createArray()
300 return cJSON_CreateArray();
303 cJSON_bool JsonHelper::addJsonNodeToArray(cJSON * array, cJSON* node)
305 return cJSON_AddItemToArray(array, node);
310 /**********************************************************************
312 **********************************************************************/