json_util.c
Go to the documentation of this file.
1 
9 #include "cJSON/cJSON.h"
10 #include "mcl_core/mcl_json_util.h"
11 #include "json_util.h"
12 #include "string_util.h"
13 #include "mcl_core/mcl_assert.h"
14 #include "mcl_core/mcl_memory.h"
15 
16 static cJSON_Hooks cjson_hooks;
17 
18 // Integers and doubles are both treated as numbers in cJSON.
19 static mcl_error_t _add_number(mcl_json_t *root, const char *object_name, const double number);
20 
22 {
23  MCL_DEBUG_ENTRY("void");
24 
26 
27  MCL_DEBUG_LEAVE("retVal = void");
28 }
29 
31 {
32  MCL_DEBUG_ENTRY("void");
33 
34  cjson_hooks.malloc_fn = mcl_memory_malloc;
35  cjson_hooks.free_fn = mcl_memory_free;
36  cJSON_InitHooks(&cjson_hooks);
37 
38  MCL_DEBUG_LEAVE("retVal = void");
39 }
40 
42 {
43  mcl_error_t code;
44 
45  MCL_DEBUG_ENTRY("E_MCL_JSON_TYPE mcl_json_type = <%d>, mcl_json_t **root = <%p>", mcl_json_type, root);
46 
47  // Null check.
48  MCL_ASSERT_NOT_NULL(root, code);
49 
50  code = json_util_initialize(mcl_json_type, root);
51 
53  MCL_DEBUG_LEAVE("retVal = <%d>", code);
54  return code;
55 }
56 
58 {
59  mcl_error_t code = MCL_OK;
60 
61  MCL_VERBOSE_ENTRY("E_MCL_JSON_TYPE mcl_json_type = <%p>, mcl_json_t **root = <%p>", mcl_json_type, root);
62 
63  (*root) = MCL_NULL;
64 
65  switch (mcl_json_type)
66  {
67  case MCL_JSON_ARRAY:
68  *root = cJSON_CreateArray();
69  break;
70 
71  case MCL_JSON_OBJECT :
72  *root = cJSON_CreateObject();
73  break;
74 
75  default :
76  MCL_VERBOSE("mcl_json_type = <%d> is out of valid types.", mcl_json_type);
77  break;
78  }
79 
80  if (MCL_NULL == *root)
81  {
82  code = MCL_OUT_OF_MEMORY;
83  }
84 
85  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
86  return code;
87 }
88 
89 mcl_error_t mcl_json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
90 {
91  mcl_error_t code;
92 
93  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *array_name = <%p>, mcl_json_t **json_array = <%p>", root, array_name, json_array);
94 
95  // Null check.
96  MCL_ASSERT_NOT_NULL(root, code);
97  MCL_ASSERT_NOT_NULL(array_name, code);
98  MCL_ASSERT_NOT_NULL(json_array, code);
99 
100  code = json_util_start_array(root, array_name, json_array);
101 
103  MCL_DEBUG_LEAVE("retVal = <%d>", code);
104  return code;
105 }
106 
107 mcl_error_t json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
108 {
109  mcl_json_t *json_child = MCL_NULL;
110  mcl_error_t code;
111 
112  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *array_name = <%p>, mcl_json_t **json_array = <%p>", root, array_name, json_array);
113 
114  // Check whether object already exists or not.
115  code = json_util_get_object_item(root, array_name, &json_child);
116 
117  if (MCL_JSON_NON_EXISTING_CHILD == code)
118  {
119  // Initialize array.
120  code = json_util_initialize(MCL_JSON_ARRAY, json_array);
121 
122  if (MCL_OK == code)
123  {
124  cJSON_AddItemToObject((cJSON *) root, array_name, *json_array);
125  }
126  }
127  else
128  {
130  }
131 
132  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
133  return code;
134 }
135 
137 {
138  mcl_error_t code;
139 
140  MCL_DEBUG_ENTRY("mcl_json_t *array = <%p>, int index = <%d>, mcl_json_t **item = <%p>", array, index, item);
141 
142  // Null check.
143  MCL_ASSERT_NOT_NULL(array, code);
144  MCL_ASSERT_NOT_NULL(item, code);
145 
146  code = json_util_get_array_item(array, index, item);
147 
149  MCL_DEBUG_LEAVE("retVal = <%d>", code);
150  return code;
151 }
152 
154 {
156 
157  MCL_VERBOSE_ENTRY("mcl_json_t *array = <%p>, int index = <%d>, mcl_json_t **item = <%p>", array, index, item);
158 
159  if (cJSON_Array == ((cJSON *) array)->type)
160  {
161  (*item) = cJSON_GetArrayItem((cJSON *) array, index);
162  if (MCL_NULL != *item)
163  {
164  code = MCL_OK;
165  }
166  else
167  {
168  code = MCL_INVALID_PARAMETER;
169  }
170  }
171 
172  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
173  return code;
174 }
175 
177 {
178  mcl_error_t code = MCL_OK;
179 
180  MCL_DEBUG_ENTRY("mcl_json_t *array = <%p>, mcl_size_t *size = <%p>", array, size);
181 
182  MCL_ASSERT_NOT_NULL(array, code);
183  MCL_ASSERT_NOT_NULL(size, code);
184 
185  code = json_util_get_array_size(array, size);
186 
188  MCL_DEBUG_LEAVE("retVal = <%d>", code);
189  return code;
190 }
191 
193 {
195 
196  MCL_VERBOSE_ENTRY("mcl_json_t *array = <%p>, mcl_size_t *size = <%p>", array, size);
197 
198  if (cJSON_Array == ((cJSON *) array)->type)
199  {
200  *size = (mcl_size_t) cJSON_GetArraySize((cJSON *) array);
201  code = MCL_OK;
202  }
203 
204  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
205  return code;
206 }
207 
208 mcl_error_t mcl_json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
209 {
210  mcl_error_t code;
211 
212  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t **json_object = <%p>", root, object_name, json_object);
213 
214  // Null check.
215  MCL_ASSERT_NOT_NULL(root, code);
216  MCL_ASSERT_NOT_NULL(object_name, code);
217  MCL_ASSERT_NOT_NULL(json_object, code);
218 
219  code = json_util_start_object(root, object_name, json_object);
220 
222  MCL_DEBUG_LEAVE("retVal = <%d>", code);
223  return code;
224 }
225 
226 mcl_error_t json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
227 {
228  // Check whether object already exists or not.
229  mcl_json_t *json_child = MCL_NULL;
230  mcl_error_t code;
231 
232  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t **json_object = <%p>", root, object_name, json_object);
233 
234  code = json_util_get_object_item(root, object_name, &json_child);
235 
236  if (MCL_JSON_NON_EXISTING_CHILD != code)
237  {
239  }
240  else
241  {
242  // Initialize object.
243  code = json_util_initialize(MCL_JSON_OBJECT, json_object);
244 
245  if (MCL_OK == code)
246  {
247  // Add object.
248  cJSON_AddItemToObject((cJSON *) root, object_name, *json_object);
249  }
250  }
251 
252  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
253  return code;
254 }
255 
256 mcl_error_t mcl_json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
257 {
258  mcl_error_t code;
259 
260  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const char *object_value = <%p>", root, object_name, object_value);
261 
262  // Null check.
263  MCL_ASSERT_NOT_NULL(root, code);
264  MCL_ASSERT_NOT_NULL(object_value, code);
265 
266  code = json_util_add_string(root, object_name, object_value);
267 
269  MCL_DEBUG_LEAVE("retVal = <%d>", code);
270  return code;
271 }
272 
273 mcl_error_t json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
274 {
275  mcl_error_t code = MCL_OK;
276 
277  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const char *object_value = <%p>", root, object_name, object_value);
278 
279  // Check type of root.
280  if ((cJSON_Array == ((cJSON *) root)->type) && (MCL_NULL == object_name))
281  {
282  // Add string to array.
283  cJSON *string = cJSON_CreateString(object_value);
284 
285  if (MCL_NULL == string)
286  {
287  code = MCL_OUT_OF_MEMORY;
288  }
289  else
290  {
291  cJSON_AddItemToArray((cJSON *) root, string);
292  }
293  }
294  else if ((cJSON_Object == ((cJSON *) root)->type) && (MCL_NULL != object_name))
295  {
296  // Get object to add string.
297  mcl_json_t *json_child = MCL_NULL;
298  code = json_util_get_object_item(root, object_name, &json_child);
299 
300  if (MCL_JSON_NON_EXISTING_CHILD == code)
301  {
302  // Add string to object.
303  cJSON_AddStringToObject((cJSON *) root, object_name, object_value);
304  code = MCL_OK;
305  }
306  else
307  {
309  }
310  }
311  else
312  {
313  code = MCL_INVALID_PARAMETER;
314  }
315 
316  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
317  return code;
318 }
319 
320 mcl_error_t mcl_json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
321 {
322  mcl_error_t code;
323 
324  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const mcl_size_t number = <%u>", root, object_name, &number);
325 
326  // Null check.
327  MCL_ASSERT_NOT_NULL(root, code);
328 
329  code = json_util_add_uint(root, object_name, number);
330 
332  MCL_DEBUG_LEAVE("retVal = <%d>", code);
333  return code;
334 }
335 
336 mcl_error_t json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
337 {
338  mcl_error_t code;
339 
340  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, char *object_name = <%p>, mcl_size_t number = <%u>", root, object_name, number);
341 
342  code = _add_number(root, object_name, (double) number);
343 
344  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
345  return code;
346 }
347 
348 mcl_error_t mcl_json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
349 {
350  mcl_error_t code;
351 
352  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const double number = <%f>", root, object_name, &number);
353 
354  // Null check.
355  MCL_ASSERT_NOT_NULL(root, code);
356 
357  code = json_util_add_double(root, object_name, number);
358 
360  MCL_DEBUG_LEAVE("retVal = <%d>", code);
361  return code;
362 }
363 
364 mcl_error_t json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
365 {
366  mcl_error_t code;
367 
368  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const double number = <%f>", root, object_name, number);
369 
370  code = _add_number(root, object_name, number);
371 
372  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
373  return code;
374 }
375 
376 mcl_error_t mcl_json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
377 {
378  mcl_error_t code;
379 
380  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const mcl_bool_t bool_value = <%d>", root, object_name, &bool_value);
381 
382  // Null check.
383  MCL_ASSERT_NOT_NULL(root, code);
384 
385  code = json_util_add_bool(root, object_name, bool_value);
386 
388  MCL_DEBUG_LEAVE("retVal = <%d>", code);
389  return code;
390 }
391 
392 mcl_error_t json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
393 {
394  mcl_error_t code = MCL_OK;
395 
396  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, const mcl_bool_t bool_value = <%d>", root, object_name, bool_value);
397 
398  // Check type of root.
399  if ((cJSON_Array == ((cJSON *) root)->type) && (MCL_NULL == object_name))
400  {
401  // Add bool to array.
402  cJSON *json_bool = cJSON_CreateBool(bool_value);
403 
404  if (MCL_NULL == json_bool)
405  {
406  code = MCL_OUT_OF_MEMORY;
407  }
408  else
409  {
410  cJSON_AddItemToArray((cJSON *) root, json_bool);
411  }
412  }
413  else if ((cJSON_Object == ((cJSON *) root)->type) && (MCL_NULL != object_name))
414  {
415  // Get object to add bool.
416  mcl_json_t *json_child = MCL_NULL;
417  code = json_util_get_object_item(root, object_name, &json_child);
418 
419  if (MCL_JSON_NON_EXISTING_CHILD == code)
420  {
421  // Add uint to object. This function expects a double and type cast to integer inside the function.
422  cJSON_AddBoolToObject((cJSON *) root, object_name, bool_value);
423  code = MCL_OK;
424  }
425  else
426  {
428  }
429  }
430  else
431  {
432  code = MCL_INVALID_PARAMETER;
433  }
434 
435  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
436  return code;
437 }
438 
439 mcl_error_t mcl_json_util_add_null(mcl_json_t *root, const char *object_name)
440 {
441  mcl_error_t code;
442 
443  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>", root, object_name);
444 
445  // Null check.
446  MCL_ASSERT_NOT_NULL(root, code);
447 
448  code = json_util_add_null(root, object_name);
449 
451  MCL_DEBUG_LEAVE("retVal = <%d>", code);
452  return code;
453 }
454 
455 mcl_error_t json_util_add_null(mcl_json_t *root, const char *object_name)
456 {
457  mcl_error_t code = MCL_OK;
458 
459  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>", root, object_name);
460 
461  // Check type of root.
462  if ((cJSON_Array == ((cJSON *) root)->type) && (MCL_NULL == object_name))
463  {
464  // Add null to array.
465  cJSON *json_null = cJSON_CreateNull();
466  cJSON_AddItemToArray(root, json_null);
467  }
468  else if ((cJSON_Object == ((cJSON *) root)->type) && (MCL_NULL != object_name))
469  {
470  // Get object to add null.
471  mcl_json_t *json_child = MCL_NULL;
472  code = json_util_get_object_item(root, object_name, &json_child);
473 
474  if (MCL_JSON_NON_EXISTING_CHILD == code)
475  {
476  // Add null to object.
477  cJSON_AddNullToObject((cJSON *) root, object_name);
478  code = MCL_OK;
479  }
480  else
481  {
483  }
484  }
485  else
486  {
487  code = MCL_INVALID_PARAMETER;
488  }
489 
490  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
491  return code;
492 }
493 
494 mcl_error_t mcl_json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
495 {
496  mcl_error_t code;
497 
498  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t *object = <%p>", root, object_name, object);
499 
500  // Null check.
501  MCL_ASSERT_NOT_NULL(root, code);
502  MCL_ASSERT_NOT_NULL(object_name, code);
503  MCL_ASSERT_NOT_NULL(object, code);
504 
505  code = json_util_add_object(root, object_name, object);
506 
508  MCL_DEBUG_LEAVE("retVal = <%d>", code);
509  return code;
510 }
511 
512 mcl_error_t json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
513 {
514  mcl_json_t *json_child = MCL_NULL;
515  mcl_error_t code;
516 
517  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, const char *object_name = <%p>, mcl_json_t *object = <%p>", root, object_name, object);
518 
519  // Check whether object already exists or not.
520  code = json_util_get_object_item(root, object_name, &json_child);
521 
522  if (MCL_JSON_NON_EXISTING_CHILD == code)
523  {
524  // Add object.
525  cJSON_AddItemToObject((cJSON *) root, object_name, object);
526  code = MCL_OK;
527  }
528  else
529  {
531  }
532 
533  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
534  return code;
535 }
536 
538 {
539  mcl_error_t code = MCL_OK;
540 
541  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, mcl_json_t *object = <%p>", root, object);
542 
543  // Null check.
544  MCL_ASSERT_NOT_NULL(root, code);
545  MCL_ASSERT_NOT_NULL(object, code);
546 
547  json_util_add_item_to_array(root, object);
548 
550  MCL_DEBUG_LEAVE("retVal = <%d>", code);
551  return code;
552 }
553 
555 {
556  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, mcl_json_t *object = <%p>", root, object);
557 
558  cJSON_AddItemToArray((cJSON *) root, object);
559 
560  MCL_VERBOSE_LEAVE("retVal = void");
561 }
562 
563 mcl_error_t mcl_json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
564 {
565  mcl_error_t code;
566 
567  MCL_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);
568 
569  // Null check.
570  MCL_ASSERT_NOT_NULL(json_parent, code);
571  MCL_ASSERT_NOT_NULL(child_name, code);
572  MCL_ASSERT_NOT_NULL(json_child, code);
573 
574  code = json_util_get_object_item(json_parent, child_name, json_child);
575 
577  MCL_DEBUG_LEAVE("retVal = <%d>", code);
578  return code;
579 }
580 
581 mcl_error_t json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
582 {
583  mcl_error_t return_code = MCL_OK;
584 
585  MCL_VERBOSE_ENTRY("mcl_json_t *json_parent = <%p>, const char *child_name = <%p>, mcl_json_t **json_child = <%p>", json_parent, child_name, json_child);
586 
587  // Returns the address of object item to root handle w/o allocation.
588  (*json_child) = cJSON_GetObjectItem((cJSON *) json_parent, child_name);
589 
590  // Check if child object doesn't exist.
591  if (MCL_NULL == (*json_child))
592  {
593  return_code = MCL_JSON_NON_EXISTING_CHILD;
594  }
595 
596  MCL_VERBOSE_LEAVE("retVal = <%d>", return_code);
597  return return_code;
598 }
599 
601 {
602  mcl_error_t code = MCL_OK;
603 
604  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, mcl_bool_t *result = <%p>", root, result);
605 
606  // Null check.
607  MCL_ASSERT_NOT_NULL(root, code);
608  MCL_ASSERT_NOT_NULL(result, code);
609 
610  *result = json_util_has_child((cJSON *) root);
611 
613  MCL_DEBUG_LEAVE("retVal = <%d>", code);
614  return code;
615 }
616 
618 {
619  mcl_bool_t return_value = MCL_FALSE;
620 
621  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>", root);
622 
623  // If root has child make result true.
624  if (MCL_NULL != ((cJSON *) root)->child)
625  {
626  return_value = MCL_TRUE;
627  }
628 
629  MCL_VERBOSE_LEAVE("retVal = <%d>", return_value);
630  return return_value;
631 }
632 
634 {
635  mcl_error_t code = MCL_OK;
636 
637  MCL_DEBUG_ENTRY("mcl_json_t *json = <%p>, mcl_int32_t *number_value = <%p>", json, number_value);
638 
639  // Null check.
640  MCL_ASSERT_NOT_NULL(json, code);
641  MCL_ASSERT_NOT_NULL(number_value, code);
642 
643  code = json_util_get_number_value(json, number_value);
644 
646  MCL_DEBUG_LEAVE("retVal = <%d>", code);
647  return code;
648 }
649 
651 {
653 
654  MCL_VERBOSE_ENTRY("mcl_json_t *json = <%p>, mcl_int32_t *number_value = <%p>", json, number_value);
655 
656  if (cJSON_Number == ((cJSON *) json)->type)
657  {
658  *number_value = ((cJSON *) json)->valueint;
659  code = MCL_OK;
660  }
661 
662  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
663  return code;
664 }
665 
667 {
668  mcl_error_t code = MCL_OK;
669 
670  MCL_DEBUG_ENTRY("mcl_json_t *json = <%p>, double *double_value = <%p>", json, double_value);
671 
672  // Null check.
673  MCL_ASSERT_NOT_NULL(json, code);
674  MCL_ASSERT_NOT_NULL(double_value, code);
675 
676  code = json_util_get_double_value(json, double_value);
677 
679  MCL_DEBUG_LEAVE("retVal = <%d>", code);
680  return code;
681 }
682 
684 {
686 
687  MCL_VERBOSE_ENTRY("mcl_json_t *json = <%p>, double *double_value = <%p>", json, double_value);
688 
689  if (cJSON_Number == ((cJSON *) json)->type)
690  {
691  *double_value = ((cJSON *) json)->valuedouble;
692  code = MCL_OK;
693  }
694 
695  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
696  return code;
697 }
698 
700 {
701  mcl_error_t code = MCL_OK;
702 
703  MCL_DEBUG_ENTRY("mcl_json_t *json = <%p>, mcl_bool_t *bool_value = <%p>", json, bool_value);
704 
705  // Null check.
706  MCL_ASSERT_NOT_NULL(json, code);
707  MCL_ASSERT_NOT_NULL(bool_value, code);
708 
709  code = json_util_get_bool_value(json, bool_value);
710 
712  MCL_DEBUG_LEAVE("retVal = <%d>", code);
713  return code;
714 }
715 
717 {
719 
720  MCL_VERBOSE_ENTRY("mcl_json_t *json = <%p>, mcl_bool_t *bool_value = <%p>", json, bool_value);
721 
722  if (cJSON_True == ((cJSON *) json)->type)
723  {
724  *bool_value = MCL_TRUE;
725  code = MCL_OK;
726  }
727  else if (cJSON_False == ((cJSON *) json)->type)
728  {
729  *bool_value = MCL_FALSE;
730  code = MCL_OK;
731  }
732 
733  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
734  return code;
735 }
736 
738 {
739  mcl_error_t code;
740 
741  MCL_DEBUG_ENTRY("mcl_json_t *json = <%p>, char **string_value = <%p>", json, string_value);
742 
743  // Null check.
744  MCL_ASSERT_NOT_NULL(json, code);
745  MCL_ASSERT_NOT_NULL(string_value, code);
746 
747  code = json_util_get_string(json, string_value);
748 
750  MCL_DEBUG_LEAVE("retVal = <%d>", code);
751  return code;
752 }
753 
754 mcl_error_t json_util_get_string(mcl_json_t *json, char **string_value)
755 {
756  mcl_size_t string_length;
757  mcl_error_t code;
758 
759  MCL_VERBOSE_ENTRY("mcl_json_t *json = <%p>, char **string_value = <%p>", json, string_value);
760 
761  if (cJSON_String == ((cJSON *) json)->type)
762  {
763  // Calculate string length.
764  string_length = string_util_strlen(((cJSON *) json)->valuestring);
765 
766  *string_value = MCL_MALLOC(string_length + MCL_NULL_CHAR_SIZE);
767 
768  if(MCL_NULL == *string_value)
769  {
770  code = MCL_OUT_OF_MEMORY;
771  }
772  else
773  {
774  string_util_memcpy(*string_value, ((cJSON *) json)->valuestring, string_length + MCL_NULL_CHAR_SIZE);
775  code = MCL_OK;
776  }
777  }
778  else if (cJSON_NULL == ((cJSON *) json)->type)
779  {
780  *string_value = MCL_NULL;
781  code = MCL_OK;
782  }
783  else
784  {
785  code = MCL_JSON_TYPE_MISMATCH;
786  }
787 
788  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
789  return code;
790 }
791 
793 {
794  mcl_error_t code;
795 
796  MCL_DEBUG_ENTRY("mcl_json_t *root = <%p>, char **json_string = <%p>", root, json_string);
797 
798  // Null check.
799  MCL_ASSERT_NOT_NULL(root, code);
800  MCL_ASSERT_NOT_NULL(json_string, code);
801 
802  code = json_util_to_string(root, json_string);
803 
805  MCL_DEBUG_LEAVE("retVal = <%d>", code);
806  return code;
807 }
808 
809 mcl_error_t json_util_to_string(mcl_json_t *root, char **json_string)
810 {
811  mcl_error_t code = MCL_OK;
812 
813  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, char **json_string = <%p>", root, json_string);
814 
815  *json_string = cJSON_PrintUnformatted((cJSON *) root);
816 
817  if (MCL_NULL == *json_string)
818  {
819  code = MCL_FAIL;
820  MCL_ERROR("Either the given json object is invalid or memory can not be allocated for root.");
821  }
822 
823  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
824  return code;
825 }
826 
827 mcl_error_t mcl_json_util_parse(const char *json_string, mcl_size_t buffer_size, mcl_json_t **root)
828 {
829  mcl_error_t code;
830 
831  MCL_DEBUG_ENTRY("const char *json_string = <%p>, mcl_size_t buffer_size = <%lu>, mcl_json_t **root = <%p>", json_string, buffer_size, root);
832 
833  // Null check.
834  MCL_ASSERT_NOT_NULL(json_string, code);
835  MCL_ASSERT_NOT_NULL(root, code);
836 
837  code = json_util_parse(json_string, buffer_size, root);
838 
840  MCL_DEBUG_LEAVE("retVal = <%d>", code);
841  return code;
842 }
843 
844 mcl_error_t json_util_parse(const char *json_string, mcl_size_t size, mcl_json_t **root)
845 {
846  mcl_error_t code = MCL_OK;
847  *root = MCL_NULL;
848 
849  MCL_VERBOSE_ENTRY("const char *json_string = <%p>, mcl_size_t size = <%lu>, mcl_json_t **root = <%p>", json_string, size, root);
850 
851  if (0 == size)
852  {
853  (*root) = cJSON_Parse(json_string);
854  }
855  else
856  {
857  (*root) = cJSON_ParseWithSize(json_string, size);
858  }
859 
860  if (MCL_NULL == (*root))
861  {
862  code = MCL_FAIL;
863  }
864 
865  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
866  return code;
867 }
868 
869 mcl_error_t mcl_json_util_duplicate(const mcl_json_t *source_json, mcl_json_t **duplicated_json)
870 {
871  mcl_error_t code;
872 
873  MCL_DEBUG_ENTRY("const mcl_json_t *source_json = <%p>, mcl_json_t **duplicated_json = <%p>", source_json, duplicated_json);
874 
875  MCL_ASSERT_NOT_NULL(duplicated_json, code);
876  *duplicated_json = MCL_NULL;
877 
878  MCL_ASSERT_NOT_NULL(source_json, code);
879 
880  code = json_util_duplicate(source_json, duplicated_json);
881 
883  MCL_DEBUG_LEAVE("retVal = <%d>", code);
884  return code;
885 }
886 
887 mcl_error_t json_util_duplicate(const mcl_json_t *source_json, mcl_json_t **duplicated_json)
888 {
889  mcl_error_t code = MCL_OK;
890 
891  MCL_VERBOSE_ENTRY("const mcl_json_t *source_json = <%p>, mcl_json_t **duplicated_json = <%p>", source_json, duplicated_json);
892 
893  (*duplicated_json) = cJSON_Duplicate((const cJSON *) source_json, MCL_TRUE);
894 
895  if (MCL_NULL == *duplicated_json)
896  {
897  code = MCL_OUT_OF_MEMORY;
898  }
899 
900  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
901  return code;
902 }
903 
905 {
906  MCL_DEBUG_ENTRY("mcl_json_t **root = <%p>", root);
907 
908  json_util_destroy(root);
909 
910  MCL_DEBUG_LEAVE("retVal = void");
911 }
912 
914 {
915  MCL_VERBOSE_ENTRY("mcl_json_t **root = <%p>", root);
916 
917  if (MCL_NULL != *root)
918  {
919  cJSON_Delete((cJSON *) *root);
920  *root = MCL_NULL;
921  }
922 
923  MCL_VERBOSE_LEAVE("retVal = void");
924 }
925 
926 static mcl_error_t _add_number(mcl_json_t *root, const char *object_name, const double number)
927 {
928  mcl_error_t code = MCL_OK;
929 
930  MCL_VERBOSE_ENTRY("mcl_json_t *root = <%p>, char *object_name = <%p>, mcl_size_t number = <%u>", root, object_name, number);
931 
932  // Check type of root.
933  if ((cJSON_Array == ((cJSON *) root)->type) && (MCL_NULL == object_name))
934  {
935  cJSON *json_number = cJSON_CreateNumber(number);
936 
937  if (MCL_NULL == json_number)
938  {
939  code = MCL_OUT_OF_MEMORY;
940  }
941  else
942  {
943  cJSON_AddItemToArray((cJSON *) root, json_number);
944  }
945  }
946  else if ((cJSON_Object == ((cJSON *) root)->type) && (MCL_NULL != object_name))
947  {
948  mcl_json_t *json_child = MCL_NULL;
949  code = json_util_get_object_item(root, object_name, &json_child);
950 
951  if (MCL_JSON_NON_EXISTING_CHILD == code)
952  {
953  cJSON_AddNumberToObject((cJSON *) root, object_name, number);
954  code = MCL_OK;
955  }
956  else
957  {
959  }
960  }
961  else
962  {
963  code = MCL_INVALID_PARAMETER;
964  }
965 
966  MCL_VERBOSE_LEAVE("retVal = <%d>", code);
967  return code;
968 }
mcl_error_t mcl_json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
Definition: json_util.c:376
mcl_error_t json_util_get_string(mcl_json_t *json, char **string_value)
Definition: json_util.c:754
#define MCL_FUNCTION_LEAVE_LABEL
size_t mcl_size_t
int32_t mcl_int32_t
void json_util_add_item_to_array(mcl_json_t *root, mcl_json_t *object)
Definition: json_util.c:554
Assert module header file.
Success.
Json utility module header file.
void mcl_json_t
Definition: mcl_json_util.h:24
Json object.
Definition: mcl_json_util.h:32
mcl_int32_t mcl_error_t
mcl_error_t mcl_json_util_has_child(mcl_json_t *root, mcl_bool_t *result)
Definition: json_util.c:600
mcl_error_t mcl_json_util_add_item_to_array(mcl_json_t *root, mcl_json_t *object)
Definition: json_util.c:537
Json array.
Definition: mcl_json_util.h:31
#define MCL_DEBUG_ENTRY(...)
Definition: mcl_log_util.h:115
#define MCL_VERBOSE(...)
Definition: mcl_log_util.h:102
mcl_error_t mcl_json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
Definition: json_util.c:494
mcl_error_t mcl_json_util_parse(const char *json_string, mcl_size_t buffer_size, mcl_json_t **root)
Definition: json_util.c:827
#define MCL_FALSE
mcl_error_t mcl_json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
Definition: json_util.c:320
mcl_error_t json_util_parse(const char *json_string, mcl_size_t size, mcl_json_t **root)
Definition: json_util.c:844
mcl_error_t json_util_get_number_value(mcl_json_t *json, mcl_int32_t *number_value)
Definition: json_util.c:650
String utility module header file.
mcl_error_t json_util_to_string(mcl_json_t *root, char **json_string)
Definition: json_util.c:809
mcl_error_t json_util_get_double_value(mcl_json_t *json, double *double_value)
Definition: json_util.c:683
mcl_error_t json_util_add_object(mcl_json_t *root, const char *object_name, mcl_json_t *object)
Definition: json_util.c:512
#define MCL_NULL
void json_util_destroy(mcl_json_t **root)
Definition: json_util.c:913
#define MCL_ERROR(...)
Definition: mcl_log_util.h:142
#define MCL_VERBOSE_LEAVE(...)
Definition: mcl_log_util.h:104
mcl_error_t json_util_add_uint(mcl_json_t *root, const char *object_name, const mcl_size_t number)
Definition: json_util.c:336
mcl_error_t mcl_json_util_get_string(mcl_json_t *json, char **string_value)
Definition: json_util.c:737
static mcl_error_t _add_number(mcl_json_t *root, const char *object_name, const double number)
Definition: json_util.c:926
mcl_bool_t json_util_has_child(mcl_json_t *root)
Definition: json_util.c:617
E_MCL_JSON_TYPE
Definition: mcl_json_util.h:29
mcl_error_t json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
Definition: json_util.c:226
mcl_error_t mcl_json_util_get_array_size(mcl_json_t *array, mcl_size_t *size)
Definition: json_util.c:176
void string_util_memcpy(void *destination, const void *source, mcl_size_t count)
Definition: string_util.c:229
MCL_CORE_EXPORT void mcl_memory_free(void *p)
Definition: memory.c:45
mcl_error_t mcl_json_util_add_null(mcl_json_t *root, const char *object_name)
Definition: json_util.c:439
mcl_error_t mcl_json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
Definition: json_util.c:89
Json utility module interface header file.
#define MCL_ASSERT_NOT_NULL(argument, return_variable)
Definition: mcl_assert.h:38
mcl_error_t json_util_start_array(mcl_json_t *root, const char *array_name, mcl_json_t **json_array)
Definition: json_util.c:107
mcl_error_t mcl_json_util_duplicate(const mcl_json_t *source_json, mcl_json_t **duplicated_json)
Definition: json_util.c:869
mcl_error_t json_util_add_bool(mcl_json_t *root, const char *object_name, const mcl_bool_t bool_value)
Definition: json_util.c:392
mcl_error_t mcl_json_util_get_array_item(mcl_json_t *array, int index, mcl_json_t **item)
Definition: json_util.c:136
mcl_error_t mcl_json_util_get_bool_value(mcl_json_t *json, mcl_bool_t *bool_value)
Definition: json_util.c:699
mcl_error_t json_util_get_array_item(mcl_json_t *array, int index, mcl_json_t **item)
Definition: json_util.c:153
mcl_error_t json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
Definition: json_util.c:581
void mcl_json_util_library_initialize(void)
Definition: json_util.c:21
mcl_error_t json_util_add_null(mcl_json_t *root, const char *object_name)
Definition: json_util.c:455
mcl_error_t mcl_json_util_initialize(E_MCL_JSON_TYPE mcl_json_type, mcl_json_t **root)
Definition: json_util.c:41
mcl_error_t mcl_json_util_to_string(mcl_json_t *root, char **json_string)
Definition: json_util.c:792
mcl_error_t mcl_json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
Definition: json_util.c:348
mcl_error_t mcl_json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
Definition: json_util.c:256
mcl_uint8_t mcl_bool_t
mcl_error_t json_util_get_array_size(mcl_json_t *array, mcl_size_t *size)
Definition: json_util.c:192
mcl_error_t mcl_json_util_get_object_item(mcl_json_t *json_parent, const char *child_name, mcl_json_t **json_child)
Definition: json_util.c:563
mcl_error_t json_util_duplicate(const mcl_json_t *source_json, mcl_json_t **duplicated_json)
Definition: json_util.c:887
mcl_error_t json_util_get_bool_value(mcl_json_t *json, mcl_bool_t *bool_value)
Definition: json_util.c:716
Memory allocation fail.
static cJSON_Hooks cjson_hooks
Definition: json_util.c:16
The same name can not be added in the same level of json object.
#define MCL_NULL_CHAR_SIZE
mcl_error_t json_util_add_string(mcl_json_t *root, const char *object_name, const char *object_value)
Definition: json_util.c:273
#define MCL_MALLOC(bytes)
Definition: mcl_memory.h:54
void json_util_initialize_json_library(void)
Definition: json_util.c:30
mcl_error_t json_util_initialize(E_MCL_JSON_TYPE mcl_json_type, mcl_json_t **root)
Definition: json_util.c:57
General invalid parameter fail.
#define MCL_DEBUG_LEAVE(...)
Definition: mcl_log_util.h:116
#define MCL_TRUE
MCL_CORE_EXPORT void * mcl_memory_malloc(mcl_size_t size)
Definition: memory.c:14
#define MCL_VERBOSE_ENTRY(...)
Definition: mcl_log_util.h:103
mcl_error_t mcl_json_util_start_object(mcl_json_t *root, const char *object_name, mcl_json_t **json_object)
Definition: json_util.c:208
Internal failure in MCL.
void mcl_json_util_destroy(mcl_json_t **root)
Definition: json_util.c:904
mcl_size_t string_util_strlen(const char *buffer)
Definition: string_util.c:35
Type of the value of the json object does not match the type requested.
Json child which we try to get doesn&#39;t exist in the parent Json object.
mcl_error_t json_util_add_double(mcl_json_t *root, const char *object_name, const double number)
Definition: json_util.c:364
mcl_error_t mcl_json_util_get_double_value(mcl_json_t *json, double *double_value)
Definition: json_util.c:666
mcl_error_t mcl_json_util_get_number_value(mcl_json_t *json, mcl_int32_t *number_value)
Definition: json_util.c:633
Memory module interface header file.