Skip to content

Unexpected behaviour of is_null #1112

Description

@phyz777

The following code produces unexpected output (which is, all four statements being printed):

#include <iostream>

#include "json.hpp"

int main()
{
    nlohmann::json j;

    if (j.empty()) {
        std::cout << "Object is empty" << std::endl;
    }

    if (j.is_null()) {
        std::cout << "Object is null" << std::endl;
    }

    nlohmann::json k;

    j["aminull"] = k;

    if (j["aminull"].empty()) {
        std::cout << "Object under key is empty" << std::endl;
    }

    if (j["aminull"].is_null()) {
        std::cout << "Object under key is null" << std::endl;
    }

    return 0;
}

I am not entirely sure whether or not this is the expected behaviour, but I think it would be more intuitive to have only the empty() conditions evaluate to true and the others to false?

Best wishes and thanks for the great work!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions