Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(experimental) json_parse_into_struct #5

Open
Kiyoshika opened this issue Jun 3, 2023 · 0 comments
Open

(experimental) json_parse_into_struct #5

Kiyoshika opened this issue Jun 3, 2023 · 0 comments

Comments

@Kiyoshika
Copy link
Owner

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant