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

Syntax checking #1003

Closed
zkelo opened this issue Mar 9, 2018 · 9 comments
Closed

Syntax checking #1003

zkelo opened this issue Mar 9, 2018 · 9 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@zkelo
Copy link

zkelo commented Mar 9, 2018

I'm need to read some *.json file, but this file can have invalid json format. How to check for right json format before or while parsing?

@gregmarr
Copy link
Contributor

gregmarr commented Mar 9, 2018

You can now pass a boolean allow_exceptions to the parse functions. If it is false, no exceptions are thrown in case of a parse error. Instead, parsing is stopped at the first error and a JSON value of type "discarded" (check with is_discarded()) is returned.

@gregmarr
Copy link
Contributor

gregmarr commented Mar 9, 2018

FYI: It is now possible to call static bool accept(i) with any type i from which an input adapter can be constructed. Basically, i can have the same types as static basic_json parse(i). This function returns true iff the input contains a valid serialized JSON value. No exceptions are thrown in case of an error.

@gregmarr
Copy link
Contributor

gregmarr commented Mar 9, 2018

With b27d2ad I added a family static bool accept() functions that accept the same parameters as the parse() family (without the callback parameter) and return true if and only if the provided input is a valid JSON text. In case a parse error occurs, no exception is thrown.

#458

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 10, 2018
@zkelo
Copy link
Author

zkelo commented Mar 10, 2018

How to use this function?

@nlohmann
Copy link
Owner

Use accept instead of parse and take the bool it returns.

@zkelo zkelo closed this as completed Mar 10, 2018
@caiwan
Copy link

caiwan commented Jul 27, 2018

I have encountered the same issue, I could not catch anything that parse might throw. However I dug myself into the code to see the inheritence of the exception classes, and as far as I could get none of these exception declaration seemed worked, and passed through:

using Json = nlohmann::json;

//...

try {
    Json::parse(data);
}
    catch (const Json::parse_error &e) { //...
}
    catch (const Json::exception &e) { //...
}
    catch (const std::exception &e) { //...
}

I don't want to open another issue, due already there's a similar one, but are there any method to get at least an error message of the reason of the failure? It is mandatory for me, because the JSON document comes directly form a user input, and have to prompt it.

@nlohmann
Copy link
Owner

@caiwan Which version are you using? In https://nlohmann.github.io/json/classnlohmann_1_1basic__json_af1efc2468e6022be6e35fc2944cabe4d.html#af1efc2468e6022be6e35fc2944cabe4d is an example how to catch parse error exceptions.

@caiwan
Copy link

caiwan commented Jul 27, 2018

I've tried it with no const qualifiers, and still no luck.

I use the lastest stable v3.1.2 with Visual Studio 2015 (Version 14.0.25431.01 Update 3), within a Google Test testcase.

If I'll have a little extra time, I'd hack together a PoC project to reproduce the issue.

@nlohmann
Copy link
Owner

I'd be really interested, because we are using MSVC 2015 in our CI chain since we introduced the new exceptions in 3.0.0 and never heard of any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants