Remove unnecessary NULL checks
Remove reference to mirror repos
Add -Wcast-qual
A simple embedded friendly JSON generator.
No heap usage, no malloc(). As minimal stack usage as possible.
Supports serialization to objects, arrays and primitives from int, unsigned int, _Bool and char arrays.
This is still beta!
#include "mtojson.h"
/* Some dummy functions to get and send some data */
size_t receive_data(int*);
void send_data(char *, size_t);
int
main()
{
size_t len;
int data[100];
enum { MAX_STRING_LEN = 1000 };
char json_text[MAX_STRING_LEN];
size_t json_len;
/* Define the JSON object */
const struct to_json json[] = {
{ .name = "received_data",
.value = data,
.vtype = t_to_int,
.count = &len,
.stype = t_to_object
},
{ NULL }
};
while (1) {
len = receive_data(data);
json_len = json_generate(json_text, json, len);
send_data(json_text, json_len);
}
}
See example.c
and test_mtojson.c
for more usage examples.
Use make example
to build all four variations of example.c
.
json_generate()
returns the length of the generated JSON text or 0 in case of an error.
To create an arbitrary value use t_to_value
and pass the correctly formatted value as char array.
NULL pointers passed as value are converted to literal 'null'.
Make sure struct arrays are NULL terminated!
const struct to_json tjs = { .value = &val, .vtype = t_to_int };
This is the simplest case, all other members are either not needed or initialized to the correct default value.
const struct to_json tjs[] = {
{ .value = &val, .vtype = t_to_int, .stype = t_to_array },
{ NULL }
};
const struct to_json tjs[] = {
{ .name = "name", .value = &val, .vtype = t_to_int, .stype = t_to_object },
{ NULL }
};
An structured type can have multiple members, so we need to create an array of structs here.
Arrays of structs MUST be terminated with a NULL member.
Additionally we need to set .stype
to tell what type of structured type we want to create.
Probably the most common case when creating an array is using values from an C array.
There is a shortcut to make this more convenient.
Instead of creating an explicit array type, use a primitive and provide a count:
const struct to_json tjs = { .value = int_arr, .count = 2, .vtype = t_to_int };
To convert an unsigned integer to a string with hexadecimal notation use the corresponding .vtype = t_to_hex...
. See the header file for all supported types.
This project is mainly developed on sourcehut.
If you want to contribute or find any bugs, feel free to choose the way that best fits your work style.
This project is inspired by microjson from Eric S. Raymond.