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

avoid malformed json #433

Closed
whitecloud-sas opened this issue Jan 21, 2017 · 4 comments
Closed

avoid malformed json #433

whitecloud-sas opened this issue Jan 21, 2017 · 4 comments

Comments

@whitecloud-sas
Copy link

Hello!

if I deliver a malformed string like {"key1":"val1", "key.... it crashes at json j_complete = json::parse(v_string); line, exists something to check this before parse? thanks!

@nlohmann
Copy link
Owner

No, the parser does this check. But you can catch the exception that is thrown in case of a parse error.

@nlohmann
Copy link
Owner

@whitecloud-sas Does this help you?

@whitecloud-sas
Copy link
Author

whitecloud-sas commented Jan 24, 2017

Yes, this is what I did:

try {
json object;
auto text = str;
std::stringstream ss;
ss << text;
json j_complete = json::parse(ss);
object = j_complete;
auto key = object.find("key");
if (*key== "value") {
//do something
}
} catch (const std::invalid_argument&) {
// parsing a JSON serialization must not fail
printf("JSON malformed %s\n", str.c_str());
}

Thanks @nlohmann

@nlohmann
Copy link
Owner

Note with #301 we shall add diagnosis information to the exceptions so you can provide the user the location in the JSON text that led to the error.

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