added a new method of implementing a custom parser using a Reflector … (#142)

Co-authored-by: Daedie <daedie@gmail.com>
This commit is contained in:
BjornSAIM
2024-07-07 22:41:31 +02:00
committed by GitHub
co-authored by Daedie
parent 1ff7f762f1
commit 6dadbc2506
4 changed files with 91 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
#include <cassert>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace {
struct Person {
std::string first_name = "Homer";
std::string last_name = "Simpson";
std::vector<Person> children;
};
} // namespace
namespace rfl {
template <>
struct Reflector<Person> {
struct ReflType {
std::string first_name;
std::string last_name;
std::vector<Person> children;
};
static Person to(const ReflType& v) noexcept {
return {v.first_name, v.last_name, v.children};
}
static ReflType from(const Person& v) {
return {v.first_name, v.last_name, v.children};
}
};
} // namespace rfl
namespace test_reflector {
TEST(json, test_reflector) {
const auto homer =
Person{"Homer", "Simpson", {{"Bart", "Simpson"}, {"Lisa", "Simpson"}}};
write_and_read(
homer,
R"({"first_name":"Homer","last_name":"Simpson","children":[{"first_name":"Bart","last_name":"Simpson","children":[]},{"first_name":"Lisa","last_name":"Simpson","children":[]}]})");
}
} // namespace test_reflector