s2jdef.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * This file is part of the struct2json Library.
  3. *
  4. * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: It is an head file for this library.
  26. * Created on: 2015-10-14
  27. */
  28. #ifndef __S2JDEF_H__
  29. #define __S2JDEF_H__
  30. #include <cJSON.h>
  31. #include <string.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef struct {
  36. void *(*malloc_fn)(size_t sz);
  37. void (*free_fn)(void *ptr);
  38. } S2jHook, *S2jHook_t;
  39. #define S2J_STRUCT_GET_int_ELEMENT(to_struct, from_json, _element) \
  40. json_temp = cJSON_GetObjectItem(from_json, #_element); \
  41. if (json_temp) (to_struct)->_element = json_temp->valueint;
  42. #define S2J_STRUCT_GET_string_ELEMENT(to_struct, from_json, _element) \
  43. json_temp = cJSON_GetObjectItem(from_json, #_element); \
  44. if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring);
  45. #define S2J_STRUCT_GET_double_ELEMENT(to_struct, from_json, _element) \
  46. json_temp = cJSON_GetObjectItem(from_json, #_element); \
  47. if (json_temp) (to_struct)->_element = json_temp->valuedouble;
  48. #define S2J_STRUCT_ARRAY_GET_int_ELEMENT(to_struct, from_json, _element, index) \
  49. (to_struct)->_element[index] = from_json->valueint;
  50. #define S2J_STRUCT_ARRAY_GET_string_ELEMENT(to_struct, from_json, _element, index) \
  51. strcpy((to_struct)->_element[index], from_json->valuestring);
  52. #define S2J_STRUCT_ARRAY_GET_double_ELEMENT(to_struct, from_json, _element, index) \
  53. (to_struct)->_element[index] = from_json->valuedouble;
  54. #define S2J_STRUCT_ARRAY_GET_ELEMENT(to_struct, from_json, type, _element, index) \
  55. S2J_STRUCT_ARRAY_GET_##type##_ELEMENT(to_struct, from_json, _element, index)
  56. #define S2J_JSON_SET_int_ELEMENT(to_json, from_struct, _element) \
  57. cJSON_AddNumberToObject(to_json, #_element, (from_struct)->_element);
  58. #define S2J_JSON_SET_double_ELEMENT(to_json, from_struct, _element) \
  59. cJSON_AddNumberToObject(to_json, #_element, (from_struct)->_element);
  60. #define S2J_JSON_SET_string_ELEMENT(to_json, from_struct, _element) \
  61. cJSON_AddStringToObject(to_json, #_element, (from_struct)->_element);
  62. #define S2J_JSON_ARRAY_SET_int_ELEMENT(to_json, from_struct, _element, index) \
  63. cJSON_AddItemToArray(to_json, cJSON_CreateNumber((from_struct)->_element[index]));
  64. #define S2J_JSON_ARRAY_SET_double_ELEMENT(to_json, from_struct, _element, index) \
  65. cJSON_AddItemToArray(to_json, cJSON_CreateNumber((from_struct)->_element[index]));
  66. #define S2J_JSON_ARRAY_SET_string_ELEMENT(to_json, from_struct, _element, index) \
  67. cJSON_AddItemToArray(to_json, cJSON_CreateString((from_struct)->_element[index]));
  68. #define S2J_JSON_ARRAY_SET_ELEMENT(to_json, from_struct, type, _element, index) \
  69. S2J_JSON_ARRAY_SET_##type##_ELEMENT(to_json, from_struct, _element, index)
  70. #define S2J_CREATE_JSON_OBJECT(json_obj) \
  71. cJSON *json_obj = cJSON_CreateObject();
  72. #define S2J_DELETE_JSON_OBJECT(json_obj) \
  73. cJSON_Delete(json_obj);
  74. #define S2J_JSON_SET_BASIC_ELEMENT(to_json, from_struct, type, _element) \
  75. S2J_JSON_SET_##type##_ELEMENT(to_json, from_struct, _element)
  76. #define S2J_JSON_SET_ARRAY_ELEMENT(to_json, from_struct, type, _element, size) \
  77. { \
  78. cJSON *array; \
  79. size_t index = 0; \
  80. array = cJSON_CreateArray(); \
  81. if (array) { \
  82. while (index < size) { \
  83. S2J_JSON_ARRAY_SET_ELEMENT(array, from_struct, type, _element, index++); \
  84. } \
  85. cJSON_AddItemToObject(to_json, #_element, array); \
  86. } \
  87. }
  88. #define S2J_JSON_SET_STRUCT_ELEMENT(child_json, to_json, child_struct, from_struct, type, _element) \
  89. type *child_struct = &((from_struct)->_element); \
  90. cJSON *child_json = cJSON_CreateObject(); \
  91. if (child_json) cJSON_AddItemToObject(to_json, #_element, child_json);
  92. #define S2J_CREATE_STRUCT_OBJECT(struct_obj, type) \
  93. cJSON *json_temp; \
  94. type *struct_obj = s2jHook.malloc_fn(sizeof(type)); \
  95. if (struct_obj) memset(struct_obj, 0, sizeof(type));
  96. #define S2J_DELETE_STRUCT_OBJECT(struct_obj) \
  97. s2jHook.free_fn(struct_obj);
  98. #define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \
  99. S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element)
  100. #define S2J_STRUCT_GET_ARRAY_ELEMENT(to_struct, from_json, type, _element) \
  101. { \
  102. cJSON *array, *array_element; \
  103. size_t index = 0, size = 0; \
  104. array = cJSON_GetObjectItem(from_json, #_element); \
  105. if (array) { \
  106. size = cJSON_GetArraySize(array); \
  107. while (index < size) { \
  108. array_element = cJSON_GetArrayItem(array, index); \
  109. if (array_element) S2J_STRUCT_ARRAY_GET_ELEMENT(to_struct, array_element, type, _element, index++); \
  110. } \
  111. } \
  112. }
  113. #define S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, _element) \
  114. type *child_struct = &((to_struct)->_element); \
  115. cJSON *child_json = cJSON_GetObjectItem(from_json, #_element);
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* __S2JDEF_H__ */