mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Serialize/Deserialize vector<char> as bytestring (#466)
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../Vectorstring.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
@@ -66,13 +67,21 @@ struct Reader {
|
||||
return std::string(str.ptr, str.size);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
rfl::Bytestring>() ||
|
||||
std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Vectorstring>()) {
|
||||
using VectorType = std::remove_cvref_t<T>;
|
||||
using ValueType = typename VectorType::value_type;
|
||||
if (type != MSGPACK_OBJECT_BIN) {
|
||||
return error("Could not cast to a bytestring.");
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Bytestring>()) {
|
||||
return error("Could not cast to bytestring.");
|
||||
} else {
|
||||
return error("Could not cast to vectorstring.");
|
||||
}
|
||||
}
|
||||
const auto bin = _var.via.bin;
|
||||
const auto data = internal::ptr_cast<const std::byte*>(bin.ptr);
|
||||
return rfl::Bytestring(data, data + bin.size);
|
||||
const auto data = internal::ptr_cast<const ValueType*>(bin.ptr);
|
||||
return VectorType(data, data + bin.size);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (type != MSGPACK_OBJECT_BOOLEAN) {
|
||||
|
||||
@@ -14,10 +14,11 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../Vectorstring.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
@@ -97,7 +98,7 @@ class Writer {
|
||||
if constexpr (std::is_same<Type, std::string>()) {
|
||||
msgpack_pack_str(pk_, _var.size());
|
||||
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<Type, rfl::Bytestring>()) {
|
||||
} else if constexpr (std::is_same<Type, rfl::Bytestring>() || std::is_same<Type, rfl::Vectorstring>()) {
|
||||
msgpack_pack_bin(pk_, _var.size());
|
||||
msgpack_pack_bin_body(pk_, _var.data(), _var.size());
|
||||
} else if constexpr (std::is_same<Type, bool>()) {
|
||||
|
||||
Reference in New Issue
Block a user