39#ifndef GP_JSON_SERDES_H
40#define GP_JSON_SERDES_H
61#define GP_JSON_SERDES_TYPE(type) ((type) & 0x0f)
103#define GP_2(P1, P2, ...) P2
131#define GP_JSON_SERDES_STR_CPY(struct, memb, flags, size, ...) \
132 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
133 .offset = offsetof(struct, memb), \
134 .type = GP_JSON_SERDES_STR | flags, \
164#define GP_JSON_SERDES_STR_DUP(struct, memb, flags, max_size, ...) \
165 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
166 .offset = offsetof(struct, memb), \
167 .type = GP_JSON_SERDES_STR | flags, \
168 .str_max_size = max_size}
197#define GP_JSON_SERDES_INT(struct, memb, flags, min, max, ...) \
198 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
199 .offset = offsetof(struct, memb), \
200 .type = GP_JSON_SERDES_INT | flags, \
201 .type_size = sizeof(int), \
202 .lim_int = {min, max}}
231#define GP_JSON_SERDES_UINT(struct, memb, flags, min, max, ...) \
232 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
233 .offset = offsetof(struct, memb), \
234 .type = GP_JSON_SERDES_UINT | flags, \
235 .type_size = sizeof(unsigned int), \
236 .lim_int = {min, max}}
265#define GP_JSON_SERDES_LONG(struct, memb, flags, min, max, ...) \
266 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
267 .offset = offsetof(struct, memb), \
268 .type = GP_JSON_SERDES_INT | flags, \
269 .type_size = sizeof(long), \
270 .lim_int = {min, max}}
299#define GP_JSON_SERDES_ULONG(struct, memb, flags, min, max, ...) \
300 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
301 .offset = offsetof(struct, memb), \
302 .type = GP_JSON_SERDES_UINT | flags, \
303 .type_size = sizeof(unsigned long), \
304 .lim_int = {min, max}}
333#define GP_JSON_SERDES_LLONG(struct, memb, flags, min, max, ...) \
334 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
335 .offset = offsetof(struct, memb), \
336 .type = GP_JSON_SERDES_INT | flags, \
337 .type_size = sizeof(long long), \
338 .lim_int = {min, max}}
367#define GP_JSON_SERDES_ULLONG(struct, memb, flags, min, max, ...) \
368 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
369 .offset = offsetof(struct, memb), \
370 .type = GP_JSON_SERDES_UINT | flags, \
371 .type_size = sizeof(unsigned long long), \
372 .lim_int = {min, max}}
401#define GP_JSON_SERDES_INT8(struct, memb, flags, min, max, ...) \
402 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
403 .offset = offsetof(struct, memb), \
404 .type = GP_JSON_SERDES_INT | flags, \
406 .lim_int = {min, max}}
435#define GP_JSON_SERDES_UINT8(struct, memb, flags, min, max, ...) \
436 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
437 .offset = offsetof(struct, memb), \
438 .type = GP_JSON_SERDES_UINT | flags, \
440 .lim_int = {min, max}}
469#define GP_JSON_SERDES_INT16(struct, memb, flags, min, max, ...) \
470 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
471 .offset = offsetof(struct, memb), \
472 .type = GP_JSON_SERDES_INT | flags, \
474 .lim_int = {min, max}}
503#define GP_JSON_SERDES_UINT16(struct, memb, flags, min, max, ...) \
504 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
505 .offset = offsetof(struct, memb), \
506 .type = GP_JSON_SERDES_UINT | flags, \
508 .lim_int = {min, max}}
510#define GP_JSON_SERDES_INT32(struct, memb, flags, min, max, ...) \
511 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
512 .offset = offsetof(struct, memb), \
513 .type = GP_JSON_SERDES_INT | flags, \
514 .type_size = 4, .lim_int = {min, max}}
516#define GP_JSON_SERDES_UINT32(struct, memb, flags, min, max, ...) \
517 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
518 .offset = offsetof(struct, memb), \
519 .type = GP_JSON_SERDES_UINT | flags, \
521 .lim_int = {min, max}}
523#define GP_JSON_SERDES_INT64(struct, memb, flags, min, max, ...) \
524 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
525 .offset = offsetof(struct, memb), \
526 .type = GP_JSON_SERDES_INT | flags, \
528 .lim_int = {min, max}}
530#define GP_JSON_SERDES_UINT64(struct, memb, flags, min, max, ...) \
531 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
532 .offset = offsetof(struct, memb), \
533 .type = GP_JSON_SERDES_UINT | flags, \
535 .lim_int = {min, max}}
537#define GP_JSON_SERDES_FLOAT(struct, memb, flags, min, max, ...) \
538 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
539 .offset = offsetof(struct, memb), \
540 .type = GP_JSON_SERDES_FLOAT | flags, \
541 .type_size = sizeof(float), \
542 .lim_float = {min, max}}
544#define GP_JSON_SERDES_DOUBLE(struct, memb, flags, min, max, ...) \
545 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
546 .offset = offsetof(struct, memb), \
547 .type = GP_JSON_SERDES_FLOAT | flags, \
548 .type_size = sizeof(double), \
549 .lim_float = {min, max}}
576#define GP_JSON_SERDES_BOOL(struct, memb, flags, ...) \
577 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
578 .offset = offsetof(struct, memb), \
579 .type = GP_JSON_SERDES_BOOL | flags}
622 const char *
id,
void *baseptr);
Common JSON reader/writer definitions.
int gp_json_save_struct(const char *path, const gp_json_struct *desc, void *baseptr)
Serializes a C structure to JSON and saves the result into a file.
int gp_json_read_struct(gp_json_reader *json, gp_json_val *val, const gp_json_struct *desc, void *baseptr)
Deserializes a JSON object into a C structure.
json_serdes_type
Value types, the complete type is determinted by both value and size.
@ GP_JSON_SERDES_OPTIONAL
#define GP_JSON_SERDES_UINT(struct, memb, flags, min, max,...)
Defines an unsigned integer.
int gp_json_load_struct(const char *path, const gp_json_struct *desc, void *baseptr)
Deserializes a JSON object into a C structure.
#define GP_JSON_SERDES_INT(struct, memb, flags, min, max,...)
Defines an integer.
int gp_json_write_struct(gp_json_writer *json, const gp_json_struct *desc, const char *id, void *baseptr)
Serializes a C structure into a JSON object.
#define GP_JSON_SERDES_BOOL(struct, memb, flags,...)
Defines an bool.
Serializer and deserializer floating point limits.
Serializer and deserializer integer limits.
A JSON parser internal state.
Describe a single structure member for serializer and deserialzer.
size_t type_size
Variable size, either explicit size or sizeof()
enum json_serdes_type type
Variable type.
size_t offset
Offset in the C structure.
A parsed JSON key value pair.