-
-
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
assertion on runtime causes program to stop when accessing const json with missing key #2392
Comments
Only |
Well is there a reason why or just "because"? |
This is not a bug, but a feature. Unchecked access is apparently faster than bounded-checked access, and accessing a nonexisting element of a const object is undefined behavior. This is described in https://nlohmann.github.io/json/api/basic_json/operator%5B%5D/. A more detailed description can be found in https://nlohmann.github.io/json/features/element_access/unchecked_access/ . Please let me know if further information is needed. |
See #2111 for discussion of a possible solution to this. |
Thank you for clarifying this. I know understand why the function behaves like this. Closing this issue. |
Hi,
i just stumbled upon something i think might be a bug but am unsure about.
when accessing a
const nlohmann::json &j
object and using a missing key the library will fail with an assertion which causes the whole program to stop (assertion not catchable).Here is an example how to reproduce it.
i know i should check for a keys existence with
j.count(<key>)
but shouldn't the access on a missing key throw an exception of typejson::out_of_range
(or similar) so it is catchable instead of asserting and therefore aborting the whole program?The error it produces is:
Which compiler and operating system are you using?
Which version of the library did you use?
current develop (@1bcabd9e83ebc2d04b5767c7f51bf385e80398d6)
develop
branchThe text was updated successfully, but these errors were encountered: