Skip to content

Possible to do implicit conversion of number to string? #2249

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

You must be logged in to vote

If you don't know the types of the values coming in, then you do need to check the types, as you've written. If you're going to do this a lot, create a helper function for this.

std::string get_number_as_string(json const &j) {
    if(j.is_string()) {
       return j.get<std::string>();
    }

    return std::to_string(j.get<int>());
}

auto a = get_number_as_string(j["a"]);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jezeke
Comment options

Answer selected by jezeke
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