From 268600e63be857ab6ada8504ec7a6247badd2d8f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 29 Mar 2021 12:18:33 +0200 Subject: [PATCH] :ok_hand: apply review comments --- include/nlohmann/json.hpp | 23 +++++++++++++++++++---- single_include/nlohmann/json.hpp | 23 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 0d8d78558..b49576c8a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3725,12 +3725,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - if (auto it = m_value.object->find(key); it != m_value.object->end()) + auto it = m_value.object->find(key); + if (it != m_value.object->end()) { return it->second; } - return set_parent(m_value.object->operator[](json::object_t::key_type(key))); + auto result = m_value.object->emplace(key, nullptr); + return set_parent(result.first->second); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); @@ -4421,9 +4423,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } #if defined(JSON_HAS_CPP_17) - size_type erase(std::string_view key) + size_type erase(const std::string_view& key) { - return erase(typename object_t::key_type(key.data(), key.size())); + // this erase only works for objects + if (JSON_HEDLEY_UNLIKELY(!is_object())) + { + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); + } + + const auto it = m_value.object->find(key); + if (it != m_value.object->end()) + { + m_value.object->erase(it); + return 1; + } + + return 0; } #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 89c436202..58552e2f7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20614,12 +20614,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - if (auto it = m_value.object->find(key); it != m_value.object->end()) + auto it = m_value.object->find(key); + if (it != m_value.object->end()) { return it->second; } - return set_parent(m_value.object->operator[](json::object_t::key_type(key))); + auto result = m_value.object->emplace(key, nullptr); + return set_parent(result.first->second); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); @@ -21310,9 +21312,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } #if defined(JSON_HAS_CPP_17) - size_type erase(std::string_view key) + size_type erase(const std::string_view& key) { - return erase(typename object_t::key_type(key.data(), key.size())); + // this erase only works for objects + if (JSON_HEDLEY_UNLIKELY(!is_object())) + { + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); + } + + const auto it = m_value.object->find(key); + if (it != m_value.object->end()) + { + m_value.object->erase(it); + return 1; + } + + return 0; } #endif