1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-31 10:24:23 +03:00

Specialize char_traits for std::byte to fix from_msgpack (fixes #4756) (#4760)

* Specialize char_traits for std::byte to fix from_msgpack (fixes #4756)

Provide a char_traits<std::byte> specialization under __cpp_lib_byte
to allow parsing MessagePack data from containers of std::byte.

Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>

* Fix comments for cstddef include and MessagePack tests

Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>

* Fix include <cstddef> only when __cpp_lib_byte is defined and sufficient

Signed-off-by: yexiaochuan <tap91624@gmail.com>

* Fix clang-tidy warnings in MessagePack std::byte tests

Signed-off-by: yexiaochuan <tap91624@gmail.com>

* Fix handle return value in MessagePack tests

Signed-off-by: yexiaochuan <tap91624@gmail.com>

---------

Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
This commit is contained in:
Xiaochuan Ye
2025-04-28 22:19:47 +08:00
committed by GitHub
parent 6b9199382b
commit 3b02afb9d9
3 changed files with 131 additions and 2 deletions

View File

@ -3367,7 +3367,9 @@ NLOHMANN_JSON_NAMESPACE_END
#include <tuple> // tuple
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
#include <utility> // declval
#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
#include <cstddef> // byte
#endif
// #include <nlohmann/detail/iterators/iterator_traits.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
@ -3776,6 +3778,30 @@ struct char_traits<signed char> : std::char_traits<char>
}
};
#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
template<>
struct char_traits<std::byte> : std::char_traits<char>
{
using char_type = std::byte;
using int_type = uint64_t;
static int_type to_int_type(char_type c) noexcept
{
return static_cast<int_type>(std::to_integer<unsigned char>(c));
}
static char_type to_char_type(int_type i) noexcept
{
return std::byte(static_cast<unsigned char>(i));
}
static constexpr int_type eof() noexcept
{
return static_cast<int_type>(std::char_traits<char>::eof());
}
};
#endif
///////////////////
// is_ functions //
///////////////////