diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index fce8001a5..e183ff968 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -19,6 +19,9 @@ class json_pointer friend class basic_json; public: + typedef std::vector::const_iterator const_iterator; + typedef std::vector::const_reverse_iterator const_reverse_iterator; + /*! @brief create JSON pointer @@ -75,6 +78,38 @@ class json_pointer return to_string(); } + const_iterator cbegin() const + { + return reference_tokens.cbegin(); + } + + const_iterator cend() const + { + return reference_tokens.cend(); + } + + const_reverse_iterator crbegin() const + { + return reference_tokens.crbegin(); + } + + const_reverse_iterator crend() const + { + return reference_tokens.crend(); + } + + json_pointer appended(std::string const& next) const + { + json_pointer copy(*this); + copy.reference_tokens.push_back(next); + return copy; + } + + json_pointer appended(size_t next) const + { + return appended(std::to_string(next)); + } + /*! @param[in] s reference token to be converted into an array index diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index aef95084a..22c58bc18 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10585,6 +10585,9 @@ class json_pointer friend class basic_json; public: + typedef std::vector::const_iterator const_iterator; + typedef std::vector::const_reverse_iterator const_reverse_iterator; + /*! @brief create JSON pointer @@ -10641,6 +10644,38 @@ class json_pointer return to_string(); } + const_iterator cbegin() const + { + return reference_tokens.cbegin(); + } + + const_iterator cend() const + { + return reference_tokens.cend(); + } + + const_reverse_iterator crbegin() const + { + return reference_tokens.crbegin(); + } + + const_reverse_iterator crend() const + { + return reference_tokens.crend(); + } + + json_pointer appended(std::string const& next) const + { + json_pointer copy(*this); + copy.reference_tokens.push_back(next); + return copy; + } + + json_pointer appended(size_t next) const + { + return appended(std::to_string(next)); + } + /*! @param[in] s reference token to be converted into an array index diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index ec49e1147..0ecfcb47a 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -441,6 +441,47 @@ TEST_CASE("JSON pointers") } } + SECTION("iterator access") + { + SECTION("forward iterator access") + { + auto ptr = "/foo/bar/c%d"_json_pointer; + std::vector components(ptr.cbegin(), ptr.cend()); + CHECK(components.size() == 3u); + CHECK(components[0] == "foo"); + CHECK(components[1] == "bar"); + CHECK(components[2] == "c%d"); + } + + SECTION("reverse iterator access") + { + auto ptr = "/foo/bar/c%d"_json_pointer; + std::vector rcomponents(ptr.crbegin(), ptr.crend()); + CHECK(rcomponents.size() == 3u); + CHECK(rcomponents[2] == "foo"); + CHECK(rcomponents[1] == "bar"); + CHECK(rcomponents[0] == "c%d"); + } + } + + SECTION("appending") + { + SECTION("can append to empty string pointer") + { + auto ptr = ""_json_pointer; + CHECK(ptr.to_string() == ""); + + ptr = ptr.appended("foo"); + CHECK(ptr.to_string() == "/foo"); + + ptr = ptr.appended("bar"); + CHECK(ptr.to_string() == "/foo/bar"); + + ptr = ptr.appended(10); + CHECK(ptr.to_string() == "/foo/bar/10"); + } + } + SECTION("conversion") { SECTION("array")