Skip to content

Unexpected behaviour of is_null - Part II #1167

Description

@phyz777

The following code produces a core dump with this error message: Assertion `m_value.object->find(key) != m_value.object->end()' failed

int main() {
    nlohmann::json j;
    j["subobject"] = {{"key1", "value1"}, {"key2", "value2"}};

    const nlohmann::json &subobject = j["subobject"];

    if (!subobject["key1"].is_null()) {
        std::cout << "CHECK" << std::endl;
    } else {
        std::cout << "FAIL" << std::endl;
    }

    if (!subobject["key3"].is_null()) {
        std::cout << "CHECK" << std::endl;
    } else {
        std::cout << "FAIL" << std::endl;
    }

    return 0;
}

This is because the check inserts {"key3", null} into the object.

I'm not sure whether this behaviour is intentionally, it might at least become a pitfall for some, so I wanted to bring this to your attention.

Activity

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

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions