You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
Activity
nlohmann commentedon Jan 21, 2017
No, the parser does this check. But you can catch the exception that is thrown in case of a parse error.
nlohmann commentedon Jan 24, 2017
@whitecloud-sas Does this help you?
whitecloud-sas commentedon 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 commentedon Jan 24, 2017
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.