From dd54563dd939ac475aa51c1d95a90df5d7267d03 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Tue, 15 Apr 2025 19:30:04 +0200 Subject: [PATCH] Added a separate parser for the Bytestring to avoid it being serialized as an array; #407 --- docs/bytestring.md | 4 +-- include/rfl/Bytestring.hpp | 9 ++---- include/rfl/parsing/Parser.hpp | 1 + include/rfl/parsing/Parser_bytestring.hpp | 38 +++++++++++++++++++++++ include/rfl/parsing/Parser_default.hpp | 4 --- include/rfl/parsing/VectorParser.hpp | 4 +++ 6 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 include/rfl/parsing/Parser_bytestring.hpp diff --git a/docs/bytestring.md b/docs/bytestring.md index e11e6e54..d4f50a3f 100644 --- a/docs/bytestring.md +++ b/docs/bytestring.md @@ -10,5 +10,5 @@ using Bytestring = std::vector; } ``` -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. diff --git a/include/rfl/Bytestring.hpp b/include/rfl/Bytestring.hpp index 56f47cda..197531e5 100644 --- a/include/rfl/Bytestring.hpp +++ b/include/rfl/Bytestring.hpp @@ -5,13 +5,8 @@ #include 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; -class Bytestring : public std::vector { -public: - using std::vector::vector; -}; + +using Bytestring = std::vector; } // namespace rfl diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index d82bc434..e7f2ed8d 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -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" diff --git a/include/rfl/parsing/Parser_bytestring.hpp b/include/rfl/parsing/Parser_bytestring.hpp new file mode 100644 index 00000000..93a7499b --- /dev/null +++ b/include/rfl/parsing/Parser_bytestring.hpp @@ -0,0 +1,38 @@ +#ifndef RFL_PARSING_PARSER_BYTESTRING_HPP_ +#define RFL_PARSING_PARSER_BYTESTRING_HPP_ + +#include + +#include "../Bytestring.hpp" +#include "../Result.hpp" +#include "Parser_base.hpp" +#include "schema/Type.hpp" + +namespace rfl::parsing { + +template + requires AreReaderAndWriter +struct Parser { + using InputVarType = typename R::InputVarType; + using ParentType = Parent; + + static Result read(const R& _r, + const InputVarType& _var) noexcept { + return _r.template to_basic_type(_var); + } + + template + 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* _definitions) { + return schema::Type{schema::Type::Bytestring{}}; + } +}; + +} // namespace rfl::parsing + +#endif diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 5bdf21cc..c4e4f3e1 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -5,7 +5,6 @@ #include #include -#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()) { return Type{Type::Boolean{}}; - } else if constexpr (std::is_same()) { - return Type{Type::Bytestring{}}; - } else if constexpr (std::is_same()) { return Type{Type::Int32{}}; diff --git a/include/rfl/parsing/VectorParser.hpp b/include/rfl/parsing/VectorParser.hpp index e40ddd4f..b6ac9cfe 100644 --- a/include/rfl/parsing/VectorParser.hpp +++ b/include/rfl/parsing/VectorParser.hpp @@ -7,6 +7,7 @@ #include #include +#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, Bytestring>, + "Cannot be a bytestring."); + static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (treat_as_map()) { return MapParser::read(_r, _var);