From b18d97df20a983e6b137a2e404a50124d0a636e9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 2 Jan 2022 13:57:04 +0100 Subject: [PATCH] :rewind: undo commit d8db435c1e1e60ebd93f9e5036437f9b8cc478e4 --- include/nlohmann/json.hpp | 34 ++++---------------------------- single_include/nlohmann/json.hpp | 34 ++++---------------------------- 2 files changed, 8 insertions(+), 60 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b8704bdb1..4bf674453 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2111,46 +2111,20 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } - template - using Alias = T; - /// @brief access specified object element /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/ template - reference operator[](Alias const& key) + reference operator[](T * (&key)[n]) { - // implicitly convert null value to an empty object - if (is_null()) - { - m_type = value_t::object; - m_value.object = create(); - assert_invariant(); - } - - // operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - 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)); + return operator[](static_cast(key)); } /// @brief access specified object element /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/ template - const_reference operator[](Alias const& key) const + const_reference operator[](T * (&key)[n]) const { - // operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - auto it = m_value.object->find(key); - JSON_ASSERT(it != m_value.object->end()); - return it->second; - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); + return operator[](static_cast(key)); } /// @brief access specified object element with default value diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9818b579d..0ce534a1e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19300,46 +19300,20 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } - template - using Alias = T; - /// @brief access specified object element /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/ template - reference operator[](Alias const& key) + reference operator[](T * (&key)[n]) { - // implicitly convert null value to an empty object - if (is_null()) - { - m_type = value_t::object; - m_value.object = create(); - assert_invariant(); - } - - // operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - 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)); + return operator[](static_cast(key)); } /// @brief access specified object element /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/ template - const_reference operator[](Alias const& key) const + const_reference operator[](T * (&key)[n]) const { - // operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - auto it = m_value.object->find(key); - JSON_ASSERT(it != m_value.object->end()); - return it->second; - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); + return operator[](static_cast(key)); } /// @brief access specified object element with default value