Skip to content

Commit

Permalink
Merge pull request armink#11 from jokidream/joki_develop
Browse files Browse the repository at this point in the history
Add xxx_ex API to solove some problems
  • Loading branch information
armink authored Feb 23, 2020
2 parents a51e9ab + 7ca54d8 commit 5674312
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion struct2json/inc/s2j.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions struct2json/inc/s2jdef.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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; \
Expand All @@ -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);
Expand Down

0 comments on commit 5674312

Please sign in to comment.