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

json::parse return value and errors #1595

Closed
lolriven opened this issue May 2, 2019 · 2 comments
Closed

json::parse return value and errors #1595

lolriven opened this issue May 2, 2019 · 2 comments

Comments

@lolriven
Copy link

lolriven commented May 2, 2019

I was reading through your examples I noticed your parse error example

    try {
        json::parse(message);
    } catch (json::parse_error& e) {
        return;
    }

First I receive a compiler warning saying discarding return value with a function declared with 'nodiscard' attribute and second I wan to be able to parse the message without wrapping the entire rest of my function in the exception handler. For instance I can just parse it twice like so.

    try {
        json::parse(message);
    } catch (json::parse_error& e) {
        return;
    }
    auto packet = json::parse(message);

Once to check if there's an error and the second to store the value in packet. Then I can use the packet structure, but that isn't very efficient because I'm parsing the message twice. I also can't store the return value in an auto type outside the block scope and assign it inside the block because auto wouldn't work that way. Essentially what I'm asking is, what does json::parse return so I can declare a variable for it outside the try catch and assign it inside the try catch then check if the assigned variable is valid.

@gregmarr
Copy link
Contributor

gregmarr commented May 2, 2019

It returns json. You can check it for is_null() outside the block.

@lolriven
Copy link
Author

lolriven commented May 2, 2019

Ah okay! Thank you. I feel so silly now.
This works fine.

    json packet;
    try {
        packet = json::parse(message);
    } catch (json::parse_error& e) {
        return;
    }

@lolriven lolriven closed this as completed May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants