diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 3dccd5875..cb1f2251e 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3893,12 +3893,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief overload for a default value of type const char* @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const */ +#if defined(JSON_HAS_CPP_17) // avoid creating a string_t value from default_value + template < class KeyType, typename detail::enable_if_t < + detail::is_usable_as_key_type::value, int> = 0> + std::string_view value(const KeyType& key, const char* default_value) const + { + return value(key, std::string_view(default_value)); + } +#else template < class KeyType, typename detail::enable_if_t < detail::is_usable_as_key_type::value, int> = 0> string_t value(const KeyType& key, const char* default_value) const { return value(key, string_t(default_value)); } +#endif /*! @brief access specified object element via JSON Pointer with default value diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2e7b193fc..9ff85b2d8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -21460,12 +21460,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief overload for a default value of type const char* @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const */ +#if defined(JSON_HAS_CPP_17) // avoid creating a string_t value from default_value + template < class KeyType, typename detail::enable_if_t < + detail::is_usable_as_key_type::value, int> = 0> + std::string_view value(const KeyType& key, const char* default_value) const + { + return value(key, std::string_view(default_value)); + } +#else template < class KeyType, typename detail::enable_if_t < detail::is_usable_as_key_type::value, int> = 0> string_t value(const KeyType& key, const char* default_value) const { return value(key, string_t(default_value)); } +#endif /*! @brief access specified object element via JSON Pointer with default value