-
-
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
How to deal with missing keys in from_json
?
#538
Comments
Not sure if this makes any sense (as I'm not a C++ hacker at all), but for the time being I added a This results in a
|
Hi, I think that in your last example, the non-const The const I believe there was an issue about those distinct behaviours, we could re-open it and see if we need to change that. |
Indeed the const operator[] is the issue here. Please use |
Ah yes, thanks Niels. I didn't quite get Thèos reply ... So now my
And I get a nice:
That's perfect! Thanks a lot for your help. Apart from that, this issue might be closed. |
Thanks for the feedback. I shall edit the README accordingly. |
d07596a added a comment to file README to use |
I am following this example: https://github.com/nlohmann/json#basic-usage
When there is a key missing in the json, inside
from_json
, I expected to get an exception, but I just get a core_dump.I would like to avoid writing a lot of boilerplate to check for the existence of keys and type of values myself.
I am mostly a python guy and am used to the try..catch style of doing things. This awesome library looked like it supports this style in this case.
My own type looks like this, lives inside namespace
Lid
:when the json is complete,
from_json
nicely works.When I leave out a necessary key, e.g.
motor_id
like this, I expected to get astd::domain_error
or so.So I tested this:
But I get a core dump instead:
Just to be sure if exception work at all. When trying to access the missing key outside
from_json
I get a nice exception. So this:Leads to this:
I tried putting the same
try{ }catch(...){}
intofrom_json
, but I still got a core dump and no nice exception I can catch.So after this long text comes the question: What is your canonical way of dealing with missing keys inside
from_json
Thanks a lot for the awesome work.
platform Ubuntu 16.04 LTS
gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1)
Using the single header
json.hpp v.2.1.1
The text was updated successfully, but these errors were encountered: