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

Documentation on operator[] behavior with missing keys #1855

Closed
sachabest opened this issue Nov 27, 2019 · 3 comments
Closed

Documentation on operator[] behavior with missing keys #1855

sachabest opened this issue Nov 27, 2019 · 3 comments
Labels
documentation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@sachabest
Copy link

  • What is the issue you have?

Right now operator[key] is documented to create a null key in the called upon json object should key not exist in that object, if the source object is NOT const. If the object is const, operator[] returns the equivalent of *(map.cend()).

I think the documentation around this could be improved, as I would bet users are not fully aware of the behavioral differences depending on the constness of the source object.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
#include <nlohmann/json.hpp>

int main()
{
    const nlohmann::json j = {{ "key1", "value1" }}; // { "key1": "value1" }
    const nlohmann::json& missingKey = j["key2"];
    // missingKey here is UB
}
  • What is the expected behavior?

I think the current behavior is fine, documentation could be improved to reflect it.

  • And what is the actual behavior instead?

N/A

GCC 5 + RHEL7 as well as VC141/151 Win10 x64

  • Did you use a released version of the library or the version from the develop branch?

Latest major version

@sachabest sachabest changed the title Documentation operator[] behavior with missing keys Documentation on operator[] behavior with missing keys Nov 27, 2019
@nlohmann
Copy link
Owner

nlohmann commented Dec 2, 2019

The function (https://github.com/nlohmann/json/blob/develop/include/nlohmann/json.hpp#L3280) has a @warning documentation:

    @warning If the element with key @a key does not exist, the behavior is
    undefined.

Any proposal how to improve this?

@sonulohani
Copy link
Contributor

Documentation seems right. While accessing any key through operator [], we need to make sure that given key is in range, otherwise it will result in undefined behaviour and can also crash application.

@stale
Copy link

stale bot commented Jan 7, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 7, 2020
@stale stale bot closed this as completed Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants