Skip to content

Commit 45e9ab1

Browse files
Added support for bytestrings
1 parent be0423b commit 45e9ab1

6 files changed

Lines changed: 27 additions & 9 deletions

File tree

include/rfl/cereal/Reader.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <string>
1010
#include <type_traits>
1111

12+
#include "../Bytestring.hpp"
1213
#include "../Result.hpp"
14+
#include "../Vectorstring.hpp"
1315
#include "../always_false.hpp"
1416
#include "../internal/is_literal.hpp"
1517
#include "../parsing/schemaful/IsSchemafulReader.hpp"
@@ -58,6 +60,17 @@ struct Reader {
5860
std::string str;
5961
(*_var.archive_)(str);
6062
return std::remove_cvref_t<T>::from_string(str);
63+
64+
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
65+
rfl::Bytestring>() ||
66+
std::is_same<std::remove_cvref_t<T>,
67+
rfl::Vectorstring>()) {
68+
size_t size;
69+
(*_var.archive_)(::cereal::make_size_tag(size));
70+
std::remove_cvref_t<T> result(size);
71+
(*_var.archive_)(::cereal::binary_data(result.data(), size));
72+
return result;
73+
6174
} else {
6275
T value;
6376
(*_var.archive_)(value);

include/rfl/cereal/Writer.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <string_view>
1010
#include <type_traits>
1111

12+
#include "../Bytestring.hpp"
13+
#include "../Vectorstring.hpp"
1214
#include "../always_false.hpp"
1315
#include "../common.hpp"
1416
#include "../internal/is_literal.hpp"
@@ -232,6 +234,12 @@ class Writer {
232234
using Type = std::remove_cvref_t<T>;
233235
if constexpr (internal::is_literal_v<Type>) {
234236
add_value(_var.str());
237+
238+
} else if constexpr (std::is_same<Type, rfl::Bytestring>() ||
239+
std::is_same<Type, rfl::Vectorstring>()) {
240+
(*archive_)(::cereal::make_size_tag(_var.size()));
241+
(*archive_)(::cereal::binary_data(_var.data(), _var.size()));
242+
235243
} else {
236244
(*archive_)(_var);
237245
}

tests/cereal/test_bytestring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ struct TestStruct {
99
};
1010

1111
TEST(cereal, test_bytestring) {
12-
/*const auto test =
12+
const auto test =
1313
TestStruct{.bytestring = rfl::Bytestring({std::byte{13}, std::byte{14},
1414
std::byte{15}, std::byte{16}})};
1515

16-
write_and_read(test);*/
16+
write_and_read(test);
1717
}
1818
} // namespace test_bytestring

tests/cereal/test_literal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ struct Person {
1717
};
1818

1919
TEST(cereal, test_literal) {
20-
/*const auto bart = Person{.first_name = FirstName::make<"Bart">()};
20+
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
2121

22-
write_and_read(bart);*/
22+
write_and_read(bart);
2323
}
2424
} // namespace test_literal

tests/cereal/test_monster_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Monster {
3636
};
3737

3838
TEST(cereal, test_monster_example) {
39-
/*const auto sword = Weapon{.name = "Sword", .damage = 3};
39+
const auto sword = Weapon{.name = "Sword", .damage = 3};
4040
const auto axe = Weapon{.name = "Axe", .damage = 5};
4141

4242
const auto weapons = std::vector<Weapon>({sword, axe});
@@ -54,6 +54,6 @@ TEST(cereal, test_monster_example) {
5454
.weapons = weapons,
5555
.equipped = rfl::make_field<"weapon">(axe)};
5656

57-
write_and_read(orc);*/
57+
write_and_read(orc);
5858
}
5959
} // namespace test_monster_example

tests/cereal/write_and_read.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <gtest/gtest.h>
55

66
#include <rfl/cereal.hpp>
7-
#include <rfl/json.hpp>
87

98
template <class... Ps>
109
void write_and_read(const auto& _struct) {
@@ -15,7 +14,5 @@ void write_and_read(const auto& _struct) {
1514
<< res.error().what();
1615
const auto serialized2 = rfl::cereal::write<Ps...>(res.value());
1716
EXPECT_EQ(serialized1, serialized2);
18-
EXPECT_EQ(rfl::json::write<Ps...>(_struct),
19-
rfl::json::write<Ps...>(res.value()));
2017
}
2118
#endif

0 commit comments

Comments
 (0)