
Fast, tiny, and friendly code generator that turns your C structs into fully featured JSON serializers/deserializers.
Highlights
- Schema-first workflow – describe your structs once, generate battle-tested C code automatically.
- Zero runtime reflection – everything happens at compile time, so the generated code stays fast and lean.
- Thread-safe runtime – the parsing context uses explicit structures rather than globals.
- Batteries included – includes a lightweight
sstr string helper library and ready-made array helpers.
- CI-friendly build – warnings are treated as errors and the Make targets work the same locally and in automation.
Contents
- Overview
- Build and Install
- Quick Start
- The Format of Structs Definition File
- The JSON API
- More Resources
- Contributing & Community
Overview
json-gen-c is a program for serializing C structs to JSON and deserializing JSON to C structs. It parses struct definition files then generates C code to handle both directions.

Build and Install
The project uses a modern, efficient build system with support for parallel compilation:
# Parallel build (recommended)
make -j$(nproc)
# Debug build
make debug
# Build with sanitizers
make sanitize
# Show build configuration
make show-config
To build example, tests, and benchmarks
# build ./build/example/example
make example
# build ./build/test/unit_test
make test
# build ./build/benchmark/json_bench
make benchmark
All build artifacts are organized under the build/ directory:
build/bin/ - Main executable
build/lib/ - Static libraries
build/example/ - Example executable
build/test/ - Test executables
build/benchmark/ - Benchmark executable
Quick Start
example
Define Structs
For example, create a file name struct.json-gen-c as contents below:
struct A {
int int_val1;
int int_val2;
long long_val;
double double_val;
float float_val;
int int_val_array[];
B b_val;
};
struct B {
int id;
};
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:74
Note that we don't use C-style string char*, a more resonable type is sstr_t. You can find more details about sstr_t in document of sstr.
Compiling Your Struct Definition File
json-gen-c -in struct.json-gen-c -out .
This generates the following files in your specified destination directory:
json.gen.h, the header which declares your generated structures and functions.
json.gen.c, which contains the implementation of your functions.
sstr.h, sstr.c, the string manipulation helper functions that generated code depends on.
Use Your Generated Codes
To Serialize Structs to JSON
struct A a;
A_init(&a);
json_marshal_A(&a, json_str);
printf(
"marshal a to json> %s\n",
sstr_cstr(json_str));
A_clear(&a);
sstr_t sstr_new()
Create an empty sstr_t.
Definition sstr.c:42
void sstr_free(sstr_t s)
delete a sstr_t.
Definition sstr.c:48
char * sstr_cstr(sstr_t s)
Return C-style string representation of s.
Definition sstr.c:86
To Serialize Array of Structs to JSON
struct A a[3];
for (i = 0; i < 3; ++i) {
A_init(&a[i]);
}
json_marshal_array_A(a, 3, json_str);
printf(
"marshal a[] to json> %s\n",
sstr_cstr(json_str));
for (i = 0; i < 3; ++i) {
A_clear(&a[i]);
}
To Deserialize JSON to Structs
struct A a;
A_init(&a);
json_unmarshal_A(json_str, &a);
A_clear(&a);
To Deserialize JSON to Array of Structs
struct A *a = NULL;
int len = 0;
json_unmarshal_array_A(&a, &len, json_str);
int i;
for (i = 0; i < len; ++i) {
A_clear(&a[i]);
}
free(a);
Build System
For detailed build system documentation, see BUILD_SYSTEM.md. If you are new to the project, the friendly walkthrough in docs/GETTING_STARTED.md covers installation, schema authoring, and integration.
The Format of Structs Definition File
Define a struct like:
struct <struct_name> {
<field_type> <field_name> []?;
<field_type> <field_name> []?;
...
};
The field type can be one of the following:
int
long
float
double
sstr_t
bool
- a struct name
If a field is an array, just append [] after the field name.
The JSON API
int <struct_name>_init(struct <struct_name> *obj);
int <struct_name>_clear(struct <struct_name> *obj);
int json_marshal_<struct_name>(struct <struct_name>*obj,
sstr_t out);
int json_marshal_array_<struct_name>(struct <struct_name>*obj,
int len,
sstr_t out);
int json_unmarshal_<struct_name>(
sstr_t in, struct <struct_name>*obj);
int json_unmarshal_<struct_name>(
sstr_t in, struct <struct_name>**obj,
int *len);
More Resources
Contributing & Community
We welcome issues, ideas, documentation updates, and code contributions.
- Read the CONTRIBUTING.md guide for local setup, coding style, and pull-request tips.
- File bugs or feature requests via GitHub Issues with clear reproduction details.
- Share what you build! Open a discussion or PR if you want your project added to a future showcase section.
Thanks for helping json-gen-c grow. Happy hacking!
License
Codes of json-gen-c are licensed under GPL-3.0, except for the codes it generated. The copy right of the codes generated by json-gen-c is owned by the user who wrote the struct definition file, same as the copy right of a PDF file generated by Latex is owned by the user who wrote the tex file.