Skip to content

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

Description

@adityamarella

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

Activity

  1. adityamarella commented on Sep 27, 2017

    @adityamarella
    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.

  2. nlohmann commented on Sep 27, 2017

    @nlohmann
    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.

  3. adityamarella commented on Sep 27, 2017

    @adityamarella
    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.

  4. gregmarr commented on Sep 27, 2017

    @gregmarr
    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.

  5. carlosaguilarmelchor commented on Aug 11, 2018

    @carlosaguilarmelchor

    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 ?

  6. nlohmann commented on Aug 12, 2018

    @nlohmann
    Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions