GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_json_serdes.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2021-2023 Cyril Hrubis <metan@ucw.cz>
4 */
5
39#ifndef GP_JSON_SERDES_H
40#define GP_JSON_SERDES_H
41
42#include <stddef.h>
43#include <stdint.h>
44#include <stdbool.h>
45#include <limits.h>
46
48
51 GP_JSON_SERDES_STR,
54 GP_JSON_SERDES_FLOAT,
56
59};
60
61#define GP_JSON_SERDES_TYPE(type) ((type) & 0x0f)
62
65 int64_t min;
66 uint64_t max;
67};
68
71 float min;
72 float max;
73};
74
84typedef struct gp_json_struct {
86 const char *id;
88 size_t offset;
89
93 size_t type_size;
94
96 union {
97 struct gp_json_int_limits lim_int;
98 struct gp_json_float_limits lim_float;
99 size_t str_max_size;
100 };
102
103#define GP_2(P1, P2, ...) P2
104
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, \
135 .type_size = size}
136
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}
169
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}}
203
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}}
237
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}}
271
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}}
305
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}}
339
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}}
373
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, \
405 .type_size = 1, \
406 .lim_int = {min, max}}
407
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, \
439 .type_size = 1, \
440 .lim_int = {min, max}}
441
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, \
473 .type_size = 2, \
474 .lim_int = {min, max}}
475
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, \
507 .type_size = 2, \
508 .lim_int = {min, max}}
509
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}}
515
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, \
520 .type_size = 4, \
521 .lim_int = {min, max}}
522
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, \
527 .type_size = 8, \
528 .lim_int = {min, max}}
529
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, \
534 .type_size = 8, \
535 .lim_int = {min, max}}
536
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}}
543
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}}
550
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}
580
595 const gp_json_struct *desc, void *baseptr);
596
607int gp_json_load_struct(const char *path,
608 const gp_json_struct *desc, void *baseptr);
609
622 const char *id, void *baseptr);
623
634int gp_json_save_struct(const char *path,
635 const gp_json_struct *desc, void *baseptr);
636
637#endif /* GP_JSON_SERDES_H */
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.
const char * id
JSON id.
A parsed JSON key value pair.
A JSON writer.