Skip to content

value_to<> method #2442

Answered by gregmarr
jjcasmar asked this question in Q&A
Discussion options

You must be logged in to vote

There is one that does the "so you dont have to specify the type to which is converted"

json o;
double d;
o.at(key).get_to(d);

and one which does the "If key doesn't exist, could leave the result as it is or return the return of decltype(d){}"

json o;
double d = o.value<double>(key, {});

but no combination of the two as far as I know. You could do this:

json o;
double d = {};

if (o.find(key) != o.end())
    o.at(key).get_to(d);

and you could probably write your own template to do that:

template<typename K, typename V>
json value_to(json const &o, K&& key, V& value) {
    if (o.find(key) != o.end()) {
        o.at(key).get_to(value);
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nlohmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants