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

Repetitive data type while reading #833

Closed
lvkins opened this issue Nov 20, 2017 · 2 comments
Closed

Repetitive data type while reading #833

lvkins opened this issue Nov 20, 2017 · 2 comments

Comments

@lvkins
Copy link

lvkins commented Nov 20, 2017

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?

@nlohmann
Copy link
Owner

You can at least do this:

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

For the vector, there seems to be confusion. Are the first two lines what you want?

@lvkins
Copy link
Author

lvkins commented Nov 21, 2017

Yep, good for common data types.

Thanks 👍

@lvkins lvkins closed this as completed Nov 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants