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

Getter is setting the value to null if the key does not exist #754

Closed
adityamarella opened this issue Sep 27, 2017 · 6 comments
Closed

Getter is setting the value to null if the key does not exist #754

adityamarella opened this issue Sep 27, 2017 · 6 comments

Comments

@adityamarella
Copy link

Here is the sample code

#include <iostream>
#include <json.hpp>

int main(void) {
    nlohmann::json json;
    json["xxx"] = 1;
    auto y = json["yyy"];
    std::cout << json.dump() << std::endl;
    return 0;
}

The output is {"xxx":1,"yyy":null}.

The expected behavior is to throw std::out_of_range exception

@adityamarella
Copy link
Author

Closing this since the documentation already recommends at() vs. operator[], although this definitely is unexpected behavior.

In function from_json, use function at() to access the object values rather than operator[]. In case a key does not exists, at throws an exception that you can handle, whereas operator[] exhibits undefined behavior.

@nlohmann
Copy link
Owner

Note this behavior is exactly the one of std::map:

#include <map>
#include <iostream>

int main(int argc, char const *argv[])
{
    std::map<std::string, int> m;
    m["xxx"] = 1;
    auto y = m["yyy"];
    
    for (auto i = m.begin(); i != m.end(); ++i)
    {
        std::cout << i->first << " : " << i->second << std::endl;
    }
}

produces:

xxx : 1
yyy : 0

The note you cited refers to the case when we access a const object. Then, operator[] cannot add null values to the object when called with an unknown key and the semantics is undefined.

@adityamarella
Copy link
Author

Thanks for your comment. I wonder why std::map behaves this way. It is counterintuitive to set the value to null or 0 when the key is missing.

@gregmarr
Copy link
Contributor

Because if it didn't, then json["xxx"] = 1; wouldn't work. It creates an entry with the default value, and then sets it to 1.

@carlosaguilarmelchor
Copy link

Hi,
First of all thx for this library it is great.

I am puzzled by the answer given to this issue.

In the readme there are a ton of sexy examples using a json variable as a map. They look indeed great, but then you are saying here "if you follow the sexy syntax we claim to have in the readme then you will run into undefined behaviour when one day you'll ask for a non-existent key. You should not follow the sexy syntax we show and access elements with an .at() function" ???

Either the readme should show .at() examples or [ ] should be a little bit safer no ? Am I missing sth ?

@nlohmann
Copy link
Owner

There is already a note, see https://github.com/nlohmann/json/blob/develop/README.md#notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants