GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions
gp_json_serdes.h File Reference

C structure to JSON serializer and deserializer. More...

#include <stddef.h>
#include <stdint.h>
#include <limits.h>

Go to the source code of this file.

Data Structures

struct  gp_json_int_limits
 Serializer and deserializer integer limits. More...
 
struct  gp_json_float_limits
 Serializer and deserializer floating point limits. More...
 
struct  gp_json_struct
 Describe a single structure member for serializer and deserialzer. More...
 

Macros

#define GP_JSON_SERDES_STR_CPY(struct, memb, flags, size, ...)
 Defines a string array.
 
#define GP_JSON_SERDES_STR_DUP(struct, memb, flags, max_size, ...)
 Defines a string pointer.
 
#define GP_JSON_SERDES_INT(struct, memb, flags, min, max, ...)
 Defines an integer.
 
#define GP_JSON_SERDES_UINT(struct, memb, flags, min, max, ...)
 Defines an unsigned integer.
 
#define GP_JSON_SERDES_LONG(struct, memb, flags, min, max, ...)
 Defines a long integer.
 
#define GP_JSON_SERDES_ULONG(struct, memb, flags, min, max, ...)
 Defines an unsigned long integer.
 
#define GP_JSON_SERDES_LLONG(struct, memb, flags, min, max, ...)
 Defines a long long integer.
 
#define GP_JSON_SERDES_ULLONG(struct, memb, flags, min, max, ...)
 Defines an unsigned long long integer.
 
#define GP_JSON_SERDES_INT8(struct, memb, flags, min, max, ...)
 Defines a 8-bit integer.
 
#define GP_JSON_SERDES_UINT8(struct, memb, flags, min, max, ...)
 Defines an unsigned 8-bit integer.
 
#define GP_JSON_SERDES_INT16(struct, memb, flags, min, max, ...)
 Defines a 16-bit integer.
 
#define GP_JSON_SERDES_UINT16(struct, memb, flags, min, max, ...)
 Defines an unsigned 16-bit integer.
 

Typedefs

typedef struct gp_json_struct gp_json_struct
 Describe a single structure member for serializer and deserialzer.
 

Enumerations

enum  json_serdes_type { }
 Value types, the complete type is determinted by both value and size. More...
 

Functions

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.
 
int gp_json_load_struct (const char *path, const gp_json_struct *desc, void *baseptr)
 Deserializes a JSON object into a C structure.
 
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.
 

Detailed Description

C structure to JSON serializer and deserializer.

How to read a C struct from a JSON file:

struct connection {
char *username;
char *password;
char *server_addr;
uint16_t port;
};
static struct gp_json_obj_attr connection_attrs[] = {
GP_JSON_SERDES_STR_DUP(struct connection, username, 0, 1024),
GP_JSON_SERDES_STR_DUP(struct connection, password, 0, 1024),
GP_JSON_SERDES_UINT16(struct connection, port, GP_JSON_SERDES_OPTIONAL, 0, UINT16_MAX),
GP_JSON_SERDES_STR_DUP(struct connection, server_addr, 0, 1024, "sever address"),
{}
};
...
struct connnection conn = {.port = 6666};
if (gp_json_load_struct(path, connection_attrs, &conn)) {
printf("Failed to load '%s'", path);
return 1;
}
...
#define GP_JSON_SERDES_STR_DUP(struct, memb, flags, max_size,...)
Defines a string pointer.
@ GP_JSON_SERDES_OPTIONAL
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_UINT16(struct, memb, flags, min, max,...)
Defines an unsigned 16-bit integer.
A JSON object attribute description i.e. key and type.

Definition in file gp_json_serdes.h.

Macro Definition Documentation

◆ GP_JSON_SERDES_INT

#define GP_JSON_SERDES_INT (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_INT | flags, \
.type_size = sizeof(int), \
.lim_int = {min, max}}
#define GP_JSON_SERDES_INT(struct, memb, flags, min, max,...)
Defines an integer.

Defines an integer.

The memb has to be an int type.

Parameters
structA structure type.
membAn integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass INT_MIN for full range.
maxAn upper limit, pass INT_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
int i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_INT(struct foo, i, 0, INT_MIN, INT_MAX),
...
};
Describe a single structure member for serializer and deserialzer.

Definition at line 193 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_INT16

#define GP_JSON_SERDES_INT16 (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_INT | flags, \
.type_size = 2, \
.lim_int = {min, max}}

Defines a 16-bit integer.

The memb has to be an int16_t type.

Parameters
structA structure type.
membAn int16_t member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass INT16_MIN for full range.
maxAn upper limit, pass INT16_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
int16_t i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_INT16(struct foo, i, 0, INT16_MIN, INT16_MAX),
...
};
#define GP_JSON_SERDES_INT16(struct, memb, flags, min, max,...)
Defines a 16-bit integer.

Definition at line 465 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_INT8

#define GP_JSON_SERDES_INT8 (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_INT | flags, \
.type_size = 1, \
.lim_int = {min, max}}

Defines a 8-bit integer.

The memb has to be an int8_t type.

Parameters
structA structure type.
membAn int8_t member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass INT8_MIN for full range.
maxAn upper limit, pass INT8_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
int8_t i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_INT8(struct foo, i, 0, INT8_MIN, INT8_MAX),
...
};
#define GP_JSON_SERDES_INT8(struct, memb, flags, min, max,...)
Defines a 8-bit integer.

Definition at line 397 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_LLONG

#define GP_JSON_SERDES_LLONG (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_INT | flags, \
.type_size = sizeof(long long), \
.lim_int = {min, max}}

Defines a long long integer.

The memb has to be a long long type.

Parameters
structA structure type.
membA long long integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass LLONG_MIN for full range.
maxAn upper limit, pass LLONG_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
long long l;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_LONG(struct foo, l, 0, LLONG_MIN, LLONG_MAX),
...
};
#define GP_JSON_SERDES_LONG(struct, memb, flags, min, max,...)
Defines a long integer.

Definition at line 329 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_LONG

#define GP_JSON_SERDES_LONG (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_INT | flags, \
.type_size = sizeof(long), \
.lim_int = {min, max}}

Defines a long integer.

The memb has to be a long type.

Parameters
structA structure type.
membA long integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass LONG_MIN for full range.
maxAn upper limit, pass LONG_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
long l;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_LONG(struct foo, l, 0, LONG_MIN, LONG_MAX),
...
};

Definition at line 261 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_STR_CPY

#define GP_JSON_SERDES_STR_CPY (   struct,
  memb,
  flags,
  size,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_STR | flags, \
.type_size = size}

Defines a string array.

The memb has to be a fixed size char array.

Parameters
structA structure type.
membA fixed size char array member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
sizeThe array size.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
char str_arr[128];
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_STR_CPY(struct foo, str_arr, 0, 128),
...
};
#define GP_JSON_SERDES_STR_CPY(struct, memb, flags, size,...)
Defines a string array.

Definition at line 127 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_STR_DUP

#define GP_JSON_SERDES_STR_DUP (   struct,
  memb,
  flags,
  max_size,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_STR | flags, \
.str_max_size = max_size}

Defines a string pointer.

The memb has to be a char * pointer. The string is allocated with strdup() and has to be later freed with free().

Parameters
structA structure type.
membA char * member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
max_sizeThe maximal string size.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
char *str;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_STR_DUP(struct foo, str, 0, 128),
...
};

Definition at line 160 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_UINT

#define GP_JSON_SERDES_UINT (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_UINT | flags, \
.type_size = sizeof(unsigned int), \
.lim_int = {min, max}}
#define GP_JSON_SERDES_UINT(struct, memb, flags, min, max,...)
Defines an unsigned integer.

Defines an unsigned integer.

The memb has to be an unsigned int type.

Parameters
structA structure type.
membAn unsigned integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass 0 for full range.
maxAn upper limit, pass UINT_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
unsigned int i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_UINT(struct foo, i, 0, 0, UINT_MAX),
...
};

Definition at line 227 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_UINT16

#define GP_JSON_SERDES_UINT16 (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_UINT | flags, \
.type_size = 2, \
.lim_int = {min, max}}

Defines an unsigned 16-bit integer.

The memb has to be an uint16_t type.

Parameters
structA structure type.
membAn uint16_t member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass 0 for full range.
maxAn upper limit, pass UINT16_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
uint16_t i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_UINT16(struct foo, i, 0, 0, UINT16_MAX),
...
};

Definition at line 499 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_UINT8

#define GP_JSON_SERDES_UINT8 (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_UINT | flags, \
.type_size = 1, \
.lim_int = {min, max}}

Defines an unsigned 8-bit integer.

The memb has to be an uint8_t type.

Parameters
structA structure type.
membAn uint8_t member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass 0 for full range.
maxAn upper limit, pass UINT8_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
uint8_t i;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_UINT8(struct foo, i, 0, 0, UINT8_MAX),
...
};
#define GP_JSON_SERDES_UINT8(struct, memb, flags, min, max,...)
Defines an unsigned 8-bit integer.

Definition at line 431 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_ULLONG

#define GP_JSON_SERDES_ULLONG (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_UINT | flags, \
.type_size = sizeof(unsigned long long), \
.lim_int = {min, max}}

Defines an unsigned long long integer.

The memb has to be an unsigned long long type.

Parameters
structA structure type.
membAn unsigned long integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass 0 for full range.
maxAn upper limit, pass ULLONG_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
unsigned long long l;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_ULLONG(struct foo, l, 0, 0, ULLONG_MAX),
...
};
#define GP_JSON_SERDES_ULLONG(struct, memb, flags, min, max,...)
Defines an unsigned long long integer.

Definition at line 363 of file gp_json_serdes.h.

◆ GP_JSON_SERDES_ULONG

#define GP_JSON_SERDES_ULONG (   struct,
  memb,
  flags,
  min,
  max,
  ... 
)
Value:
{.id = GP_2(dummy, ##__VA_ARGS__, #memb), \
.offset = offsetof(struct, memb), \
.type = GP_JSON_SERDES_UINT | flags, \
.type_size = sizeof(unsigned long), \
.lim_int = {min, max}}

Defines an unsigned long integer.

The memb has to be an unsigned long type.

Parameters
structA structure type.
membAn unsigned long integer member of the structure.
flagsCan be GP_JSON_SERDES_OPTIONAL if value does not have to be present.
minA lower limit, pass 0 for full range.
maxAn upper limit, pass ULONG_MAX for full range.
...Optional JSON id, if not set the member name is used instead.

Example use:

struct foo {
...
unsigned long l;
...
};
static const gp_json_struct struct_desc[] = {
...
GP_JSON_SERDES_ULONG(struct foo, l, 0, 0, ULONG_MAX),
...
};
#define GP_JSON_SERDES_ULONG(struct, memb, flags, min, max,...)
Defines an unsigned long integer.

Definition at line 295 of file gp_json_serdes.h.

Typedef Documentation

◆ gp_json_struct

Describe a single structure member for serializer and deserialzer.

Serialization and deserialization functions take an array sorted by id of these structures that describe subset of a structure memebers.

The array is usually constructed with the help of the GP_JSON_SERDES_FOO() macros.

Enumeration Type Documentation

◆ json_serdes_type

Value types, the complete type is determinted by both value and size.

Enumerator
GP_JSON_SERDES_OPTIONAL 

If set parameter does not have to be present

Definition at line 47 of file gp_json_serdes.h.

Function Documentation

◆ gp_json_load_struct()

int gp_json_load_struct ( const char *  path,
const gp_json_struct desc,
void *  baseptr 
)

Deserializes a JSON object into a C structure.

This is a simplified interface that loads just a single structure from a file.

Parameters
pathA path to a file.
descAn alphabetically sorted by id and NULL id terminatred array of structure member descriptions.
baseptrA pointer to the deserialized C structure.

◆ gp_json_read_struct()

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.

Consumes and JSON object and deserializes it into a C structure.

This function can be mixed with the rest of the JSON parser functions.

Parameters
jsonA json reader.
valA json reader value.
descAn alphabetically sorted by id and NULL id terminatred array of structure member descriptions.
baseptrA pointer to the deserialized C structure.

◆ gp_json_write_struct()

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.

This function can be mixed with the rest of the JSON writer functions.

Parameters
jsonA json writer
descAn NULL id terminated array of structure member descriptions.
idAn JSON id for the object, should be NULL if there is no id required in current context, e.g. inside of an JSON array. @baseptr A pointer to the serialized C structure.