implemented serialization
This commit is contained in:
parent
22e063ff59
commit
655e040f1d
12 changed files with 540 additions and 218 deletions
|
|
@ -31,13 +31,10 @@ struct struct_B {
|
|||
KWIL_FIELD()
|
||||
float f;
|
||||
|
||||
float f_NOT_KWIL;
|
||||
int i_NOT_KWIL;
|
||||
|
||||
KWIL_FIELD()
|
||||
int i;
|
||||
KWIL_FIELD()
|
||||
char* str;
|
||||
//KWIL_FIELD()
|
||||
//char* str;
|
||||
KWIL_FIELD()
|
||||
char str_static[44];
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
#define KWIL_GEN_IMPL(...)\
|
||||
size_t enum_A_json_length(enum enum_A* src) {\
|
||||
switch(*src) {\
|
||||
case VALUE_A: return 7;\
|
||||
case VALUE_B: return 7;\
|
||||
case VALUE_C: return 7;\
|
||||
case VALUE_A: return 7 + 2;\
|
||||
case VALUE_B: return 7 + 2;\
|
||||
case VALUE_C: return 7 + 2;\
|
||||
}\
|
||||
return 2;\
|
||||
}\
|
||||
|
|
@ -19,8 +19,17 @@ size_t enum_A_to_json(enum enum_A* src, char* out) {\
|
|||
}\
|
||||
return sprintf(out, "\"\"");\
|
||||
}\
|
||||
enum enum_A enum_A_from_json(const char* json) {\
|
||||
if(strncmp("\"VALUE_A\"", json, 8) == 0) {\
|
||||
return VALUE_A;\
|
||||
} else if(strncmp("\"VALUE_B\"", json, 8) == 0) {\
|
||||
return VALUE_B;\
|
||||
} else if(strncmp("\"VALUE_C\"", json, 8) == 0) {\
|
||||
return VALUE_C;\
|
||||
} else { return VALUE_A; }\
|
||||
}\
|
||||
size_t struct_A_json_length(struct struct_A* src) {\
|
||||
size_t json_capacity = 2;\
|
||||
size_t json_capacity = 0;\
|
||||
/* length of b */\
|
||||
json_capacity += 5+ snprintf(NULL, 0, "%d", src->b);\
|
||||
/* length of a */\
|
||||
|
|
@ -28,7 +37,7 @@ size_t struct_A_json_length(struct struct_A* src) {\
|
|||
/* length of u */\
|
||||
json_capacity += 5 + snprintf(NULL, 0, "%du", src->u);\
|
||||
/* length of dyn_str */\
|
||||
json_capacity += 11 + strlen(src->dyn_str);\
|
||||
json_capacity += 11 + strlen(src->dyn_str) + 2;\
|
||||
return json_capacity;\
|
||||
}\
|
||||
int struct_A_to_json(struct struct_A* src, char* json) {\
|
||||
|
|
@ -59,16 +68,75 @@ int struct_A_to_json(struct struct_A* src, char* json) {\
|
|||
strcpy(json + json_len - 1, "}");\
|
||||
return json_len;\
|
||||
}\
|
||||
struct struct_A struct_A_from_json(const char* json) {\
|
||||
struct struct_A dest;\
|
||||
memset(&dest, 0x0, sizeof(struct struct_A)); const char* reader = json;\
|
||||
char buffer[48];\
|
||||
char* writer = buffer;\
|
||||
buffer[47] = '\0';\
|
||||
int counter = 0;counter = counter;\
|
||||
do {\
|
||||
while(*reader != '"') { reader++; }\
|
||||
reader++;\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != '"');\
|
||||
*writer = '\0';\
|
||||
do {\
|
||||
reader++;\
|
||||
} while(*reader != ':');\
|
||||
if(strcmp(buffer, "b") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.b = atoi(buffer);\
|
||||
} else if(strcmp(buffer, "a") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.a = atof(buffer);\
|
||||
} else if(strcmp(buffer, "u") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.u = atoi(buffer);\
|
||||
} else if(strcmp(buffer, "dyn_str") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.dyn_str = malloc(strlen(buffer)-1);\
|
||||
dest.dyn_str[0] = '\0';\
|
||||
strncpy(dest.dyn_str, buffer+1, strlen(buffer)-2);\
|
||||
} else {}\
|
||||
} while(*reader != '}');\
|
||||
return dest;\
|
||||
}\
|
||||
size_t struct_B_json_length(struct struct_B* src) {\
|
||||
size_t json_capacity = 2;\
|
||||
size_t json_capacity = 0;\
|
||||
/* length of f */\
|
||||
json_capacity += 5 + snprintf(NULL, 0, "%f", src->f);\
|
||||
/* length of i */\
|
||||
json_capacity += 5+ snprintf(NULL, 0, "%d", src->i);\
|
||||
/* length of str */\
|
||||
json_capacity += 7 + strlen(src->str);\
|
||||
/* length of str_static */\
|
||||
json_capacity += 14 + strlen(src->str_static);\
|
||||
json_capacity += 14 + strlen(src->str_static) + 2;\
|
||||
/* length of other_struct */\
|
||||
json_capacity += 16 + struct_A_json_length(&src->other_struct);\
|
||||
/* length of other_struct_typedef */\
|
||||
|
|
@ -92,13 +160,6 @@ int struct_B_to_json(struct struct_B* src, char* json) {\
|
|||
json_len += sprintf(json + json_len, "%d", src->i);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: str */\
|
||||
json_len += sprintf(json + json_len, "\"str\":");\
|
||||
if(src->str != NULL) {\
|
||||
json_len += sprintf(json + json_len, "\"%s\"", src->str);\
|
||||
}\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: str_static */\
|
||||
json_len += sprintf(json + json_len, "\"str_static\":");\
|
||||
if(src->str_static != NULL) {\
|
||||
|
|
@ -129,4 +190,110 @@ int struct_B_to_json(struct struct_B* src, char* json) {\
|
|||
strcpy(json + json_len - 1, "}");\
|
||||
return json_len;\
|
||||
}\
|
||||
struct struct_B struct_B_from_json(const char* json) {\
|
||||
struct struct_B dest;\
|
||||
memset(&dest, 0x0, sizeof(struct struct_B)); const char* reader = json;\
|
||||
char buffer[48];\
|
||||
char* writer = buffer;\
|
||||
buffer[47] = '\0';\
|
||||
int counter = 0;counter = counter;\
|
||||
do {\
|
||||
while(*reader != '"') { reader++; }\
|
||||
reader++;\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != '"');\
|
||||
*writer = '\0';\
|
||||
do {\
|
||||
reader++;\
|
||||
} while(*reader != ':');\
|
||||
if(strcmp(buffer, "f") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.f = atof(buffer);\
|
||||
} else if(strcmp(buffer, "i") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
dest.i = atoi(buffer);\
|
||||
} else if(strcmp(buffer, "str_static") == 0) {\
|
||||
while(*reader == ' ' || *reader == ':') { reader++; }\
|
||||
writer = buffer;\
|
||||
do {\
|
||||
*writer = *reader;\
|
||||
writer++; reader++;\
|
||||
} while(*reader != ',' && *reader != '}');\
|
||||
*writer = '\0';\
|
||||
strncpy(dest.str_static, buffer+1, strlen(buffer)-2);\
|
||||
} else if(strcmp(buffer, "other_struct") == 0) {\
|
||||
while(*reader != '{' && *reader != '"') { reader++; }\
|
||||
dest.other_struct = struct_A_from_json(reader);\
|
||||
counter = 0;\
|
||||
do {\
|
||||
if(*reader == '}') counter--;\
|
||||
else if(*reader == '{') counter++;\
|
||||
reader++;\
|
||||
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
||||
if(*reader == '\0') {\
|
||||
reader--;\
|
||||
} else {\
|
||||
reader++;\
|
||||
}\
|
||||
} else if(strcmp(buffer, "other_struct_typedef") == 0) {\
|
||||
while(*reader != '{' && *reader != '"') { reader++; }\
|
||||
dest.other_struct_typedef = struct_A_from_json(reader);\
|
||||
counter = 0;\
|
||||
do {\
|
||||
if(*reader == '}') counter--;\
|
||||
else if(*reader == '{') counter++;\
|
||||
reader++;\
|
||||
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
||||
if(*reader == '\0') {\
|
||||
reader--;\
|
||||
} else {\
|
||||
reader++;\
|
||||
}\
|
||||
} else if(strcmp(buffer, "other_enum") == 0) {\
|
||||
while(*reader != '{' && *reader != '"') { reader++; }\
|
||||
dest.other_enum = enum_A_from_json(reader);\
|
||||
counter = 0;\
|
||||
do {\
|
||||
if(*reader == '}') counter--;\
|
||||
else if(*reader == '{') counter++;\
|
||||
reader++;\
|
||||
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
||||
if(*reader == '\0') {\
|
||||
reader--;\
|
||||
} else {\
|
||||
reader++;\
|
||||
}\
|
||||
} else if(strcmp(buffer, "other_enum_typedef") == 0) {\
|
||||
while(*reader != '{' && *reader != '"') { reader++; }\
|
||||
dest.other_enum_typedef = enum_A_from_json(reader);\
|
||||
counter = 0;\
|
||||
do {\
|
||||
if(*reader == '}') counter--;\
|
||||
else if(*reader == '{') counter++;\
|
||||
reader++;\
|
||||
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
||||
if(*reader == '\0') {\
|
||||
reader--;\
|
||||
} else {\
|
||||
reader++;\
|
||||
}\
|
||||
} else {}\
|
||||
} while(*reader != '}');\
|
||||
return dest;\
|
||||
}\
|
||||
|
||||
|
|
|
|||
|
|
@ -19,10 +19,8 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
struct struct_B b = {
|
||||
.f = 123.0,
|
||||
.f_NOT_KWIL = 0,
|
||||
.i_NOT_KWIL = 0,
|
||||
.i = 3,
|
||||
.str = "SNALE!!",
|
||||
.str_static = "",
|
||||
.other_struct = a,
|
||||
.other_struct_typedef = {
|
||||
.b = -20, .u = 13, .a = -3.14, .dyn_str = "AWESOMEE"
|
||||
|
|
@ -31,21 +29,39 @@ int main(int argc, char* argv[]) {
|
|||
.other_enum_typedef = VALUE_C
|
||||
};
|
||||
|
||||
int required = struct_B_json_length(&b);
|
||||
char* json = malloc(required);
|
||||
int allocate_bytes = struct_B_json_length(&b)+1;
|
||||
char* json = malloc(allocate_bytes);
|
||||
int json_len = struct_B_to_json(&b, json);
|
||||
|
||||
printf("allocated %d bytes for json of struct_B\n", required);
|
||||
printf("struct_B as json:\n%s\n", json);
|
||||
printf("allocated %d bytes for json of struct_B\n", allocate_bytes);
|
||||
printf("returned length of json string: %d\n", json_len);
|
||||
printf("actual length of json string: %zu\n\n", strlen(json));
|
||||
printf("struct_B as json:\n%s\n\n", json);
|
||||
|
||||
int real_len = strlen(json);
|
||||
|
||||
free(json);
|
||||
if(real_len != json_len) {
|
||||
printf("Json Length (%d) does not match allocated space (%d)\n", required, json_len);
|
||||
if (real_len != allocate_bytes) {
|
||||
printf("Json Length (%d) does not match allocated space (%d)\n", json_len,
|
||||
allocate_bytes);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct struct_B deserialized = struct_B_from_json(json);
|
||||
|
||||
|
||||
allocate_bytes = struct_B_json_length(&b);
|
||||
char* reserialized_json = malloc(allocate_bytes);
|
||||
struct_B_to_json(&deserialized, reserialized_json);
|
||||
printf("deserialzed (serialized):\n%s\n\n", json);
|
||||
|
||||
if(strcmp(json, reserialized_json) != 0) {
|
||||
printf("Deserialized struct does not match original\n");
|
||||
free(json);
|
||||
return 2;
|
||||
}
|
||||
|
||||
free(json);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue