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

Document return value of parse() when allow_exceptions == false and parsing fails #1405

Closed
ibc opened this issue Dec 24, 2018 · 2 comments
Closed

Comments

@ibc
Copy link

ibc commented Dec 24, 2018

The parse() function says:

allow_exceptions: whether to throw exceptions in case of a parse error (optional, true by default)

What happens if allow_exceptions is set to false and parsing fails (due to incorrect JSON string)? How is the returned basic_json? Is it something that can be checked with json::type() == json::value_t::null?

@nlohmann
Copy link
Owner

You are right - the return value is not properly documented in this case. If allow_exceptions is false, then a discarded value is returned that can be checked with is_discarded(). Here is an example:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {
    json j = json::parse("{]", nullptr, false);
    std::cout << "failure: " << std::boolalpha << j.is_discarded() << std::endl;
}

Output:

failure: true

I shall update the documentation accordingly.

@nlohmann nlohmann self-assigned this Dec 24, 2018
@ibc
Copy link
Author

ibc commented Dec 24, 2018

Clear, thanks.

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