Added a separate parser for the Bytestring to avoid it being serialized as an array; #407 (#408)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-04-16 05:26:21 +08:00
committed by GitHub
parent ef0b0b4729
commit d675bfabb6
6 changed files with 47 additions and 13 deletions
+2 -2
View File
@@ -10,5 +10,5 @@ using Bytestring = std::vector<std::byte>;
}
```
Bytestrings are supported by BSON, CBOR, flexbuffers and msgpack. Textual formats
do not support them.
Bytestrings are supported by binary formats such as BSON, CBOR, flexbuffers and msgpack.
Textual formats do not support them.
+2 -7
View File
@@ -5,13 +5,8 @@
#include <vector>
namespace rfl {
// custom type to avoid serializing this as a vector of enums
// in other means this is the same as
// using Bytestring = std::vector<std::byte>;
class Bytestring : public std::vector<std::byte> {
public:
using std::vector<std::byte>::vector;
};
using Bytestring = std::vector<std::byte>;
} // namespace rfl
+1
View File
@@ -4,6 +4,7 @@
#include "Parser_array.hpp"
#include "Parser_base.hpp"
#include "Parser_box.hpp"
#include "Parser_bytestring.hpp"
#include "Parser_c_array.hpp"
#include "Parser_default.hpp"
#include "Parser_duration.hpp"
+38
View File
@@ -0,0 +1,38 @@
#ifndef RFL_PARSING_PARSER_BYTESTRING_HPP_
#define RFL_PARSING_PARSER_BYTESTRING_HPP_
#include <map>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl::parsing {
template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, Bytestring>
struct Parser<R, W, Bytestring, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using ParentType = Parent<W>;
static Result<Bytestring> read(const R& _r,
const InputVarType& _var) noexcept {
return _r.template to_basic_type<Bytestring>(_var);
}
template <class P>
static void write(const W& _w, const Bytestring& _b,
const P& _parent) noexcept {
ParentType::add_value(_w, _b, _parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return schema::Type{schema::Type::Bytestring{}};
}
};
} // namespace rfl::parsing
#endif
-4
View File
@@ -5,7 +5,6 @@
#include <stdexcept>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../from_named_tuple.hpp"
@@ -152,9 +151,6 @@ struct Parser {
if constexpr (std::is_same<U, bool>()) {
return Type{Type::Boolean{}};
} else if constexpr (std::is_same<U, rfl::Bytestring>()) {
return Type{Type::Bytestring{}};
} else if constexpr (std::is_same<U, std::int32_t>()) {
return Type{Type::Int32{}};
+4
View File
@@ -7,6 +7,7 @@
#include <string>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "MapParser.hpp"
@@ -37,6 +38,9 @@ struct VectorParser {
using T = typename VecType::value_type;
static_assert(!std::is_same_v<std::remove_cvref_t<VecType>, Bytestring>,
"Cannot be a bytestring.");
static Result<VecType> read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (treat_as_map()) {
return MapParser<R, W, VecType, ProcessorsType>::read(_r, _var);