From d41321159b1fa0ecffa380e743132e1a47e6347a Mon Sep 17 00:00:00 2001 From: Timothy Prepscius Date: Thu, 25 Oct 2018 08:51:29 -0400 Subject: [PATCH] runs make amalgamate --- include/nlohmann/detail/json_pointer.hpp | 14 +-- include/nlohmann/json.hpp | 4 +- single_include/nlohmann/json.hpp | 115 +++++++++++++++++++++++ 3 files changed, 124 insertions(+), 9 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 1165fb9e6..ad746761b 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -409,17 +409,17 @@ class json_pointer // if we get here, it means that no exception was thrown, so therefore the value specified to // erase exists - if (previous) + if (previous) { - // if previous exists, it means that the specified json value was not a root, so, we use the previous - // object and erase from it - previous->erase(reference_tokens.back()); + // if previous exists, it means that the specified json value was not a root, so, we use the previous + // object and erase from it + previous->erase(reference_tokens.back()); } else { - // else it means the root was referenced, so we nullify it. - if (ptr != nullptr) - *ptr = {}; + // else it means the root was referenced, so we nullify it. + if (ptr != nullptr) + *ptr = {}; } } diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 7bcb97287..46f88a3a6 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7085,8 +7085,8 @@ class basic_json { return ptr.get_checked(this); } - - + + /*! @brief remove element from a JSON tree given a pointer diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1de78dfc7..869a64968 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11152,6 +11152,82 @@ class json_pointer return *ptr; } + /*! + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index was not a number + @throw out_of_range.402 if the array index '-' is used + @throw out_of_range.404 if the JSON pointer can not be resolved + */ + void erase_checked(BasicJsonType* ptr) const + { + BasicJsonType* previous = nullptr; + + using size_type = typename BasicJsonType::size_type; + 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 we get here, it means that no exception was thrown, so therefore the value specified to + // erase exists + + if (previous) + { + // if previous exists, it means that the specified json value was not a root, so, we use the previous + // object and erase from it + previous->erase(reference_tokens.back()); + } + else + { + // else it means the root was referenced, so we nullify it. + if (ptr != nullptr) + *ptr = {}; + } + } + /*! @brief return a const reference to the pointed to value @@ -18570,6 +18646,45 @@ class basic_json return ptr.get_checked(this); } + + /*! + @brief remove element from a JSON tree given a pointer + + Removes elements from a JSON tree with the pointer value @ptr + + @param[in] pointer value of the elements to remove + + @post References and iterators to the erased elements are invalidated. + Other references and iterators are not affected. + + @throw parse_error.106 if an array index in the passed JSON pointer @a ptr + begins with '0'. See example below. + + @throw parse_error.109 if an array index in the passed JSON pointer @a ptr + is not a number. See example below. + + @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr + is out of range. See example below. + + @throw out_of_range.402 if the array index '-' is used in the passed JSON + pointer @a ptr. As `at` provides checked access (and no elements are + implicitly inserted), the index '-' is always invalid. See example below. + + @throw out_of_range.403 if the JSON pointer describes a key of an object + which cannot be found. See example below. + + @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved. + See example below. + + @complexity + + @since version + */ + void erase(const json_pointer& ptr) + { + ptr.erase_checked(this); + } + /*! @brief return flattened JSON value