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

Accessing missing keys on const json object leads to assert #3183

Closed
5 tasks
dharam87 opened this issue Dec 8, 2021 · 1 comment
Closed
5 tasks

Accessing missing keys on const json object leads to assert #3183

dharam87 opened this issue Dec 8, 2021 · 1 comment
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@dharam87
Copy link

dharam87 commented Dec 8, 2021

What is the issue you have?

If I try to access a Json object values for a missing key - we expect it to throw an exception. However when the json object has const qualiifer, it asserts and does not throw an exception. Not sure whether it's by design or i'm missing anything obvious here.

Please describe the steps to reproduce the issue.

You can find the code-example here:
https://wandbox.org/permlink/KQ8vwhM4tbINbZsz

Can you provide a small but working code example?

#include <iostream>

# define JSON_DIAGNOSTICS 1
#include <nlohmann/json.hpp>

using json = nlohmann::json;
void check_json(const json& j) {
  try
    {
        /* this works !! */
        std::string fake_street = j["address"]["street"];
        std::cout<<fake_street<< '\n';

        /* accessing the missing key aborts - should not it throw the exception?? */
        int housenumber = j["address"]["housenumber"];
    }
    catch (json::exception& e)
    {
        std::cout << e.what() << '\n';
    }
}
int main()
{
    json j;
    j["address"]["street"] = "Fake Street";
    // omit one of the keys
    //j["address"]["housenumber"] = "12";
    check_json(j);
}

What is the expected behavior?

The const json objects should also throw the exceptions if they are searched for missing key values.

And what is the actual behavior instead?

The program aborts
prog.exe: /opt/wandbox/nlohmann-json/include/nlohmann/json.hpp:3898: const nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value_type& nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::operator[](T*) const [with T = const char; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::const_reference = const nlohmann::basic_json<>&]: Assertion m_value.object->find(key) != m_value.object->end()' failed.
`

Which compiler and operating system are you using?

  • Compiler: ___
  • Operating system: ___

Which version of the library did you use?

  • latest release version 3.10.4
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
@nlohmann nlohmann added solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) and removed kind: bug labels Dec 8, 2021
@nlohmann
Copy link
Owner

nlohmann commented Dec 8, 2021

No, this is documented behavior, see https://json.nlohmann.me/features/element_access/unchecked_access/

@nlohmann nlohmann closed this as completed Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

2 participants