Skip to content

(experimental) json_parse_into_struct #5

Description

@Kiyoshika

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions