Skip to content

Repetitive data type while reading #833

Description

@lvkins

Is this possible to get a value with auto data type detection?

Consider following:

struct Test {
       std::string value;
       int32_t index;
       std::vector<int32_t> acro;
}

To parse these, we need to do the following:

void from_json(const json& j, Files::Chapters::Test& p) {
    p.value = j.at("value").get<std::string>();
    p.index = j.at("index").get<int32_t>();
    p.acro = j.at("acro").get<std::vector<int32_t>>();
}

Thats quite a boilerplate.

We can also do this:

void from_json(const json& j, Files::Chapters::Test& p) {
    p.value = j.at("value").get<typeof(p.value)>();
    p.index = j.at("index").get<typeof(p.index)>();
    p.acro = j.at("acro").get<typeof(p.acro)>();
}

As you can see, this is repetitive thus redudant. Can't we simply get value with the data type of a object we assign to?

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

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions