Skip to content

Weird behaviour of contains with json_pointer #1815

Closed
@kpeeters

Description

The following code tests the presence of a particular attribute using a json pointer, in two different ways. It prints '1' for the first (which is correct), but '0' for the second line.

#include "json.hpp"
#include <fstream>
#include <iostream>

int main(int, char **)
    {
    nlohmann::json j = {
        {"pi", 3.141},
        {"happy", true},
        {"name", "Niels"},
        {"nothing", nullptr},
        {"answer", {
               {"everything", 42}
        }}
    };

    nlohmann::json::json_pointer ptr("/name");
    std::cout << j.contains( nlohmann::json::json_pointer("/name") ) << std::endl;
    std::cout << j.contains( ptr ) << std::endl;
    }

Any ideas? I would have guessed these are equivalent, but obviously they are not. Tested with g++ 7.4 and clang++ 8.0 using json.hpp 3.7.0.

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: 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