| 1 |
From 3b02afb9d981614813915d432e89777b346a6ddb Mon Sep 17 00:00:00 2001 |
| 2 |
From: Xiaochuan Ye <[email protected]> |
| 3 |
Date: Mon, 28 Apr 2025 22:19:47 +0800 |
| 4 |
Subject: [PATCH] Specialize char_traits for std::byte to fix from_msgpack |
| 5 |
(fixes #4756) (#4760) |
| 6 |
|
| 7 |
* Specialize char_traits for std::byte to fix from_msgpack (fixes #4756) |
| 8 |
|
| 9 |
Provide a char_traits<std::byte> specialization under __cpp_lib_byte |
| 10 |
to allow parsing MessagePack data from containers of std::byte. |
| 11 |
|
| 12 |
Signed-off-by: xuesongtap <[email protected]> |
| 13 |
Signed-off-by: yexiaochuan <[email protected]> |
| 14 |
|
| 15 |
* Fix comments for cstddef include and MessagePack tests |
| 16 |
|
| 17 |
Signed-off-by: xuesongtap <[email protected]> |
| 18 |
Signed-off-by: yexiaochuan <[email protected]> |
| 19 |
|
| 20 |
* Fix include <cstddef> only when __cpp_lib_byte is defined and sufficient |
| 21 |
|
| 22 |
Signed-off-by: yexiaochuan <[email protected]> |
| 23 |
|
| 24 |
* Fix clang-tidy warnings in MessagePack std::byte tests |
| 25 |
|
| 26 |
Signed-off-by: yexiaochuan <[email protected]> |
| 27 |
|
| 28 |
* Fix handle return value in MessagePack tests |
| 29 |
|
| 30 |
Signed-off-by: yexiaochuan <[email protected]> |
| 31 |
|
| 32 |
--------- |
| 33 |
|
| 34 |
Signed-off-by: xuesongtap <[email protected]> |
| 35 |
Signed-off-by: yexiaochuan <[email protected]> |
| 36 |
--- |
| 37 |
include/nlohmann/detail/meta/type_traits.hpp | 28 ++++++- |
| 38 |
single_include/nlohmann/json.hpp | 28 ++++++- |
| 39 |
tests/src/unit-msgpack.cpp | 77 ++++++++++++++++++++ |
| 40 |
3 files changed, 131 insertions(+), 2 deletions(-) |
| 41 |
|
| 42 |
diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp |
| 43 |
index d834a0d56e..6bb46fbdde 100644 |
| 44 |
--- a/include/nlohmann/detail/meta/type_traits.hpp |
| 45 |
+++ b/include/nlohmann/detail/meta/type_traits.hpp |
| 46 |
@@ -13,7 +13,9 @@ |
| 47 |
#include <tuple> // tuple |
| 48 |
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type |
| 49 |
#include <utility> // declval |
| 50 |
- |
| 51 |
+#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L |
| 52 |
+ #include <cstddef> // byte |
| 53 |
+#endif |
| 54 |
#include <nlohmann/detail/iterators/iterator_traits.hpp> |
| 55 |
#include <nlohmann/detail/macro_scope.hpp> |
| 56 |
#include <nlohmann/detail/meta/call_std/begin.hpp> |
| 57 |
@@ -239,6 +241,30 @@ struct char_traits<signed char> : std::char_traits<char> |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
+#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L |
| 62 |
+template<> |
| 63 |
+struct char_traits<std::byte> : std::char_traits<char> |
| 64 |
+{ |
| 65 |
+ using char_type = std::byte; |
| 66 |
+ using int_type = uint64_t; |
| 67 |
+ |
| 68 |
+ static int_type to_int_type(char_type c) noexcept |
| 69 |
+ { |
| 70 |
+ return static_cast<int_type>(std::to_integer<unsigned char>(c)); |
| 71 |
+ } |
| 72 |
+ |
| 73 |
+ static char_type to_char_type(int_type i) noexcept |
| 74 |
+ { |
| 75 |
+ return std::byte(static_cast<unsigned char>(i)); |
| 76 |
+ } |
| 77 |
+ |
| 78 |
+ static constexpr int_type eof() noexcept |
| 79 |
+ { |
| 80 |
+ return static_cast<int_type>(std::char_traits<char>::eof()); |
| 81 |
+ } |
| 82 |
+}; |
| 83 |
+#endif |
| 84 |
+ |
| 85 |
/////////////////// |
| 86 |
// is_ functions // |
| 87 |
/////////////////// |
| 88 |
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp |
| 89 |
index 374b54ecbc..dabad062d1 100644 |
| 90 |
--- a/single_include/nlohmann/json.hpp |
| 91 |
+++ b/single_include/nlohmann/json.hpp |
| 92 |
@@ -3367,7 +3367,9 @@ NLOHMANN_JSON_NAMESPACE_END |
| 93 |
#include <tuple> // tuple |
| 94 |
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type |
| 95 |
#include <utility> // declval |
| 96 |
- |
| 97 |
+#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L |
| 98 |
+ #include <cstddef> // byte |
| 99 |
+#endif |
| 100 |
// #include <nlohmann/detail/iterators/iterator_traits.hpp> |
| 101 |
// __ _____ _____ _____ |
| 102 |
// __| | __| | | | JSON for Modern C++ |
| 103 |
@@ -3776,6 +3778,30 @@ struct char_traits<signed char> : std::char_traits<char> |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
+#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L |
| 108 |
+template<> |
| 109 |
+struct char_traits<std::byte> : std::char_traits<char> |
| 110 |
+{ |
| 111 |
+ using char_type = std::byte; |
| 112 |
+ using int_type = uint64_t; |
| 113 |
+ |
| 114 |
+ static int_type to_int_type(char_type c) noexcept |
| 115 |
+ { |
| 116 |
+ return static_cast<int_type>(std::to_integer<unsigned char>(c)); |
| 117 |
+ } |
| 118 |
+ |
| 119 |
+ static char_type to_char_type(int_type i) noexcept |
| 120 |
+ { |
| 121 |
+ return std::byte(static_cast<unsigned char>(i)); |
| 122 |
+ } |
| 123 |
+ |
| 124 |
+ static constexpr int_type eof() noexcept |
| 125 |
+ { |
| 126 |
+ return static_cast<int_type>(std::char_traits<char>::eof()); |
| 127 |
+ } |
| 128 |
+}; |
| 129 |
+#endif |
| 130 |
+ |
| 131 |
/////////////////// |
| 132 |
// is_ functions // |
| 133 |
/////////////////// |
| 134 |
diff --git a/tests/src/unit-msgpack.cpp b/tests/src/unit-msgpack.cpp |
| 135 |
index 0d49ae6db2..bfea2dce1f 100644 |
| 136 |
--- a/tests/src/unit-msgpack.cpp |
| 137 |
+++ b/tests/src/unit-msgpack.cpp |
| 138 |
@@ -1880,3 +1880,80 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip()) |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
+ |
| 143 |
+#ifdef JSON_HAS_CPP_17 |
| 144 |
+// Test suite for verifying MessagePack handling with std::byte input |
| 145 |
+TEST_CASE("MessagePack with std::byte") |
| 146 |
+{ |
| 147 |
+ |
| 148 |
+ SECTION("std::byte compatibility") |
| 149 |
+ { |
| 150 |
+ SECTION("vector roundtrip") |
| 151 |
+ { |
| 152 |
+ json original = |
| 153 |
+ { |
| 154 |
+ {"name", "test"}, |
| 155 |
+ {"value", 42}, |
| 156 |
+ {"array", {1, 2, 3}} |
| 157 |
+ }; |
| 158 |
+ |
| 159 |
+ std::vector<uint8_t> temp = json::to_msgpack(original); |
| 160 |
+ // Convert the uint8_t vector to std::byte vector |
| 161 |
+ std::vector<std::byte> msgpack_data(temp.size()); |
| 162 |
+ for (size_t i = 0; i < temp.size(); ++i) |
| 163 |
+ { |
| 164 |
+ msgpack_data[i] = std::byte(temp[i]); |
| 165 |
+ } |
| 166 |
+ // Deserialize from std::byte vector back to JSON |
| 167 |
+ json from_bytes; |
| 168 |
+ CHECK_NOTHROW(from_bytes = json::from_msgpack(msgpack_data)); |
| 169 |
+ |
| 170 |
+ CHECK(from_bytes == original); |
| 171 |
+ } |
| 172 |
+ |
| 173 |
+ SECTION("empty vector") |
| 174 |
+ { |
| 175 |
+ const std::vector<std::byte> empty_data; |
| 176 |
+ CHECK_THROWS_WITH_AS([&]() { |
| 177 |
+ [[maybe_unused]] auto result = json::from_msgpack(empty_data); |
| 178 |
+ return true; |
| 179 |
+ }(), |
| 180 |
+ "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", |
| 181 |
+ json::parse_error&); |
| 182 |
+ } |
| 183 |
+ |
| 184 |
+ SECTION("comparison with workaround") |
| 185 |
+ { |
| 186 |
+ json original = |
| 187 |
+ { |
| 188 |
+ {"string", "hello"}, |
| 189 |
+ {"integer", 42}, |
| 190 |
+ {"float", 3.14}, |
| 191 |
+ {"boolean", true}, |
| 192 |
+ {"null", nullptr}, |
| 193 |
+ {"array", {1, 2, 3}}, |
| 194 |
+ {"object", {{"key", "value"}}} |
| 195 |
+ }; |
| 196 |
+ |
| 197 |
+ std::vector<uint8_t> temp = json::to_msgpack(original); |
| 198 |
+ |
| 199 |
+ std::vector<std::byte> msgpack_data(temp.size()); |
| 200 |
+ for (size_t i = 0; i < temp.size(); ++i) |
| 201 |
+ { |
| 202 |
+ msgpack_data[i] = std::byte(temp[i]); |
| 203 |
+ } |
| 204 |
+ // Attempt direct deserialization using std::byte input |
| 205 |
+ json direct_result = json::from_msgpack(msgpack_data); |
| 206 |
+ |
| 207 |
+ // Test the workaround approach: reinterpret as unsigned char* and use iterator range |
| 208 |
+ const auto *const char_start = reinterpret_cast<unsigned char const*>(msgpack_data.data()); |
| 209 |
+ const auto *const char_end = char_start + msgpack_data.size(); |
| 210 |
+ json workaround_result = json::from_msgpack(char_start, char_end); |
| 211 |
+ |
| 212 |
+ // Verify that the final deserialized JSON matches the original JSON |
| 213 |
+ CHECK(direct_result == workaround_result); |
| 214 |
+ CHECK(direct_result == original); |
| 215 |
+ } |
| 216 |
+ } |
| 217 |
+} |
| 218 |
+#endif |