glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.47k stars 1.08k forks source link

Feature: Add typedef structs as an option (cJSON) #2346

Open pseudotronics opened 1 year ago

pseudotronics commented 1 year ago

Struct definitions are restrictive and a PITA.

There should be a flag to change the code generation to use typedefs instead of structs. I may try to tackle adding this in myself, but I am not much of a typescript programmer.

So:

struct DemoAppState {
    bool * green_led;
    bool * red_led;
    bool * user_button;
};

and

struct DemoAppState * cJSON_ParseDemoAppState(const char * s);
struct DemoAppState * cJSON_GetDemoAppStateValue(const cJSON * j);
cJSON * cJSON_CreateDemoAppState(const struct DemoAppState * x);
char * cJSON_PrintDemoAppState(const struct DemoAppState * x);
void cJSON_DeleteDemoAppState(struct DemoAppState * x);

would become:

typedef struct {
    bool * green_led;
    bool * red_led;
    bool * user_button;
} DemoAppState;

and

DemoAppState * cJSON_ParseDemoAppState(const char * s);
DemoAppState * cJSON_GetDemoAppStateValue(const cJSON * j);
cJSON * cJSON_CreateDemoAppState(const DemoAppState * x);
char * cJSON_PrintDemoAppState(const DemoAppState * x);
void cJSON_DeleteDemoAppState(DemoAppState * x);

All other references would need to be updated within the body of the functions also.

mundusnine commented 1 year ago

This can be closed as doing --typedef-alias add-typedef was implemented by you ;)

https://github.com/glideapps/quicktype/blob/9b570a73a896306778940c793c0037a38815304a/packages/quicktype-core/src/language/CJSON.ts#L85