gyf304 / cmarshal

Go-style JSON Marshal / Unmarshal for C structs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CMarshal

Online Demo

CMarshal brings Go style JSON marshaling / unmarshaling to C.

You can generate cJSON based JSON serializers and deserializers by adding

/* cmarshal:`true` */

to your C struct.

Compile

You'll need clang and libclang installed.

You may also need to modify Makefile so -I points to the correct include path for libclang

Do:

make cmarshal

This will compile cmarshal to the project directory.

Usage

Say that you have a C header file that contains some structs

typedef struct {
	int a;
	int b;
} MyStruct;

You can add some comments to it

/*
--- Add a configuration comment:
--- Note that the semi-colon after the comment is significant.
cmarshal:`{
	"cJSONInclude": "./cJSON.h",
	"unmarshalerHeaderFile": "./demo.unmarshaler.h",
	"unmarshalerImplFile": "./demo.unmarshaler.c",
	"marshalerHeaderFile": "./demo.marshaler.h",
	"marshalerImplFile": "./demo.marshaler.c"
}`
*/;

/*
--- Annotate you struct with:
cmarshal:`true`
*/
typedef struct {
	int a;
	int b;
	int c;      /* cmarshal:`{"key": "d"}` */
	int ignore; /* cmarshal:`{"ignore": true}` */
} MyStruct;

Now cd to the header of the directory. Assuming your file is called demo.h, do:

path/to/cmarshal ./demo.h

This will generate ./demo.marshaler.[ch] and ./demo.unmarshaler.[ch]

You will also need to include cJSON in your project.

You can then do things like

MyStruct s = {1, 2, 3, 4};
cJSON *json = marshal_MyStruct(&s);
char *str = cJSON_Print(json);
printf("%s\n", str);
free(str);
cJSON_Delete(json);

Which will print

{
	"a": 1,
	"b": 2,
	"d": 3
}

See the demo directory for a more in-depth example, including how to unmarshal a JSON.

Also see src/jsonstructs.h for all available options. (Fun fact: the cmarshal:... annotations are also parsed using cmarshal generated unmarshalers. cmarshal itself is bootstrapped!)

Use cases

  • Auto-generated JSON config parsers
  • Simple struct printer (as a debugging mechanism)
  • Introspecting C structs using cJSON API
  • Easily bloat your program line count! (not the intended use case, of course)

Contributing

Pull requests are welcome.

License

MIT

About

Go-style JSON Marshal / Unmarshal for C structs

License:MIT License


Languages

Language:C 96.1%Language:HTML 2.0%Language:Python 1.4%Language:Dockerfile 0.3%Language:Makefile 0.2%