json_util.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2017 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file json_util.c
9 * @date Feb 22, 2017
10 * @brief Json util module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "cJSON/cJSON.h"
15 #include "json_util.h"
16 #include "mcl/mcl_json_util.h"
17 #include "definitions.h"
18 #include "log_util.h"
19 #include "memory.h"
20 #include "string_util.h"
21 
22 // Private Function Prototypes:
23 static void _finish_json_item(json_t **json_item);
25 
26 static cJSON_Hooks cjson_hooks;
27 
29 {
30  DEBUG_ENTRY("void")
31 
32  cjson_hooks.malloc_fn = memory_malloc;
33  cjson_hooks.free_fn = memory_free;
34  cJSON_InitHooks(&cjson_hooks);
35 
36  DEBUG_LEAVE("retVal = void");
37 }
38 
40 {
41  DEBUG_ENTRY("E_MCL_JSON_TYPE json_type = <%d>, mcl_json_t **root = <%p>", json_type, root)
42 
43  E_MCL_ERROR_CODE code;
44 
45  ASSERT_NOT_NULL(root);
46 
48 
49  DEBUG_LEAVE("retVal = <%d>", code);
50  return code;
51 }
52 
54 {
55  VERBOSE_ENTRY("E_JSON_TYPE json_type = <%p>, json_t **root = <%p>", &json_type, root)
56 
57  MCL_NEW(*root);
58  ASSERT_CODE_MESSAGE(MCL_NULL != *root, MCL_OUT_OF_MEMORY, "root couldn't be allocated in memory.");
59 
60  (*root)->root_handle = MCL_NULL;
61 
62  switch (json_type)
63  {
64  case JSON_ARRAY :
65  (*root)->root_handle = cJSON_CreateArray();
66 
67  break;
68  case JSON_OBJECT :
69  (*root)->root_handle = cJSON_CreateObject();
70 
71  break;
72  default :
73  MCL_VERBOSE("json_type = <%d> is out of valid types.", json_type);
74 
75  break;
76  }
77 
78  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != (*root)->root_handle, MCL_FREE(*root), MCL_OUT_OF_MEMORY, "root_handle couldn't be allocated in memory.");
79 
80  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
81  return MCL_OK;
82 }
83 
84 E_MCL_ERROR_CODE mcl_json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
85 {
86  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *array_name = <%p>, mcl_json_t **json_array = <%p>", root, array_name, json_array)
87 
88  E_MCL_ERROR_CODE code;
89 
90  ASSERT_NOT_NULL(root);
91  ASSERT_NOT_NULL(array_name);
92  ASSERT_NOT_NULL(json_array);
93 
94  code = json_util_start_array(root, array_name, json_array);
95 
96  DEBUG_LEAVE("retVal = <%d>", code);
97  return code;
98 }
99 
100 E_MCL_ERROR_CODE json_util_start_array(json_t *root, const char *array_name, json_t **json_array)
101 {
102  VERBOSE_ENTRY("json_t *root = <%p>, const char *array_name = <%p>, json_t **json_array = <%p>", root, array_name, json_array)
103 
104  json_t *json_child = MCL_NULL;
105  E_MCL_ERROR_CODE code = json_util_get_object_item(root, array_name, &json_child);
106  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
107  MCL_FREE(json_child);
108 
109  if (MCL_NON_EXISTING_JSON_CHILD == code)
110  {
111  code = json_util_initialize(JSON_ARRAY, json_array);
112  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "json_array couldn't be allocated in memory.");
113 
114  cJSON_AddItemToObject(root->root_handle, array_name, (*json_array)->root_handle);
115  }
116  else
117  {
118  VERBOSE_LEAVE("retVal = <%d>", MCL_JSON_NAME_DUPLICATION);
120  }
121 
122  VERBOSE_LEAVE("retVal = <%d>", code);
123  return code;
124 }
125 
127 {
128  DEBUG_ENTRY("mcl_json_t *array = <%p>, int index = <%d>, mcl_json_t **item = <%p>", array, index, item)
129 
130  E_MCL_ERROR_CODE code;
131 
132  ASSERT_NOT_NULL(array);
133  ASSERT_NOT_NULL(item);
134 
135  code = json_util_get_array_item(array, index, item);
136 
137  DEBUG_LEAVE("retVal = <%d>", code);
138  return code;
139 }
140 
142 {
143  VERBOSE_ENTRY("json_t *root = <%p>, int index = <%d>", array, index, item)
144 
145  cJSON *item_cjson;
146 
147  MCL_NEW(*item);
148  ASSERT_CODE_MESSAGE(MCL_NULL != *item, MCL_OUT_OF_MEMORY, "Could not allocate memory for item!");
149 
150  (*item)->root_handle = MCL_NULL;
151 
152  item_cjson = cJSON_GetArrayItem(array->root_handle, index);
153  (*item)->root_handle = cJSON_Duplicate(item_cjson, MCL_TRUE);
154  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != (*item)->root_handle, MCL_FREE(*item), MCL_FAIL, "Could not get item from json array.");
155 
156  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
157  return MCL_OK;
158 }
159 
161 {
162  DEBUG_ENTRY("mcl_json_t *array = <%p>, int *size = <%p>", array, size)
163 
164  ASSERT_NOT_NULL(array);
165  ASSERT_NOT_NULL(size);
166 
167  json_util_get_array_size(array, size);
168 
169  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
170  return MCL_OK;
171 }
172 
174 {
175  VERBOSE_ENTRY("json_t *array = <%p>, mcl_size_t *size = <%p>", array, size)
176 
177  *size = (mcl_size_t)cJSON_GetArraySize(array->root_handle);
178 
179  VERBOSE_LEAVE("retVal = void");
180 }
181 
182 E_MCL_ERROR_CODE mcl_json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
183 {
184  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t **json_object = <%p>", root, object_name, json_object)
185 
186  E_MCL_ERROR_CODE code;
187 
188  ASSERT_NOT_NULL(root);
189  ASSERT_NOT_NULL(object_name);
190  ASSERT_NOT_NULL(json_object);
191 
192  code = json_util_start_object(root, object_name, json_object);
193 
194  DEBUG_LEAVE("retVal = <%d>", code);
195  return code;
196 }
197 
198 E_MCL_ERROR_CODE json_util_start_object(json_t *root, const char *object_name, json_t **json_object)
199 {
200  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, json_t **json_object = <%p>", root, object_name, json_object)
201 
202  json_t *json_child = MCL_NULL;
203  E_MCL_ERROR_CODE code = json_util_get_object_item(root, object_name, &json_child);
204  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
205  MCL_FREE(json_child);
206 
207  if (MCL_NON_EXISTING_JSON_CHILD != code)
208  {
209  VERBOSE_LEAVE("retVal = <%d>", MCL_JSON_NAME_DUPLICATION);
211  }
212 
213  code = json_util_initialize(JSON_OBJECT, json_object);
214  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "json_object couldn't be allocated in memory.");
215 
216  cJSON_AddItemToObject(root->root_handle, object_name, (*json_object)->root_handle);
217 
218  VERBOSE_LEAVE("retVal = <%d>", code);
219  return code;
220 }
221 
222 E_MCL_ERROR_CODE mcl_json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
223 {
224  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const char *object_value = <%p>", root, object_name, object_value)
225 
226  E_MCL_ERROR_CODE code;
227 
228  ASSERT_NOT_NULL(root);
229  ASSERT_NOT_NULL(object_value);
230 
231  code = json_util_add_string(root, object_name, object_value);
232 
233  DEBUG_LEAVE("retVal = <%d>", code);
234  return code;
235 }
236 
237 E_MCL_ERROR_CODE json_util_add_string(json_t *root, const char *object_name, const char *object_value)
238 {
239  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, const char *object_value = <%p>", root, object_name, object_value)
240 
241  E_MCL_ERROR_CODE code = MCL_OK;
242 
243  if (cJSON_Array == root->root_handle->type && MCL_NULL == object_name)
244  {
245  cJSON *string = cJSON_CreateString(object_value);
246  cJSON_AddItemToArray(root->root_handle, string);
247  }
248  else if (cJSON_Object == root->root_handle->type && MCL_NULL != object_name)
249  {
250  json_t *json_child = MCL_NULL;
251  code = json_util_get_object_item(root, object_name, &json_child);
252  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
253  MCL_FREE(json_child);
254 
255  // If object_name is already used in the root, free json_child and return error.
256  ASSERT_CODE_MESSAGE(MCL_NON_EXISTING_JSON_CHILD == code, MCL_JSON_NAME_DUPLICATION, "Name duplication fail for object_name.");
257 
258  // Add string to object.
259  cJSON_AddStringToObject(root->root_handle, object_name, object_value);
260  code = MCL_OK;
261  }
262  else
263  {
264  code = MCL_INVALID_PARAMETER;
265  }
266 
267  VERBOSE_LEAVE("retVal = <%d>", code);
268  return code;
269 }
270 
271 E_MCL_ERROR_CODE mcl_json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
272 {
273  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const mcl_size_t number = <%u>", root, object_name, &number)
274 
275  E_MCL_ERROR_CODE code;
276 
277  ASSERT_NOT_NULL(root);
278 
279  code = json_util_add_uint(root, object_name, number);
280 
281  DEBUG_LEAVE("retVal = <%d>", code);
282  return code;
283 }
284 
285 E_MCL_ERROR_CODE json_util_add_uint(json_t *root, const char *object_name, const mcl_size_t number)
286 {
287  VERBOSE_ENTRY("json_t *root = <%p>, char *object_name = <%p>, mcl_size_t number = <%u>", root, object_name, number)
288 
289  E_MCL_ERROR_CODE code = MCL_OK;
290 
291  if (cJSON_Array == root->root_handle->type && MCL_NULL == object_name)
292  {
293  cJSON *json_number = cJSON_CreateNumber((double)number);
294  cJSON_AddItemToArray(root->root_handle, json_number);
295  }
296  else if (cJSON_Object == root->root_handle->type && MCL_NULL != object_name)
297  {
298  json_t *json_child = MCL_NULL;
299  code = json_util_get_object_item(root, object_name, &json_child);
300  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
301  MCL_FREE(json_child);
302 
303  // If object_name is already used in the root, free json_child and return error.
304  ASSERT_CODE_MESSAGE(MCL_NON_EXISTING_JSON_CHILD == code, MCL_JSON_NAME_DUPLICATION, "Name duplication fail for object_name.");
305 
306  // Add uint to object. This function expects a double and type cast to integer inside the function.
307  cJSON_AddNumberToObject(root->root_handle, object_name, (double)number);
308  code = MCL_OK;
309  }
310  else
311  {
312  code = MCL_INVALID_PARAMETER;
313  }
314 
315  VERBOSE_LEAVE("retVal = <%d>", code);
316  return code;
317 }
318 
319 E_MCL_ERROR_CODE mcl_json_util_add_float(mcl_json_t *root, const char *object_name, const float number)
320 {
321  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const float number = <%p>", root, object_name, &number)
322 
323  E_MCL_ERROR_CODE code = json_util_add_float(root, object_name, number);
324 
325  DEBUG_LEAVE("retVal = <%d>", code);
326  return code;
327 }
328 
329 E_MCL_ERROR_CODE json_util_add_float(json_t *root, const char *object_name, const float number)
330 {
331  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, const float number = <%f>", root, object_name, number)
332 
333  // After completing the function, name duplication should be checked.
334 
335  // Unused parameters.
336  (void)root;
337  (void)object_name;
338  (void)number;
339 
342 }
343 
344 E_MCL_ERROR_CODE mcl_json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
345 {
346  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const double number = <%f>", root, object_name, &number)
347 
348  E_MCL_ERROR_CODE code;
349 
350  ASSERT_NOT_NULL(root);
351 
352  code = json_util_add_double(root, object_name, number);
353 
354  DEBUG_LEAVE("retVal = <%d>", code);
355  return code;
356 }
357 
358 E_MCL_ERROR_CODE json_util_add_double(json_t *root, const char *object_name, const double number)
359 {
360  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, const double number = <%f>", root, object_name, number)
361 
362  E_MCL_ERROR_CODE code = MCL_OK;
363 
364  if (cJSON_Array == root->root_handle->type && MCL_NULL == object_name)
365  {
366  cJSON *json_number = cJSON_CreateNumber(number);
367  cJSON_AddItemToArray(root->root_handle, json_number);
368  }
369  else if (cJSON_Object == root->root_handle->type && MCL_NULL != object_name)
370  {
371  json_t *json_child = MCL_NULL;
372  code = json_util_get_object_item(root, object_name, &json_child);
373  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
374  MCL_FREE(json_child);
375 
376  // If object_name is already used in the root, free json_child and return error.
377  ASSERT_CODE_MESSAGE(MCL_NON_EXISTING_JSON_CHILD == code, MCL_JSON_NAME_DUPLICATION, "Name duplication fail for object_name.");
378 
379  // Add double to object.
380  cJSON_AddNumberToObject(root->root_handle, object_name, number);
381  code = MCL_OK;
382  }
383  else
384  {
385  code = MCL_INVALID_PARAMETER;
386  }
387 
388  VERBOSE_LEAVE("retVal = <%d>", code);
389  return code;
390 }
391 
392 E_MCL_ERROR_CODE mcl_json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
393 {
394  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const mcl_bool_t bool_value = <%d>", root, object_name, &bool_value)
395 
396  E_MCL_ERROR_CODE code;
397 
398  ASSERT_NOT_NULL(root);
399 
400  code = json_util_add_bool(root, object_name, bool_value);
401 
402  DEBUG_LEAVE("retVal = <%d>", code);
403  return code;
404 }
405 
406 E_MCL_ERROR_CODE json_util_add_bool(json_t *root, const char *object_name, const mcl_bool_t bool_value)
407 {
408  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, const mcl_bool_t bool_value = <%d>", root, object_name, bool_value)
409 
410  E_MCL_ERROR_CODE code = MCL_OK;
411 
412  if (cJSON_Array == root->root_handle->type && MCL_NULL == object_name)
413  {
414  cJSON *json_bool = cJSON_CreateBool(bool_value);
415  cJSON_AddItemToArray(root->root_handle, json_bool);
416  }
417  else if (cJSON_Object == root->root_handle->type && MCL_NULL != object_name)
418  {
419  json_t *json_child = MCL_NULL;
420  code = json_util_get_object_item(root, object_name, &json_child);
421  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
422  MCL_FREE(json_child);
423 
424  // If object_name is already used in the root, free json_child and return error.
425  ASSERT_CODE_MESSAGE(MCL_NON_EXISTING_JSON_CHILD == code, MCL_JSON_NAME_DUPLICATION, "Name duplication fail for object_name.");
426 
427  // Add bool to object.
428  cJSON_AddBoolToObject(root->root_handle, object_name, bool_value);
429  code = MCL_OK;
430  }
431  else
432  {
433  code = MCL_INVALID_PARAMETER;
434  }
435 
436  VERBOSE_LEAVE("retVal = <%d>", code);
437  return code;
438 }
439 
440 E_MCL_ERROR_CODE mcl_json_util_add_null(mcl_json_t *root, const char *object_name)
441 {
442  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>", root, object_name)
443 
444  E_MCL_ERROR_CODE code;
445 
446  ASSERT_NOT_NULL(root);
447 
448  code = json_util_add_null(root, object_name);
449 
450  DEBUG_LEAVE("retVal = <%d>", code);
451  return code;
452 }
453 
454 E_MCL_ERROR_CODE json_util_add_null(json_t *root, const char *object_name)
455 {
456  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>", root, object_name)
457 
458  E_MCL_ERROR_CODE code = MCL_OK;
459 
460  if (cJSON_Array == root->root_handle->type && MCL_NULL == object_name)
461  {
462  cJSON *json_null = cJSON_CreateNull();
463  cJSON_AddItemToArray(root->root_handle, json_null);
464  }
465  else if (cJSON_Object == root->root_handle->type && MCL_NULL != object_name)
466  {
467  json_t *json_child = MCL_NULL;
468  code = json_util_get_object_item(root, object_name, &json_child);
469  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
470  MCL_FREE(json_child);
471 
472  // If object_name is already used in the root, free json_child and return error.
473  ASSERT_CODE_MESSAGE(MCL_NON_EXISTING_JSON_CHILD == code, MCL_JSON_NAME_DUPLICATION, "Name duplication fail for object_name.");
474 
475  // Add null to object.
476  cJSON_AddNullToObject(root->root_handle, object_name);
477  code = MCL_OK;
478  }
479  else
480  {
481  code = MCL_INVALID_PARAMETER;
482  }
483 
484  VERBOSE_LEAVE("retVal = <%d>", code);
485  return code;
486 }
487 
488 E_MCL_ERROR_CODE mcl_json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
489 {
490  DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t *object = <%p>", root, object_name, object)
491 
492  E_MCL_ERROR_CODE code;
493 
494  ASSERT_NOT_NULL(root);
495  ASSERT_NOT_NULL(object_name);
496  ASSERT_NOT_NULL(object);
497 
498  code = json_util_add_object(root, object_name, object);
499 
500  DEBUG_LEAVE("retVal = <%d>", code);
501  return MCL_OK;
502 }
503 
504 E_MCL_ERROR_CODE json_util_add_object(json_t *root, const char *object_name, json_t *object)
505 {
506  VERBOSE_ENTRY("json_t *root = <%p>, const char *object_name = <%p>, json_t *object = <%p>", root, object_name, object)
507 
508  json_t *json_child = MCL_NULL;
509  E_MCL_ERROR_CODE code = json_util_get_object_item(root, object_name, &json_child);
510  ASSERT_CODE_MESSAGE(MCL_OUT_OF_MEMORY != code, code, "json_child couldn't be allocated in memory.");
511  MCL_FREE(json_child);
512 
513  if (MCL_NON_EXISTING_JSON_CHILD == code)
514  {
515  cJSON_AddItemToObject(root->root_handle, object_name, object->root_handle);
516  }
517  else
518  {
519  VERBOSE_LEAVE("retVal = <%d>", MCL_JSON_NAME_DUPLICATION);
521  }
522 
523  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
524  return MCL_OK;
525 }
526 
528 {
529  DEBUG_ENTRY("mcl_json_t *root = <%p>, mcl_json_t *object = <%p>", root, object)
530 
531  ASSERT_NOT_NULL(root);
532  ASSERT_NOT_NULL(object);
533 
534  json_util_add_item_to_array(root, object);
535 
536  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
537  return MCL_OK;
538 }
539 
541 {
542  VERBOSE_ENTRY("json_t *root = <%p>, json_t *object = <%p>", root, object)
543 
544  cJSON_AddItemToArray(root->root_handle, object->root_handle);
545 
546  VERBOSE_LEAVE("retVal = void");
547 }
548 
549 E_MCL_ERROR_CODE mcl_json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
550 {
551  DEBUG_ENTRY("mcl_json_t *json_parent = <%p>, const char *child_name = <%p>, mcl_json_t **json_child = <%p>", json_parent, child_name, json_child)
552 
553  E_MCL_ERROR_CODE code;
554 
555  ASSERT_NOT_NULL(json_parent);
556  ASSERT_NOT_NULL(child_name);
557  ASSERT_NOT_NULL(json_child);
558 
559  code = json_util_get_object_item(json_parent, child_name, json_child);
560 
561  DEBUG_LEAVE("retVal = <%d>", code);
562  return code;
563 }
564 
565 E_MCL_ERROR_CODE json_util_get_object_item(json_t *json_parent, const char *child_name, json_t **json_child)
566 {
567  VERBOSE_ENTRY("json_t *json_parent = <%p>, const char *child_name = <%p>, json_t **json_child = <%p>", json_parent, child_name, json_child)
568 
569  E_MCL_ERROR_CODE return_code = MCL_OK;
570 
571  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*json_child), MCL_OUT_OF_MEMORY, "json_child couldn't be allocated in memory.");
572 
573  // Returns the address of object item to root handle w/o allocation.
574  (*json_child)->root_handle = cJSON_GetObjectItem(json_parent->root_handle, child_name);
575 
576  // Check if child object doesn't exist.
577  if (MCL_NULL == (*json_child)->root_handle)
578  {
579  MCL_FREE(*json_child);
580  return_code = MCL_NON_EXISTING_JSON_CHILD;
581  }
582 
583  VERBOSE_LEAVE("retVal = <%d>", return_code);
584  return return_code;
585 }
586 
588 {
589  DEBUG_ENTRY("mcl_json_t *root = <%p>, mcl_bool_t *result = <%p>", root, result)
590 
591  ASSERT_NOT_NULL(root);
592  ASSERT_NOT_NULL(result);
593 
594  *result = json_util_has_child(root);
595 
596  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
597  return MCL_OK;
598 }
599 
601 {
602  VERBOSE_ENTRY("json_t *root = <%p>", root)
603 
604  mcl_bool_t return_value = MCL_FALSE;
605 
606  // If root has child make result true.
607  if (MCL_NULL != root->root_handle->child)
608  {
609  return_value = MCL_TRUE;
610  }
611 
612  VERBOSE_LEAVE("retVal = <%d>", return_value);
613  return return_value;
614 }
615 
617 {
618  DEBUG_ENTRY("mcl_json_t *json = <%p>, mcl_int32_t *number_value = <%p>", json, number_value)
619 
620  ASSERT_NOT_NULL(json);
621  ASSERT_NOT_NULL(number_value);
622 
623  json_util_get_number_value(json, number_value);
624 
625  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
626  return MCL_OK;
627 }
628 
630 {
631  VERBOSE_ENTRY("json_t *json = <%p>, mcl_int32_t *number_value = <%p>", json, number_value)
632 
633  *number_value = json->root_handle->valueint;
634 
635  VERBOSE_LEAVE("retVal = void");
636 }
637 
639 {
640  DEBUG_ENTRY("mcl_json_t *json = <%p>, double *double_value = <%p>", json, double_value)
641 
642  ASSERT_NOT_NULL(json);
643  ASSERT_NOT_NULL(double_value);
644 
645  json_util_get_double_value(json, double_value);
646 
647  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
648  return MCL_OK;
649 }
650 
651 void json_util_get_double_value(json_t *json, double *double_value)
652 {
653  VERBOSE_ENTRY("json_t *json = <%p>, double *double_value = <%p>", json, double_value)
654 
655  *double_value = json->root_handle->valuedouble;
656 
657  VERBOSE_LEAVE("retVal = void");
658 }
659 
661 {
662  DEBUG_ENTRY("json_t *json = <%p>, mcl_bool_t *bool_value = <%p>", json, bool_value)
663 
664  ASSERT_NOT_NULL(json);
665  ASSERT_NOT_NULL(bool_value);
666 
667  json_util_get_bool_value(json, bool_value);
668 
669  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
670  return MCL_OK;
671 }
672 
674 {
675  VERBOSE_ENTRY("json_t *json = <%p>, mcl_bool_t *bool_value = <%p>", json, bool_value)
676 
677  if(cJSON_True == json->root_handle->type)
678  {
679  *bool_value = MCL_TRUE;
680  }
681  else
682  {
683  *bool_value = MCL_FALSE;
684  }
685 
686  VERBOSE_LEAVE("retVal = void");
687 }
688 
689 E_MCL_ERROR_CODE mcl_json_util_get_string(mcl_json_t *json_item, char **string_value)
690 {
691  DEBUG_ENTRY("mcl_json_t *json_item = <%p>, char **string_value = <%p>", json_item, string_value)
692 
693  E_MCL_ERROR_CODE code;
694  string_t *string_value_local = MCL_NULL;
695 
696  ASSERT_NOT_NULL(json_item);
697  ASSERT_NOT_NULL(string_value);
698 
699  code = json_util_get_string(json_item, &string_value_local);
700  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Memory can not be allocated for string_value_local.");
701 
702  *string_value = string_value_local->buffer;
703 
704  // Free only the struct, its buffer should stay.
705  MCL_FREE(string_value_local);
706 
707  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
708  return MCL_OK;
709 }
710 
712 {
713  VERBOSE_ENTRY("json_t *json_item = <%p>, char **string_value = <%p>", json_item, string_value)
714 
715  ASSERT_CODE_MESSAGE(MCL_OK == string_initialize_new(json_item->root_handle->valuestring, 0, string_value), MCL_OUT_OF_MEMORY,
716  "Memory can not be allocated for string_value.");
717 
718  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
719  return MCL_OK;
720 }
721 
723 {
724  DEBUG_ENTRY("mcl_json_t *root = <%p>, char **json_string = <%p>", root, json_string)
725 
726  E_MCL_ERROR_CODE code;
727 
728  ASSERT_NOT_NULL(root);
729  ASSERT_NOT_NULL(json_string);
730 
731  code = json_util_to_string(root, json_string);
732 
733  DEBUG_LEAVE("retVal = <%d>", code);
734  return code;
735 }
736 
737 E_MCL_ERROR_CODE json_util_to_string(json_t *root, char **json_string)
738 {
739  VERBOSE_ENTRY("json_t *root = <%p>, char **json_string = <%p>", root, json_string)
740 
741  *json_string = cJSON_PrintUnformatted(root->root_handle);
742  ASSERT_CODE_MESSAGE(MCL_NULL != *json_string, MCL_FAIL, "Either the given json object is invalid or memory can not be allocated for root.");
743 
744  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
745  return MCL_OK;
746 }
747 
748 E_MCL_ERROR_CODE mcl_json_util_parse(const char *json_string, mcl_json_t **root)
749 {
750  DEBUG_ENTRY("const char *json_string = <%p>, mcl_size_t size = <%u>, mcl_json_t **root = <%p>", json_string, size, root);
751 
752  E_MCL_ERROR_CODE code;
753 
754  ASSERT_NOT_NULL(json_string);
755  ASSERT_NOT_NULL(root);
756 
757  code = json_util_parse(json_string, root);
758 
759  DEBUG_LEAVE("retVal = <%d>", code);
760  return code;
761 }
762 
763 E_MCL_ERROR_CODE json_util_parse(const char *json_string, json_t **root)
764 {
765  VERBOSE_ENTRY("const char *json_string = <%p>, size_t size = <%u>, json_t **root = <%p>", json_string, size, root);
766 
767  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*root), MCL_OUT_OF_MEMORY, "Memory can not be allocated for root.");
768 
769  (*root)->root_handle = cJSON_Parse(json_string);
770 
771  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != (*root)->root_handle, MCL_FREE(*root), MCL_FAIL, "json string could not be parsed.");
772 
773  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
774  return MCL_OK;
775 }
776 
777 E_MCL_ERROR_CODE json_util_parse_with_size(const char *json_string, mcl_size_t size, json_t **root)
778 {
779  VERBOSE_ENTRY("const char *json_string = <%p>, size_t size = <%u>, json_t **root = <%p>", json_string, size, root);
780 
781  E_MCL_ERROR_CODE code = MCL_OK;
782 
783  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*root), MCL_OUT_OF_MEMORY, "Memory can not be allocated for root.");
784 
785  if (0 == size)
786  {
787  (*root)->root_handle = cJSON_Parse(json_string);
788  }
789  else
790  {
791  (*root)->root_handle = cJSON_ParseWithSize(json_string, size);
792  }
793 
794  if (MCL_NULL == (*root))
795  {
796  code = MCL_FAIL;
797  }
798 
799  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != (*root)->root_handle, MCL_FREE(*root), MCL_FAIL, "json string could not be parsed.");
800 
801  VERBOSE_LEAVE("retVal = <%d>", code);
802  return code;
803 }
804 
805 E_MCL_ERROR_CODE json_util_duplicate(const json_t *source_json, mcl_bool_t with_children, json_t **duplicated_json)
806 {
807  VERBOSE_ENTRY("const json_t *source_json = <%p>, mcl_bool_t with_children = <%d>, json_t **duplicated_json = <%p>", source_json, with_children, duplicated_json)
808  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*duplicated_json), MCL_OUT_OF_MEMORY, "Memory can not be allocated for duplicated_json.");
809 
810  (*duplicated_json)->root_handle = MCL_NULL;
811  (*duplicated_json)->root_handle = cJSON_Duplicate(source_json->root_handle, with_children);
812  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != (*duplicated_json)->root_handle, MCL_FREE(*duplicated_json), MCL_OUT_OF_MEMORY, "cJSON_Duplicate() failed.");
813 
814  VERBOSE_LEAVE("retVal = <%d>", MCL_OK);
815  return MCL_OK;
816 }
817 
819 {
820  DEBUG_ENTRY("json_t **json_array = <%p>", json_array)
821 
822  json_util_finish_array(json_array);
823 
824  DEBUG_LEAVE("retVal = void");
825 }
826 
827 void json_util_finish_array(json_t **json_array)
828 {
829  VERBOSE_ENTRY("json_t **json_array = <%p>", json_array)
830 
831  _finish_json_item(json_array);
832 
833  VERBOSE_LEAVE("retVal = void");
834 }
835 
837 {
838  DEBUG_ENTRY("json_t **json_object = <%p>", json_object)
839 
840  json_util_finish_object(json_object);
841 
842  DEBUG_LEAVE("retVal = void");
843 }
844 
845 void json_util_finish_object(json_t **json_object)
846 {
847  VERBOSE_ENTRY("json_t **json_object = <%p>", json_object)
848 
849  _finish_json_item(json_object);
850 
851  VERBOSE_LEAVE("retVal = void");
852 }
853 
855 {
856  DEBUG_ENTRY("json_t **root = <%p>", root)
857 
858  json_util_destroy(root);
859 
860  DEBUG_LEAVE("retVal = void");
861 }
862 
864 {
865  VERBOSE_ENTRY("json_t **root = <%p>", root)
866 
867  if (MCL_NULL != *root)
868  {
869  cJSON_Delete((*root)->root_handle);
870  MCL_FREE(*root);
871  }
872 
873  VERBOSE_LEAVE("retVal = void");
874 }
875 
876 static void _finish_json_item(json_t **json_item)
877 {
878  VERBOSE_ENTRY("json_t **json_item = <%p>", json_item)
879 
880  if (MCL_NULL != *json_item)
881  {
882  MCL_FREE(*json_item);
883  }
884 
885  VERBOSE_LEAVE("retVal = void");
886 }
887 
889 {
890  VERBOSE_ENTRY("E_MCL_JSON_TYPE mcl_json_type = <%d>", mcl_json_type)
891 
892  E_JSON_TYPE json_type = JSON_NULL;
893 
894  switch (mcl_json_type)
895  {
896  case MCL_JSON_ARRAY :
897  json_type = JSON_ARRAY;
898 
899  break;
900  case MCL_JSON_OBJECT :
901  json_type = JSON_OBJECT;
902 
903  break;
904  default :
905  MCL_VERBOSE("mcl_json_type = <%d> is out of valid types.", mcl_json_type);
906 
907  break;
908  }
909 
910  VERBOSE_LEAVE("retVal = <%d>", json_type);
911  return json_type;
912 }
struct mcl_json_t mcl_json_t
This struct is used for json handling.
mcl_bool_t json_util_has_child(json_t *root)
This function checks whether root object has child object or not.
Definition: json_util.c:600
Internal failure in MCL.
Definition: mcl_common.h:141
E_MCL_ERROR_CODE mcl_json_util_get_double_value(mcl_json_t *json, double *double_value)
This function gets the double value of a given json object.
Definition: json_util.c:638
Memory module header file.
E_MCL_ERROR_CODE mcl_json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
This function creates an object in root.
Definition: json_util.c:182
#define VERBOSE_LEAVE(...)
Definition: log_util.h:66
E_MCL_ERROR_CODE json_util_parse(const char *json_string, json_t **root)
This function parses the given string to the given json object.
Definition: json_util.c:763
E_MCL_ERROR_CODE json_util_add_uint(json_t *root, const char *object_name, const mcl_size_t number)
This function adds integer number to root which can be object or array.
Definition: json_util.c:285
E_MCL_ERROR_CODE mcl_json_util_get_array_size(mcl_json_t *array, mcl_size_t *size)
This function returns the size of array.
Definition: json_util.c:160
E_MCL_ERROR_CODE json_util_add_bool(json_t *root, const char *object_name, const mcl_bool_t bool_value)
This function adds bool_value to root which can be object or array.
Definition: json_util.c:406
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
cJSON * root_handle
Root cJson object.
Definition: json_util.h:29
E_MCL_ERROR_CODE json_util_get_string(json_t *json_item, string_t **string_value)
This function gets the string value of a given json object.
Definition: json_util.c:711
Json util module header file.
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_NEW(p)
Definition: memory.h:121
If a particular operation is not supported.
Definition: mcl_common.h:148
E_MCL_ERROR_CODE string_initialize_new(const char *value, mcl_size_t value_length, string_t **string)
Initializes a new string_t object with the given value and length.
Definition: string_type.c:46
E_MCL_ERROR_CODE mcl_json_util_has_child(mcl_json_t *root, mcl_bool_t *result)
This function checks whether root object has child object or not.
Definition: json_util.c:587
#define MCL_TRUE
Definition: mcl_common.h:54
Json object.
Definition: mcl_json_util.h:37
char * buffer
Buffer of string handle.
Definition: string_type.h:45
E_MCL_ERROR_CODE mcl_json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
This function adds object to root.
Definition: json_util.c:488
Json array.
Definition: mcl_json_util.h:36
E_MCL_ERROR_CODE json_util_start_array(json_t *root, const char *array_name, json_t **json_array)
This function creates an array in root.
Definition: json_util.c:100
#define ASSERT_NOT_NULL(argument)
Definition: definitions.h:129
#define MCL_FALSE
MCL bool type.
Definition: mcl_common.h:53
E_MCL_ERROR_CODE mcl_json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
This function gives the value of json_child object, when the child_name in json_parent object is give...
Definition: json_util.c:549
E_MCL_ERROR_CODE json_util_add_float(json_t *root, const char *object_name, const float number)
This function adds floating number to root.
Definition: json_util.c:329
E_MCL_ERROR_CODE mcl_json_util_get_array_item(mcl_json_t *array, int index, mcl_json_t **item)
This function gets the item at given index from array.
Definition: json_util.c:126
Log utility module header file.
String utility module implementation file.
E_MCL_ERROR_CODE mcl_json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
This function adds string to root which can be object or array.
Definition: json_util.c:222
E_MCL_ERROR_CODE mcl_json_util_get_number_value(mcl_json_t *json, mcl_int32_t *number_value)
This function gets the number value of a given json object.
Definition: json_util.c:616
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
E_MCL_ERROR_CODE mcl_json_util_get_string(mcl_json_t *json_item, char **string_value)
This function gets the string value of a given json object.
Definition: json_util.c:689
void json_util_destroy(json_t **root)
This function destroys root.
Definition: json_util.c:863
void * memory_malloc(mcl_size_t size)
malloc wrapper
Definition: memory.c:99
#define MCL_FREE(p)
Definition: memory.h:125
void json_util_get_number_value(json_t *json, mcl_int32_t *number_value)
This function gets the number value of a given json object.
Definition: json_util.c:629
void mcl_json_util_finish_object(mcl_json_t **json_object)
This function destroys json_object data struct. But the object still exists in root json object...
Definition: json_util.c:836
E_MCL_ERROR_CODE mcl_json_util_add_item_to_array(mcl_json_t *root, mcl_json_t *object)
This function adds object to root array.
Definition: json_util.c:527
#define VERBOSE_ENTRY(...)
Definition: log_util.h:65
Json array.
Definition: json_util.h:34
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
Definition: definitions.h:121
void json_util_get_double_value(json_t *json, double *double_value)
This function gets the double value of a given json object.
Definition: json_util.c:651
E_MCL_JSON_TYPE
Json type definitions.
Definition: mcl_json_util.h:34
E_MCL_ERROR_CODE mcl_json_util_to_string(mcl_json_t *root, char **json_string)
This function gives the string of root in json format.
Definition: json_util.c:722
General invalid parameter fail.
Definition: mcl_common.h:144
void mcl_json_util_finish_array(mcl_json_t **json_array)
This function destroys json_array data struct. But the array still exists in root json object...
Definition: json_util.c:818
void json_util_get_array_size(json_t *array, mcl_size_t *size)
This function returns the size of array.
Definition: json_util.c:173
void json_util_finish_array(json_t **json_array)
This function destroys json_array data struct. But the array still exists in root json object...
Definition: json_util.c:827
E_MCL_ERROR_CODE json_util_get_object_item(json_t *json_parent, const char *child_name, json_t **json_child)
This function gives the value of json_child object, when the child_name in json_parent object is give...
Definition: json_util.c:565
void json_util_initialize_json_library()
This function initializes json library.
Definition: json_util.c:28
static E_JSON_TYPE _convert_mcl_json_type_to_json_type(E_MCL_JSON_TYPE mcl_json_type)
Definition: json_util.c:888
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: definitions.h:105
Json null.
Definition: json_util.h:36
Definitions module header file.
void memory_free(void *p)
free wrapper
Definition: memory.c:129
Json utility module interface header file.
E_MCL_ERROR_CODE json_util_add_string(json_t *root, const char *object_name, const char *object_value)
This function adds string to root which can be object or array.
Definition: json_util.c:237
E_MCL_ERROR_CODE json_util_add_object(json_t *root, const char *object_name, json_t *object)
This function adds object to root.
Definition: json_util.c:504
E_MCL_ERROR_CODE mcl_json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
This function adds integer number to root which can be object or array.
Definition: json_util.c:271
struct cJSON cJSON
Definition: json_util.h:22
E_MCL_ERROR_CODE json_util_add_null(json_t *root, const char *object_name)
This function adds null to root which can be object or array.
Definition: json_util.c:454
void json_util_finish_object(json_t **json_object)
This function destroys json_object data struct. But the object still exists in root json object...
Definition: json_util.c:845
E_MCL_ERROR_CODE json_util_parse_with_size(const char *json_string, mcl_size_t size, json_t **root)
This function parses the given string to the given json object.
Definition: json_util.c:777
E_MCL_ERROR_CODE mcl_json_util_parse(const char *json_string, mcl_json_t **root)
This function parses the given string to the given json object.
Definition: json_util.c:748
size_t mcl_size_t
Definition: mcl_common.h:38
E_MCL_ERROR_CODE json_util_duplicate(const json_t *source_json, mcl_bool_t with_children, json_t **duplicated_json)
This function duplicates source_json as duplicated_json.
Definition: json_util.c:805
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
#define MCL_VERBOSE(...)
Definition: log_util.h:57
Json child which we try to get doesn&#39;t exist in the parent Json object.
Definition: mcl_common.h:208
Success.
Definition: mcl_common.h:140
E_MCL_ERROR_CODE mcl_json_util_add_null(mcl_json_t *root, const char *object_name)
This function adds null to root which can be object or array.
Definition: json_util.c:440
E_MCL_ERROR_CODE mcl_json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
This function creates an array in root.
Definition: json_util.c:84
The same name can not be added in the same level of json object.
Definition: mcl_common.h:150
E_MCL_ERROR_CODE json_util_to_string(json_t *root, char **json_string)
This function gives the string of root in json format.
Definition: json_util.c:737
static cJSON_Hooks cjson_hooks
Definition: json_util.c:26
E_MCL_ERROR_CODE mcl_json_util_initialize(E_MCL_JSON_TYPE json_type, mcl_json_t **root)
This function initializes the given root json.
Definition: json_util.c:39
Json object.
Definition: json_util.h:35
void json_util_get_bool_value(json_t *json, mcl_bool_t *bool_value)
This function gets the boolean value of a given json object.
Definition: json_util.c:673
E_MCL_ERROR_CODE mcl_json_util_get_bool_value(mcl_json_t *json, mcl_bool_t *bool_value)
This function gets the boolean value of a given json object.
Definition: json_util.c:660
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
void json_util_add_item_to_array(json_t *root, json_t *object)
This function adds object to root array.
Definition: json_util.c:540
E_MCL_ERROR_CODE mcl_json_util_add_float(mcl_json_t *root, const char *object_name, const float number)
This function adds floating number to root.
Definition: json_util.c:319
E_MCL_ERROR_CODE json_util_get_array_item(json_t *array, int index, json_t **item)
This function gets the item at given index from array.
Definition: json_util.c:141
E_MCL_ERROR_CODE mcl_json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
This function adds double number to root which can be object or array.
Definition: json_util.c:344
void mcl_json_util_destroy(mcl_json_t **root)
This function destroys root.
Definition: json_util.c:854
E_MCL_ERROR_CODE json_util_start_object(json_t *root, const char *object_name, json_t **json_object)
This function creates an object in root.
Definition: json_util.c:198
int32_t mcl_int32_t
Definition: mcl_common.h:41
E_MCL_ERROR_CODE json_util_initialize(E_JSON_TYPE json_type, json_t **root)
This function initializes the given root json.
Definition: json_util.c:53
static void _finish_json_item(json_t **json_item)
Definition: json_util.c:876
This struct is used for json handling.
Definition: json_util.h:27
E_MCL_ERROR_CODE mcl_json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
This function adds bool_value to root which can be object or array.
Definition: json_util.c:392
E_MCL_ERROR_CODE json_util_add_double(json_t *root, const char *object_name, const double number)
This function adds double number to root which can be object or array.
Definition: json_util.c:358
E_JSON_TYPE
Definition: json_util.h:32