Simple APIs for converting between JSON and C structs, based on cJSON https://sourceforge.net/p/cjson/wiki/Home
I. How it Work
II. Limits
III. How to use it
IV. Examples
V. Tests
First of all, there's a simple pre-parser tool to parse the pre-compiled C struct headers and generate a C source to describe the struct members' information table, e.g. name, type, offset, size.
Then the J2S framework can use cJSON to parse JSON and do the conversion based on that information table.
The parser only supports a few data types, such as char, int, short, long, float, double, struct, enum, union...etc.
The enum and struct defination should use typedef:
typedef enum {
...
} xxx;
typedef struct {
...
} xxx;
The system C headers should be included at the top of input C header, which would be stripped during parsing.
For arrays, only support array, 2D array, array pointer. And the array pointer defination should be:
typedef <type> (*array_ptr_<type>)[len]
For pointer member "data"(including array pointer), requires an extra int member "data_len" to describe the allocated data length.
The float(or double)'s decimal places of precision limited to 6:
// src/cJSON.c
#if 0
...
#else
/* HACK: Force use 6 decimal places of precision */
length = sprintf((char*)number_buffer, "%1.6g", d);
#endif
There's also a demo to show how to use this:
# Run demo
$ ./demo.sh
There're a few examples under examples/:
# Run all examples
$ ./example.sh
# Run single example
$ ./example.sh examples/01_struct_to_json/
There're also a few tests under tests/:
# Run all tests
$ ./test.sh
# Run single test
$ ./test.sh tests/01_basic/
- Jeffy Chen
<jeffy.chen@rock-chips.com>