-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
When exceptions disabled with JSON_NOEXCEPTION, lib just aborts without any message #2724
Comments
Switching off exceptions means that
If you want to parse JSON without exceptions, see https://json.nlohmann.me/features/parsing/parse_exceptions/. |
Is there even a reason to have the "allow_exception" codepaths that abort() when JSON_NOEXCEPTION is set? Seems like if a parser is just going to abort() without useful info, it would be better to strip that code and return false. There are many games and apps built with Emscripten where correct json parsing and errors are needed without exceptions enabled. Emulating exceptions in Emscripten is a huge slowdown to perf since they generate so much code, so they're typically disabled. And Win didn't have zero-cost runtime exceptions until Win64, but the cost is still in the codegen. |
The If you do want diagnostics without exceptions, then the last option (https://json.nlohmann.me/features/parsing/parse_exceptions/#user-defined-sax-interface) should be used. |
even when using the sax interface, this is a more a curiosity thing than an actual proposal, but why was smth like template<typename KeyType, typename ValueType>
bool get(const KeyType& key, ValueType& value);
int val;
if (!j.get("someValue", val))
{
std::cerr << "Failed to get value" << std::endl;
} (basically just returning false if the key wasnt found or the type is not valid) not considered? to access it safely with exceptions off you would have to do smth like int i = 0;
if (j.contains("someValue") && j.at("someValue").is_number_integer())
{
i = j.at("someValue").get<int>();
} |
The reason is that the library was designed with the assumption that exceptions are enabled. |
fair enough, was just curious. thanks for the answer i got one more question (didnt feel like making a new issue just for this question). with c++ 17's template<typename T>
bool json.is_type<T>(); or am i missing something? |
As we are using a |
Is there anything left for this issue to do? |
This would be really useful. For example in embedded environments, where you may not want to enable exceptions. Also while at it, would be nice to have a template for deserializing into objects which also skips any missing or invalid values. |
Directly aborting doesn't seem like a great idea at all since it gives little to no-information about what is happening in production, sure it is useful if we currently have a debugger and debug symbols enabled but on a production view it just causes big headaches when trying to understand what is magically closing your software. Not sure if anything has changed since 2021, but it would be great if there is another way to disable exceptions and still not call "abort()". |
What would you propose instead? There is no way to proceed in the situation where abort is called. |
I don't really have clear what raises this behavior of calling it so I am not that sure about what the possible ways are... The major problem comes when your software uses SEH Exceptions and not C++ ones and so it's not very well possible to manage them in a clean way. |
Yeah this whole thing is causing issues again. Idk, maybe let it write to some |
I thought about implementing it with the given macros, but uh, to "leave the current scope" one would need to call |
I would honestly go with a cleaner path by having the macro being just a boolean toggle and then implement all the code with an actual handler itself. |
What is the issue you have?
When using this json library, I'm seeing the release build either produce an "exception" at the middle or end of a perfectly valid json document. I'm still tracking that down, but noted that our app would just "abort code 6" without any useful information. That's usually associated with a buffer overrun, but in this case it's a parse error that calls JSON_THROW that then aborts without useful info about where the parse error occurs.
Please describe the steps to reproduce the issue.
Can you provide a small but working code example?
Proposing the following change to at least report the line, column, and other data from exception.what() before the abort. If you have some sort of log abstraction, then this could go to that. Now I actually see useful data that I can act on, but I suspect that the parser we have is not initializing something. Sometimes I get line 40 column 2, and other times it's the end of my document at line 82, 2.
What is the expected behavior?
Really with exceptions off, I expect the code to first print error information and then return false. I didn't write this wrapping code, but we still have allow_exceptions set on the parser even though we have JSON_NOEXCEPTIONS set, so the code goes into a series of JSON_THROW calls, but the other path just returns false without useful data.
And what is the actual behavior instead?
The app just does and abort without any useful info about where parsing failed.
Which compiler and operating system are you using?
Which version of the library did you use?
develop
branchIf you experience a compilation error: can you compile and run the unit tests?
The text was updated successfully, but these errors were encountered: