unify types for object keys

parent 6019b5d3
...@@ -3468,6 +3468,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3468,6 +3468,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
Returns a reference to the element at with specified key @a key, with Returns a reference to the element at with specified key @a key, with
bounds checking. bounds checking.
@tparam KeyT a type convertible to an object key or a `std::string_view`
@param[in] key key of the element to access @param[in] key key of the element to access
@return reference to the element at key @a key @return reference to the element at key @a key
...@@ -3492,12 +3493,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3492,12 +3493,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
written using `at()`. It also demonstrates the different exceptions that written using `at()`. It also demonstrates the different exceptions that
can be thrown.,at__object_t_key_type} can be thrown.,at__object_t_key_type}
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
reference at(const KeyType& key) reference at(KeyT && key)
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_UNLIKELY(!is_object())) if (JSON_HEDLEY_UNLIKELY(!is_object()))
...@@ -3505,7 +3506,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3505,7 +3506,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
} }
auto it = m_value.object->find(key); auto it = m_value.object->find(std::forward<KeyT>(key));
if (it == m_value.object->end()) if (it == m_value.object->end())
{ {
JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this)); JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this));
...@@ -3520,6 +3521,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3520,6 +3521,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
with bounds checking. with bounds checking.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return const reference to the element at key @a key @return const reference to the element at key @a key
...@@ -3543,12 +3545,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3543,12 +3545,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
`at()`. It also demonstrates the different exceptions that can be thrown., `at()`. It also demonstrates the different exceptions that can be thrown.,
at__object_t_key_type_const} at__object_t_key_type_const}
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
const_reference at(const KeyType& key) const const_reference at(KeyT && key) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_UNLIKELY(!is_object())) if (JSON_HEDLEY_UNLIKELY(!is_object()))
...@@ -3556,7 +3558,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3556,7 +3558,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
} }
auto it = m_value.object->find(key); auto it = m_value.object->find(std::forward<KeyT>(key));
if (it == m_value.object->end()) if (it == m_value.object->end())
{ {
JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this)); JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this));
...@@ -3663,6 +3665,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3663,6 +3665,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
In case the value was `null` before, it is converted to an object. In case the value was `null` before, it is converted to an object.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return reference to the element at key @a key @return reference to the element at key @a key
...@@ -3680,12 +3683,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3680,12 +3683,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
reference operator[](const KeyType& key) reference operator[](KeyT && key)
{ {
// implicitly convert null value to an empty object // implicitly convert null value to an empty object
if (is_null()) if (is_null())
...@@ -3698,7 +3701,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3698,7 +3701,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// operator[] only works for objects // operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
return set_parent(m_value.object->operator[](key)); return set_parent(m_value.object->operator[](std::forward<KeyT>(key)));
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
...@@ -3714,6 +3717,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3714,6 +3717,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
undefined. undefined.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return const reference to the element at key @a key @return const reference to the element at key @a key
...@@ -3734,17 +3738,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -3734,17 +3738,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
const_reference operator[](const KeyType& key) const const_reference operator[](KeyT && key) const
{ {
// const operator[] only works for objects // const operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
JSON_ASSERT(m_value.object->find(key) != m_value.object->end()); JSON_ASSERT(m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end());
return m_value.object->find(key)->second; return m_value.object->find(key)->second;
} }
...@@ -4328,6 +4332,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -4328,6 +4332,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
Removes elements from a JSON object with the key value @a key. Removes elements from a JSON object with the key value @a key.
@param[in] key value of the elements to remove @param[in] key value of the elements to remove
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return Number of elements removed. If @a ObjectType is the default @return Number of elements removed. If @a ObjectType is the default
`std::map` type, the return value will always be `0` (@a key was not `std::map` type, the return value will always be `0` (@a key was not
...@@ -4351,17 +4356,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -4351,17 +4356,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
size_type erase(const KeyType& key) size_type erase(KeyT && key)
{ {
// this erase only works for objects // this erase only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
return m_value.object->erase(key); return m_value.object->erase(std::forward<KeyT>(key));
} }
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
......
...@@ -20281,6 +20281,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20281,6 +20281,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
Returns a reference to the element at with specified key @a key, with Returns a reference to the element at with specified key @a key, with
bounds checking. bounds checking.
@tparam KeyT a type convertible to an object key or a `std::string_view`
@param[in] key key of the element to access @param[in] key key of the element to access
@return reference to the element at key @a key @return reference to the element at key @a key
...@@ -20305,12 +20306,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20305,12 +20306,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
written using `at()`. It also demonstrates the different exceptions that written using `at()`. It also demonstrates the different exceptions that
can be thrown.,at__object_t_key_type} can be thrown.,at__object_t_key_type}
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
reference at(const KeyType& key) reference at(KeyT && key)
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_UNLIKELY(!is_object())) if (JSON_HEDLEY_UNLIKELY(!is_object()))
...@@ -20318,7 +20319,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20318,7 +20319,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
} }
auto it = m_value.object->find(key); auto it = m_value.object->find(std::forward<KeyT>(key));
if (it == m_value.object->end()) if (it == m_value.object->end())
{ {
JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this)); JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this));
...@@ -20333,6 +20334,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20333,6 +20334,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
with bounds checking. with bounds checking.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return const reference to the element at key @a key @return const reference to the element at key @a key
...@@ -20356,12 +20358,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20356,12 +20358,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
`at()`. It also demonstrates the different exceptions that can be thrown., `at()`. It also demonstrates the different exceptions that can be thrown.,
at__object_t_key_type_const} at__object_t_key_type_const}
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
const_reference at(const KeyType& key) const const_reference at(KeyT && key) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_UNLIKELY(!is_object())) if (JSON_HEDLEY_UNLIKELY(!is_object()))
...@@ -20369,7 +20371,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20369,7 +20371,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
} }
auto it = m_value.object->find(key); auto it = m_value.object->find(std::forward<KeyT>(key));
if (it == m_value.object->end()) if (it == m_value.object->end())
{ {
JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this)); JSON_THROW(out_of_range::create(403, "key '" + std::string(key) + "' not found", *this));
...@@ -20476,6 +20478,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20476,6 +20478,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
In case the value was `null` before, it is converted to an object. In case the value was `null` before, it is converted to an object.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return reference to the element at key @a key @return reference to the element at key @a key
...@@ -20493,12 +20496,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20493,12 +20496,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
reference operator[](const KeyType& key) reference operator[](KeyT && key)
{ {
// implicitly convert null value to an empty object // implicitly convert null value to an empty object
if (is_null()) if (is_null())
...@@ -20511,7 +20514,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20511,7 +20514,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// operator[] only works for objects // operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
return set_parent(m_value.object->operator[](key)); return set_parent(m_value.object->operator[](std::forward<KeyT>(key)));
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
...@@ -20527,6 +20530,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20527,6 +20530,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
undefined. undefined.
@param[in] key key of the element to access @param[in] key key of the element to access
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return const reference to the element at key @a key @return const reference to the element at key @a key
...@@ -20547,17 +20551,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -20547,17 +20551,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
const_reference operator[](const KeyType& key) const const_reference operator[](KeyT && key) const
{ {
// const operator[] only works for objects // const operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
JSON_ASSERT(m_value.object->find(key) != m_value.object->end()); JSON_ASSERT(m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end());
return m_value.object->find(key)->second; return m_value.object->find(key)->second;
} }
...@@ -21141,6 +21145,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -21141,6 +21145,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
Removes elements from a JSON object with the key value @a key. Removes elements from a JSON object with the key value @a key.
@param[in] key value of the elements to remove @param[in] key value of the elements to remove
@tparam KeyT a type convertible to an object key or a `std::string_view`
@return Number of elements removed. If @a ObjectType is the default @return Number of elements removed. If @a ObjectType is the default
`std::map` type, the return value will always be `0` (@a key was not `std::map` type, the return value will always be `0` (@a key was not
...@@ -21164,17 +21169,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ...@@ -21164,17 +21169,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0 @since version 1.0.0
*/ */
template < class KeyType, typename std::enable_if < template < class KeyT, typename std::enable_if <
#if defined(JSON_HAS_CPP_17) #if defined(JSON_HAS_CPP_17)
std::is_same<KeyType, std::string_view>::value || std::is_same<KeyT, std::string_view>::value ||
#endif #endif
std::is_convertible<KeyType, typename object_t::key_type>::value, int >::type = 0 > std::is_convertible<KeyT, typename object_t::key_type>::value, int >::type = 0 >
size_type erase(const KeyType& key) size_type erase(KeyT && key)
{ {
// this erase only works for objects // this erase only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
return m_value.object->erase(key); return m_value.object->erase(std::forward<KeyT>(key));
} }
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment