I'd like to define a schema in JSON and generate C files to encode/decode streams that conform to the schema. A simplified example...
The JSON object...
var schema = {
version: 1,
objectA: {
key: 0,
type: "uint32_t",
defaultValue: 42
},
objectB: {
key: 1,
type: "int16_t",
defaultValue: -128
},
objectC: {
key: 2,
type: "double"
defaultValue: "3.1415926"
}
};
...would result in a C header file...
#define SCHEMA_VERSION (1)
typedef enum keys
{
key_objectA = 0,
key_objectB = 1,
key_objectC = 2,
KEY_COUNT
} keys_t;
typedef uint32_t objectA_t;
typedef int16_t objectB_t;
typedef double objectC_t;
typedef union univ_value
{
objectA_t objectA;
objectB_t objectB;
objectC_t objectC;
} value_t;
result_t encode(char * ostream, keys_t key, value_t * value);
result_t decode(char * istream, keys_t key, value_t * value);
...and a corresponding C source file (would contain the default values as an array, ommitted for brevity).
Is there a code generation library (ideally in Javascript) that I could use the generate the header and source file contents?
To be clear, I'm not looking to feed in the JSON directly; rather, ideally, the ability to do something (in Javascript) like:
var headerOutput;
headerOutput += dreamCGenerator.appendMacro("define", {
symbol: "SCHEMA_VERSION",
value: schema.version
});
headerOutput += dreamCGenerator.appendEnum(...);
// etc...