From 7ca54d8e1893a448bcbaebf24c8b4373067af5ba Mon Sep 17 00:00:00 2001 From: "joki.zhu" Date: Sun, 23 Feb 2020 16:31:44 +0800 Subject: [PATCH] Add xxx_ex API to solove some problems 01. solve the segment error caused by the s2j_struct_get_basic_element's from_json being null. 02. add the default value option of the parameter. --- demo/main.c | 6 ++++ struct2json/inc/s2j.h | 7 +++- struct2json/inc/s2jdef.h | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) mode change 100644 => 100755 struct2json/inc/s2j.h mode change 100644 => 100755 struct2json/inc/s2jdef.h diff --git a/demo/main.c b/demo/main.c index a3904ad..63f8494 100644 --- a/demo/main.c +++ b/demo/main.c @@ -55,9 +55,15 @@ static void *json_to_struct(cJSON* json_obj) { /* deserialize data to Student structure object. */ s2j_struct_get_basic_element(struct_student, json_obj, int, id); +#if 1 s2j_struct_get_array_element(struct_student, json_obj, int, score); s2j_struct_get_basic_element(struct_student, json_obj, string, name); s2j_struct_get_basic_element(struct_student, json_obj, double, weight); +#else // another xxx_ex api, add default value and more secure + s2j_struct_get_array_element_ex(struct_student, json_obj, int, score, 8, 0); + s2j_struct_get_basic_element_ex(struct_student, json_obj, string, name, "John"); + s2j_struct_get_basic_element_ex(struct_student, json_obj, double, weight, 0); +#endif /* deserialize data to Student.Hometown structure object. */ s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown); diff --git a/struct2json/inc/s2j.h b/struct2json/inc/s2j.h old mode 100644 new mode 100755 index b9581c0..8d90e89 --- a/struct2json/inc/s2j.h +++ b/struct2json/inc/s2j.h @@ -38,7 +38,7 @@ extern "C" { #endif /* struct2json software version number */ -#define S2J_SW_VERSION "1.0.3" +#define S2J_SW_VERSION "1.0.4" /* Create JSON object */ #define s2j_create_json_obj(json_obj) \ @@ -71,11 +71,16 @@ extern "C" { /* Get basic type element for structure object */ #define s2j_struct_get_basic_element(to_struct, from_json, type, element) \ S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, element) +#define s2j_struct_get_basic_element_ex(to_struct, from_json, type, element, _defval) \ + S2J_STRUCT_GET_BASIC_ELEMENT_EX(to_struct, from_json, type, element, _defval) /* Get array type element for structure object */ #define s2j_struct_get_array_element(to_struct, from_json, type, element) \ S2J_STRUCT_GET_ARRAY_ELEMENT(to_struct, from_json, type, element) +#define s2j_struct_get_array_element_ex(to_struct, from_json, type, element, size, _defval) \ + S2J_STRUCT_GET_ARRAY_ELEMENT_EX(to_struct, from_json, type, element, size, _defval) + /* Get child structure type element for structure object */ #define s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) \ S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, element) diff --git a/struct2json/inc/s2jdef.h b/struct2json/inc/s2jdef.h old mode 100644 new mode 100755 index be445ac..578b8d5 --- a/struct2json/inc/s2jdef.h +++ b/struct2json/inc/s2jdef.h @@ -45,14 +45,47 @@ typedef struct { json_temp = cJSON_GetObjectItem(from_json, #_element); \ if (json_temp) (to_struct)->_element = json_temp->valueint; +#define S2J_STRUCT_GET_int_ELEMENT_EX(to_struct, from_json, _element, _defval) \ + { \ + if (from_json) { \ + json_temp = cJSON_GetObjectItem(from_json, #_element); \ + if (json_temp) (to_struct)->_element = json_temp->valueint; \ + else (to_struct)->_element = _defval; \ + } else { \ + (to_struct)->_element = _defval; \ + } \ + } + #define S2J_STRUCT_GET_string_ELEMENT(to_struct, from_json, _element) \ json_temp = cJSON_GetObjectItem(from_json, #_element); \ if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring); +#define S2J_STRUCT_GET_string_ELEMENT_EX(to_struct, from_json, _element, _defval) \ + { \ + if (from_json) { \ + json_temp = cJSON_GetObjectItem(from_json, #_element); \ + if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring); \ + else strcpy((to_struct)->_element, _defval); \ + } else { \ + strcpy((to_struct)->_element, _defval); \ + } \ + } + #define S2J_STRUCT_GET_double_ELEMENT(to_struct, from_json, _element) \ json_temp = cJSON_GetObjectItem(from_json, #_element); \ if (json_temp) (to_struct)->_element = json_temp->valuedouble; +#define S2J_STRUCT_GET_double_ELEMENT_EX(to_struct, from_json, _element, _defval) \ + { \ + if (from_json) { \ + json_temp = cJSON_GetObjectItem(from_json, #_element); \ + if (json_temp) (to_struct)->_element = json_temp->valuedouble; \ + else (to_struct)->_element = _defval; \ + } else { \ + (to_struct)->_element = _defval; \ + } \ + } + #define S2J_STRUCT_ARRAY_GET_int_ELEMENT(to_struct, from_json, _element, index) \ (to_struct)->_element[index] = from_json->valueint; @@ -65,6 +98,21 @@ typedef struct { #define S2J_STRUCT_ARRAY_GET_ELEMENT(to_struct, from_json, type, _element, index) \ S2J_STRUCT_ARRAY_GET_##type##_ELEMENT(to_struct, from_json, _element, index) +#define S2J_STRUCT_ARRAY_GET_int_ELEMENT_EX(to_struct, from_json, _element, index, _defval) \ + if (from_json) (to_struct)->_element[index] = from_json->valueint; \ + else (to_struct)->_element[index] = _defval; + +#define S2J_STRUCT_ARRAY_GET_string_ELEMENT_EX(to_struct, from_json, _element, index, _defval) \ + if (from_json) strcpy((to_struct)->_element[index], from_json->valuestring); \ + else strcpy((to_struct)->_element[index], _defval); + +#define S2J_STRUCT_ARRAY_GET_double_ELEMENT_EX(to_struct, from_json, _element, index) \ + if (from_json) (to_struct)->_element[index] = from_json->valuedouble; \ + else (to_struct)->_element[index] = _defval; + +#define S2J_STRUCT_ARRAY_GET_ELEMENT_EX(to_struct, from_json, type, _element, index, _defval) \ + S2J_STRUCT_ARRAY_GET_##type##_ELEMENT_EX(to_struct, from_json, _element, index, _defval) + #define S2J_JSON_SET_int_ELEMENT(to_json, from_struct, _element) \ cJSON_AddNumberToObject(to_json, #_element, (from_struct)->_element); @@ -125,6 +173,9 @@ typedef struct { #define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \ S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element) +#define S2J_STRUCT_GET_BASIC_ELEMENT_EX(to_struct, from_json, type, _element, _defval) \ + S2J_STRUCT_GET_##type##_ELEMENT_EX(to_struct, from_json, _element, _defval) + #define S2J_STRUCT_GET_ARRAY_ELEMENT(to_struct, from_json, type, _element) \ { \ cJSON *array, *array_element; \ @@ -139,6 +190,30 @@ typedef struct { } \ } +#define S2J_STRUCT_GET_ARRAY_ELEMENT_EX(to_struct, from_json, type, _element, size, _defval) \ + { \ + size_t index = 0, realsize = 0; \ + if (from_json) { \ + cJSON *array = NULL, *array_element = NULL; \ + array = cJSON_GetObjectItem(from_json, #_element); \ + if (array) { \ + realsize = cJSON_GetArraySize(array); \ + while (index < realsize) { \ + array_element = cJSON_GetArrayItem(array, index); \ + S2J_STRUCT_ARRAY_GET_ELEMENT_EX(to_struct, array_element, type, _element, index++, _defval); \ + } \ + } else { \ + while (index < size) { \ + S2J_STRUCT_ARRAY_GET_ELEMENT_EX(to_struct, array_element, type, _element, index++, _defval); \ + } \ + } \ + } else { \ + while (index < size) { \ + S2J_STRUCT_ARRAY_GET_ELEMENT_EX(to_struct, from_json, type, _element, index++, _defval); \ + } \ + } \ + } + #define S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, _element) \ type *child_struct = &((to_struct)->_element); \ cJSON *child_json = cJSON_GetObjectItem(from_json, #_element);