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 <limits.h>
45
48 GP_JSON_SERDES_STR,
51 GP_JSON_SERDES_FLOAT,
52
55};
56
57#define GP_JSON_SERDES_TYPE(type) ((type) & 0x0f)
58
61 int64_t min;
62 uint64_t max;
63};
64
67 float min;
68 float max;
69};
70
80typedef struct gp_json_struct {
82 const char *id;
84 size_t offset;
85
89 size_t type_size;
90
92 union {
93 struct gp_json_int_limits lim_int;
94 struct gp_json_float_limits lim_float;
95 size_t str_max_size;
96 };
98
99#define GP_2(P1, P2, ...) P2
100
127#define GP_JSON_SERDES_STR_CPY(struct, memb, flags, size, ...) \
128 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
129 .offset = offsetof(struct, memb), \
130 .type = GP_JSON_SERDES_STR | flags, \
131 .type_size = size}
132
160#define GP_JSON_SERDES_STR_DUP(struct, memb, flags, max_size, ...) \
161 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
162 .offset = offsetof(struct, memb), \
163 .type = GP_JSON_SERDES_STR | flags, \
164 .str_max_size = max_size}
165
193#define GP_JSON_SERDES_INT(struct, memb, flags, min, max, ...) \
194 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
195 .offset = offsetof(struct, memb), \
196 .type = GP_JSON_SERDES_INT | flags, \
197 .type_size = sizeof(int), \
198 .lim_int = {min, max}}
199
227#define GP_JSON_SERDES_UINT(struct, memb, flags, min, max, ...) \
228 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
229 .offset = offsetof(struct, memb), \
230 .type = GP_JSON_SERDES_UINT | flags, \
231 .type_size = sizeof(unsigned int), \
232 .lim_int = {min, max}}
233
261#define GP_JSON_SERDES_LONG(struct, memb, flags, min, max, ...) \
262 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
263 .offset = offsetof(struct, memb), \
264 .type = GP_JSON_SERDES_INT | flags, \
265 .type_size = sizeof(long), \
266 .lim_int = {min, max}}
267
295#define GP_JSON_SERDES_ULONG(struct, memb, flags, min, max, ...) \
296 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
297 .offset = offsetof(struct, memb), \
298 .type = GP_JSON_SERDES_UINT | flags, \
299 .type_size = sizeof(unsigned long), \
300 .lim_int = {min, max}}
301
329#define GP_JSON_SERDES_LLONG(struct, memb, flags, min, max, ...) \
330 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
331 .offset = offsetof(struct, memb), \
332 .type = GP_JSON_SERDES_INT | flags, \
333 .type_size = sizeof(long long), \
334 .lim_int = {min, max}}
335
363#define GP_JSON_SERDES_ULLONG(struct, memb, flags, min, max, ...) \
364 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
365 .offset = offsetof(struct, memb), \
366 .type = GP_JSON_SERDES_UINT | flags, \
367 .type_size = sizeof(unsigned long long), \
368 .lim_int = {min, max}}
369
397#define GP_JSON_SERDES_INT8(struct, memb, flags, min, max, ...) \
398 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
399 .offset = offsetof(struct, memb), \
400 .type = GP_JSON_SERDES_INT | flags, \
401 .type_size = 1, \
402 .lim_int = {min, max}}
403
431#define GP_JSON_SERDES_UINT8(struct, memb, flags, min, max, ...) \
432 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
433 .offset = offsetof(struct, memb), \
434 .type = GP_JSON_SERDES_UINT | flags, \
435 .type_size = 1, \
436 .lim_int = {min, max}}
437
465#define GP_JSON_SERDES_INT16(struct, memb, flags, min, max, ...) \
466 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
467 .offset = offsetof(struct, memb), \
468 .type = GP_JSON_SERDES_INT | flags, \
469 .type_size = 2, \
470 .lim_int = {min, max}}
471
499#define GP_JSON_SERDES_UINT16(struct, memb, flags, min, max, ...) \
500 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
501 .offset = offsetof(struct, memb), \
502 .type = GP_JSON_SERDES_UINT | flags, \
503 .type_size = 2, \
504 .lim_int = {min, max}}
505
506#define GP_JSON_SERDES_INT32(struct, memb, flags, min, max, ...) \
507 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
508 .offset = offsetof(struct, memb), \
509 .type = GP_JSON_SERDES_INT | flags, \
510 .type_size = 4, .lim_int = {min, max}}
511
512#define GP_JSON_SERDES_UINT32(struct, memb, flags, min, max, ...) \
513 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
514 .offset = offsetof(struct, memb), \
515 .type = GP_JSON_SERDES_UINT | flags, \
516 .type_size = 4, \
517 .lim_int = {min, max}}
518
519#define GP_JSON_SERDES_INT64(struct, memb, flags, min, max, ...) \
520 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
521 .offset = offsetof(struct, memb), \
522 .type = GP_JSON_SERDES_INT | flags, \
523 .type_size = 8, \
524 .lim_int = {min, max}}
525
526#define GP_JSON_SERDES_UINT64(struct, memb, flags, min, max, ...) \
527 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
528 .offset = offsetof(struct, memb), \
529 .type = GP_JSON_SERDES_UINT | flags, \
530 .type_size = 8, \
531 .lim_int = {min, max}}
532
533#define GP_JSON_SERDES_FLOAT(struct, memb, flags, min, max, ...) \
534 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
535 .offset = offsetof(struct, memb), \
536 .type = GP_JSON_SERDES_FLOAT | flags, \
537 .type_size = sizeof(float), \
538 .lim_float = {min, max}}
539
540#define GP_JSON_SERDES_DOUBLE(struct, memb, flags, min, max, ...) \
541 {.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
542 .offset = offsetof(struct, memb), \
543 .type = GP_JSON_SERDES_FLOAT | flags, \
544 .type_size = sizeof(double), \
545 .lim_float = {min, max}}
546
561 const gp_json_struct *desc, void *baseptr);
562
573int gp_json_load_struct(const char *path,
574 const gp_json_struct *desc, void *baseptr);
575
588 const char *id, void *baseptr);
589
590#endif /* GP_JSON_SERDES_H */
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.
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.