commit 75125446d21f2119fc0e2d26b5024e7a1677ec19 Author: Timothy Prepscius Date: Fri Oct 12 14:38:37 2018 -0400 changes for json_pointer erasing something, and fix for json_pointer with just / diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index fce8001..4f83dab 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -471,6 +471,63 @@ class json_pointer return *ptr; } + // TJP + void erase_checked(BasicJsonType* ptr) const + { + using size_type = typename BasicJsonType::size_type; + BasicJsonType* previous = nullptr; + for (const auto& reference_token : reference_tokens) + { + previous = ptr; + + switch (ptr->m_type) + { + case detail::value_t::object: + { + // note: at performs range check + ptr = &ptr->at(reference_token); + break; + } + + case detail::value_t::array: + { + if (JSON_UNLIKELY(reference_token == "-")) + { + // "-" always fails the range check + JSON_THROW(detail::out_of_range::create(402, + "array index '-' (" + std::to_string(ptr->m_value.array->size()) + + ") is out of range")); + } + + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + { + JSON_THROW(detail::parse_error::create(106, 0, + "array index '" + reference_token + + "' must not begin with '0'")); + } + + // note: at performs range check + JSON_TRY + { + ptr = &ptr->at(static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + break; + } + + default: + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + } + } + + if (previous) + previous->erase(reference_tokens.back()); + } + /*! @brief split the string input to reference tokens @@ -497,6 +554,10 @@ class json_pointer "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'")); } + + // tjp + if (reference_string.size() == 1) + return result; // extract the reference tokens: // - slash: position of the last read slash (or end of string) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ee78c1c..049056c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -6943,6 +6943,12 @@ class basic_json { return ptr.get_unchecked(this); } + + // TJP + void erase(const json_pointer& ptr) + { + ptr.erase_checked(this); + } /*! @brief access specified element via JSON Pointer