mirror of
https://github.com/nlohmann/json.git
synced 2026-08-28 11:57:31 +00:00
Custom BinaryType direct assignment (#5266)
* Fix discussion #4209: custom BinaryType direct assignment and extraction When a custom BinaryType is configured (other than the default std::vector<uint8_t>), users can now: 1. Assign values of that type directly to create binary values (not arrays) 2. Extract binary values back to that type with get<>() 3. Extract arrays to that type (for backward compatibility) Implementation: - Add is_compatible_binary_type trait to centralize SFINAE condition - Update to_json to accept custom BinaryType values directly - Update from_json to handle both binary and array inputs for custom BinaryType - Add #include <vector> with IWYU comment to from_json.hpp - Add comprehensive tests for assignment and array extraction - Update binary_t documentation with example This is purely additive and invisible to the default nlohmann::json alias, which continues to treat std::vector<uint8_t> as arrays. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix CI: missing include and const-correctness - Add #include <vector> to type_traits.hpp for the new is_compatible_binary_type trait's std::vector<std::uint8_t> reference (caught by cpplint's include-what-you-use check) - Mark test-local json variables const where never reassigned (caught by clang-tidy's misc-const-correctness check) Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <tuple> // tuple
|
||||
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
|
||||
#include <utility> // declval
|
||||
#include <vector> // vector
|
||||
#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
|
||||
#include <cstddef> // byte
|
||||
#endif
|
||||
@@ -632,6 +633,14 @@ template<typename BasicJsonType, typename CompatibleType>
|
||||
struct is_compatible_type
|
||||
: is_compatible_type_impl<BasicJsonType, CompatibleType> {};
|
||||
|
||||
template<typename BasicJsonType, typename CompatibleArrayType>
|
||||
struct is_compatible_binary_type
|
||||
{
|
||||
static constexpr bool value =
|
||||
std::is_same<typename BasicJsonType::binary_t::container_type, CompatibleArrayType>::value &&
|
||||
!std::is_same<typename BasicJsonType::binary_t::container_type, std::vector<std::uint8_t>>::value;
|
||||
};
|
||||
|
||||
template<typename BasicJsonType, typename CompatibleReferenceType>
|
||||
struct is_compatible_reference_type_impl
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user