mirror of
https://github.com/nlohmann/json.git
synced 2026-10-09 16:07:14 +00:00
Restore v3.12.0 support for custom object key types
Custom object_t types whose key_type is not string_t compiled with v3.12.0 for several APIs that unreleased changes broke: - to_bson failed for every custom key type (#5553 kept a const string_t* to the key); the nested entry's header is now written where the entry is found. - Copying deep values (and parse, merge_patch, update, insert) required operator== on keys (#5389); keys without one are now paired via find(). - to_cbor/to_msgpack required an implicit conversion to string_t (#5746, #5328); keys without one go through a temporary basic_json again. - at() required a conversion to string_t for its error message (#5727); other keys are passed to concat() unchanged again. The new unit-custom-object-key-type.cpp covers five key types with different capabilities. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
6 files changed
+672
-48
No files matched your search
@@ -172,6 +172,10 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
|
||||
template<typename T>
|
||||
using detect_key_compare = typename T::key_compare;
|
||||
|
||||
// detects whether two values of type T can be compared with operator==
|
||||
template<typename T>
|
||||
using detect_equal_comparable = decltype(static_cast<bool>(std::declval<const T&>() == std::declval<const T&>()));
|
||||
|
||||
// obtains the actual object key comparator: object_t::key_compare if the
|
||||
// object type defines it, and default_object_comparator_t otherwise
|
||||
//
|
||||
|
||||
@@ -307,12 +307,6 @@ class binary_writer
|
||||
|
||||
case value_t::object:
|
||||
{
|
||||
static_assert(
|
||||
std::is_convertible <
|
||||
typename BasicJsonType::object_t::key_type,
|
||||
string_t >::value,
|
||||
"object_t::key_type must be implicitly convertible to string_t");
|
||||
|
||||
// step 1: write control byte and the object size
|
||||
write_cbor_head(0xA0, j.m_data.m_value.object->size());
|
||||
|
||||
@@ -320,7 +314,7 @@ class binary_writer
|
||||
{
|
||||
// el.first is written directly (not via a temporary
|
||||
// basic_json), with the object as diagnostics context
|
||||
write_cbor_string(el.first, j);
|
||||
write_cbor_key(el.first, j);
|
||||
write_cbor(el.second, depth + 1);
|
||||
}
|
||||
break;
|
||||
@@ -585,12 +579,6 @@ class binary_writer
|
||||
|
||||
case value_t::object:
|
||||
{
|
||||
static_assert(
|
||||
std::is_convertible <
|
||||
typename BasicJsonType::object_t::key_type,
|
||||
string_t >::value,
|
||||
"object_t::key_type must be implicitly convertible to string_t");
|
||||
|
||||
// step 1: write control byte and the object size
|
||||
write_msgpack_object_prefix(j.m_data.m_value.object->size(), j);
|
||||
|
||||
@@ -598,7 +586,7 @@ class binary_writer
|
||||
{
|
||||
// as in write_cbor, el.first is written directly with the
|
||||
// object as diagnostics context
|
||||
write_msgpack_string(el.first, j);
|
||||
write_msgpack_key(el.first, j);
|
||||
write_msgpack(el.second, depth + 1);
|
||||
}
|
||||
break;
|
||||
@@ -984,7 +972,7 @@ class binary_writer
|
||||
|
||||
// the key is written directly (not via a temporary basic_json),
|
||||
// with the object as diagnostics context, as in write_cbor
|
||||
write_cbor_string(current.object_it->first, *current.value);
|
||||
write_cbor_key(current.object_it->first, *current.value);
|
||||
const BasicJsonType* child = &(current.object_it->second);
|
||||
++stack.back().object_it;
|
||||
write_cbor_value_or_push(*child, stack);
|
||||
@@ -1061,7 +1049,7 @@ class binary_writer
|
||||
|
||||
// as in write_cbor_iterative, the key is written directly with
|
||||
// the object as diagnostics context
|
||||
write_msgpack_string(current.object_it->first, *current.value);
|
||||
write_msgpack_key(current.object_it->first, *current.value);
|
||||
const BasicJsonType* child = &(current.object_it->second);
|
||||
++stack.back().object_it;
|
||||
write_msgpack_value_or_push(*child, stack);
|
||||
@@ -1828,7 +1816,6 @@ class binary_writer
|
||||
{
|
||||
// write entries until the current object or array is done, or an
|
||||
// entry is an object or array itself
|
||||
const string_t* nested_name = nullptr;
|
||||
const BasicJsonType* nested = nullptr;
|
||||
if (current.value->is_object())
|
||||
{
|
||||
@@ -1839,7 +1826,8 @@ class binary_writer
|
||||
++current.member;
|
||||
if (el.second.is_structured())
|
||||
{
|
||||
nested_name = &el.first;
|
||||
write_bson_entry_header(el.first, el.second.is_object() ? 0x03 : 0x04);
|
||||
write_number<std::int32_t>(to_bson_length(nested_sizes[next_size++]), true);
|
||||
nested = &el.second;
|
||||
}
|
||||
else
|
||||
@@ -1858,7 +1846,8 @@ class binary_writer
|
||||
++current.index;
|
||||
if (el.is_structured())
|
||||
{
|
||||
nested_name = &index_name;
|
||||
write_bson_entry_header(index_name, el.is_object() ? 0x03 : 0x04);
|
||||
write_number<std::int32_t>(to_bson_length(nested_sizes[next_size++]), true);
|
||||
nested = ⪙
|
||||
}
|
||||
else
|
||||
@@ -1870,8 +1859,6 @@ class binary_writer
|
||||
|
||||
if (nested != nullptr)
|
||||
{
|
||||
write_bson_entry_header(*nested_name, nested->is_object() ? 0x03 : 0x04);
|
||||
write_number<std::int32_t>(to_bson_length(nested_sizes[next_size++]), true);
|
||||
parents.push_back(std::move(current));
|
||||
current = bson_frame(nested);
|
||||
continue;
|
||||
@@ -1939,6 +1926,43 @@ class binary_writer
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write an object key as a CBOR text string
|
||||
|
||||
A key convertible to string_t is written directly. Other key types (only
|
||||
an explicit conversion, or only a to_json overload) go through a temporary
|
||||
basic_json, as in version 3.12.0; the temporary is then the diagnostics
|
||||
context for strict UTF-8 checks.
|
||||
*/
|
||||
template<typename Key = typename BasicJsonType::object_t::key_type,
|
||||
enable_if_t<std::is_convertible<Key, string_t>::value, int> = 0>
|
||||
void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context)
|
||||
{
|
||||
write_cbor_string(key, context);
|
||||
}
|
||||
|
||||
template < typename Key = typename BasicJsonType::object_t::key_type,
|
||||
enable_if_t < !std::is_convertible<Key, string_t>::value, int > = 0 >
|
||||
void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/)
|
||||
{
|
||||
write_cbor(BasicJsonType(key));
|
||||
}
|
||||
|
||||
/// @brief write an object key as a MessagePack str, as in @ref write_cbor_key
|
||||
template<typename Key = typename BasicJsonType::object_t::key_type,
|
||||
enable_if_t<std::is_convertible<Key, string_t>::value, int> = 0>
|
||||
void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context)
|
||||
{
|
||||
write_msgpack_string(key, context);
|
||||
}
|
||||
|
||||
template < typename Key = typename BasicJsonType::object_t::key_type,
|
||||
enable_if_t < !std::is_convertible<Key, string_t>::value, int > = 0 >
|
||||
void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/)
|
||||
{
|
||||
write_msgpack(BasicJsonType(key));
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a CBOR text string
|
||||
|
||||
|
||||
@@ -1421,6 +1421,24 @@ public:
|
||||
return create<object_t>(first, last);
|
||||
}
|
||||
|
||||
/// @brief compare two object keys for equality, if the key type supports it
|
||||
/// @note object_t only needs operator< for its keys (std::map), so operator==
|
||||
/// may not exist; the keys are then reported as different, which makes
|
||||
/// copy_object_level pair the values via object_t::find()
|
||||
template<typename K = typename object_t::key_type,
|
||||
detail::enable_if_t<detail::is_detected<detail::detect_equal_comparable, K>::value, int> = 0>
|
||||
static bool copy_keys_equal(const K& a, const K& b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
template < typename K = typename object_t::key_type,
|
||||
detail::enable_if_t < !detail::is_detected<detail::detect_equal_comparable, K>::value, int > = 0 >
|
||||
static bool copy_keys_equal(const K& /*a*/, const K& /*b*/)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// @brief create the copy of the object @a src in @a dst
|
||||
/// @note structured values are appended to @a worklist instead
|
||||
static void copy_object_level(const basic_json& src, basic_json& dst,
|
||||
@@ -1453,7 +1471,7 @@ public:
|
||||
auto src_it = src_object.cbegin();
|
||||
for (auto& element : *dst.m_data.m_value.object)
|
||||
{
|
||||
if (JSON_HEDLEY_LIKELY(src_it != src_object.cend() && src_it->first == element.first))
|
||||
if (JSON_HEDLEY_LIKELY(src_it != src_object.cend() && copy_keys_equal(src_it->first, element.first)))
|
||||
{
|
||||
copy_shallow(src_it->second, element.second, worklist);
|
||||
++src_it;
|
||||
@@ -3330,11 +3348,27 @@ public:
|
||||
// std::map or ordered_map) never moves from its argument, so key is still
|
||||
// valid here regardless of whether KeyType was deduced as an rvalue reference
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move,hicpp-invalid-access-moved)
|
||||
JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(key), "' not found"), &j));
|
||||
JSON_THROW(out_of_range::create(403, detail::concat("key '", key_for_message(key), "' not found"), &j));
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
/// @brief key as it is passed to detail::concat for an error message
|
||||
/// @note string_t is used where it can be constructed from the key; other
|
||||
/// key types are passed through unchanged, as concat only needs
|
||||
/// data() and size() of them
|
||||
template<typename KeyType, detail::enable_if_t<std::is_constructible<string_t, const KeyType&>::value, int> = 0>
|
||||
static string_t key_for_message(const KeyType& key)
|
||||
{
|
||||
return string_t(key);
|
||||
}
|
||||
|
||||
template < typename KeyType, detail::enable_if_t < !std::is_constructible<string_t, const KeyType&>::value, int > = 0 >
|
||||
static const KeyType & key_for_message(const KeyType& key)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
/// @brief checked array element access used by the at() overloads taking an index
|
||||
/// @throw type_error.304 if @a j is not an array
|
||||
/// @throw out_of_range.401 if @a idx is out of range
|
||||
|
||||
Reference in new issue
Block a user