This guide walks you through installing json-gen-c, generating code, and integrating it into your project.
Prerequisites
- A C11-compatible compiler (GCC or Clang).
- GNU Make.
- Optional:
pkg-config, clang-format, and cppcheck for a smoother workflow.
1. Install json-gen-c
git clone https://github.com/zltl/json-gen-c.git
cd json-gen-c
make
sudo make install # optional, installs json-gen-c into your PATH
2. Describe Your Data
Create a .json-gen-c file describing the structs to serialize:
struct User {
int id;
double balance;
Address address;
};
struct Address {
};
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:74
3. Generate Code
json-gen-c -in demo.json-gen-c -out gen
The command emits:
gen/json.gen.h / gen/json.gen.c – struct definitions and JSON helpers.
gen/sstr.h / gen/sstr.c – lightweight string utilities required by the generated code.
4. Use the Generated API
#include "json.gen.h"
int main(void) {
struct User user;
User_init(&user);
user.id = 7;
json_marshal_User(&user, payload);
User_clear(&user);
return 0;
}
sstr_t sstr(const char *cstr)
Create a sstr_t from C-style (NULL-terminated) string str.
Definition sstr.c:84
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
Link your program with the generated json.gen.c and sstr.c files.
5. Regenerate on Schema Changes
Any time you edit the .json-gen-c schema, rerun json-gen-c to refresh the generated sources. Consider automating this step with a Makefile target so your build always has up-to-date code.
6. Learn More
- Browse the
example/ directory for reference schemas and usage.
- Run
json-gen-c --help to see command-line options.
- Consult the online documentation for API details.
Happy hacking! 🎉