-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
C doesn't support reflection, but we could use struct packing tricks to try and map a JSON object to a user-defined struct.
The main downside would be to organize the fields EXACTLY the same as the JSON. The names wouldn't matter since we can't actually read them.
E.g., we would have to do something like
// this specific packed attribute is GCC specific and may be different
// for other compilers
struct __attribute__((__packed__)) person_t
{
char* name;
int32_t age;
};
char* json_string = "{\"name\": \"John\", \"age\": 24}";
struct person_t person;
json_create_struct_from_string(&person, json_string);
person.name; // John
person.age; // 24