diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 3521b889db..033625bc4c 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -97,7 +97,6 @@ class json_pointer return res; } - private: /*! @brief remove and return last reference pointer @throw out_of_range.405 if JSON pointer has no parent @@ -114,6 +113,16 @@ class json_pointer return last; } + /*! + @brief remove and return last reference pointer + @throw out_of_range.405 if JSON pointer has no parent + */ + void push_back(const std::string& tok) + { + reference_tokens.push_back(tok); + } + + private: /// return whether pointer points to the root document bool is_root() const noexcept { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 70b6c8a855..e3b54c88f6 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11885,7 +11885,6 @@ class json_pointer return res; } - private: /*! @brief remove and return last reference pointer @throw out_of_range.405 if JSON pointer has no parent @@ -11902,6 +11901,16 @@ class json_pointer return last; } + /*! + @brief remove and return last reference pointer + @throw out_of_range.405 if JSON pointer has no parent + */ + void push_back(const std::string& tok) + { + reference_tokens.push_back(tok); + } + + private: /// return whether pointer points to the root document bool is_root() const noexcept { diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index ac9cd71e2a..1bcd3938a7 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -459,4 +459,58 @@ TEST_CASE("JSON pointers") CHECK(j.is_object()); } } + + SECTION("push and pop") + { + const json j = + { + {"", "Hello"}, + {"pi", 3.141}, + {"happy", true}, + {"name", "Niels"}, + {"nothing", nullptr}, + { + "answer", { + {"everything", 42} + } + }, + {"list", {1, 0, 2}}, + { + "object", { + {"currency", "USD"}, + {"value", 42.99}, + {"", "empty string"}, + {"/", "slash"}, + {"~", "tilde"}, + {"~1", "tilde1"} + } + } + }; + + // empty json_pointer returns the root JSON-object + auto ptr = ""_json_pointer; + CHECK(j[ptr] == j); + + // simple field access + ptr.push_back("pi"); + CHECK(j[ptr] == j["pi"]); + + ptr.pop_back(); + CHECK(j[ptr] == j); + + // object and children access + ptr.push_back("answer"); + ptr.push_back("everything"); + CHECK(j[ptr] == j["answer"]["everything"]); + + ptr.pop_back(); + ptr.pop_back(); + CHECK(j[ptr] == j); + + // push key which has to be encoded + ptr.push_back("object"); + ptr.push_back("/"); + CHECK(j[ptr] == j["object"]["/"]); + CHECK(ptr.to_string() == "/object/~1"); + } }