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

Handle invalid string in Json file #937

Closed
arunpkio opened this issue Jan 25, 2018 · 3 comments
Closed

Handle invalid string in Json file #937

arunpkio opened this issue Jan 25, 2018 · 3 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@arunpkio
Copy link

Bug Report

  • I am using the following code load a JSON file. I want to handle the error cases if the JSON is wrong or corrupted. How do I handle or get to know if the JSON I am trying to load is with correct syntax or not.
    std::ifstream presetfile(presetName); json jsobject; jsobject << presetfile;

  • Use a simple JSON file and add any character at the beginning of the JSON like "test" in the first line. Then try to load the file using the above code we can see a crash.

  • I would like to see the information that the JSON that I am trying to load is corrupted/not in the expected format with a line where the issue is[optionally].

  • Is simply crashes.

  • I use Clang

  • released

  • No compile error

@nlohmann
Copy link
Owner

In case of a parse error, an exception is thrown. You should try:

std::ifstream presetfile(presetName);
json jsobject; 
try { jsobject << presetfile; }
catch (json::exception &e) { std::err << e.what(); << std::endl; }

@nlohmann
Copy link
Owner

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jan 25, 2018
@arunpkio
Copy link
Author

Thanks. I was using an old version and now updated to 3.0.1 and was able to handle the exception.

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

No branches or pull requests

2 participants