-
Couldn't load subscription status.
- Fork 813
Open
Description
What do you all think about using pfr reflection library for having serialization functions be created from static reflection?
github.com/apolukhin/pfr
We could have :
- automatic serialization function from reflection for all aggregate types
- user can still declare specific serialization functions that will be picked up first
- with C++>=20 we even have automatic field names for those aggregate types
Motivating exemple :
struct Bar {
std::string s1;
std::string s2;
};
struct Foo {
int int1;
int int2;
Bar bar1;
float float1;
};
int main()
{
Foo f { 42, 45, { "Hello", "World" }, 3.14f };
std::stringstream ss;
{
cereal::JSONOutputArchive oar(ss);
oar(f); // No manual serialize function needed
}
std::cout << ss.str() << "\n";
}output :
{
"value0": {
"int1": 42,
"int2": 45,
"bar1": {
"s1": "Hello",
"s2": "World"
},
"float1": 3.140000104904175
}
}Example extension implementation :
#include <pfr.hpp>
namespace cereal {
#if PFR_CORE_NAME_ENABLED
template <typename Archive, typename T>
std::enable_if_t<std::is_aggregate_v<T>>
serialize(Archive& archive, T& object)
{
pfr::for_each_field_with_name(object, [&](std::string_view name, auto& field) {
archive(cereal::make_nvp(std::string(name).c_str(), field));
});
}
#else
template <typename Archive, typename T>
std::enable_if_t<std::is_aggregate_v<T>>
serialize(Archive& archive, T& object)
{
pfr::for_each_field(object, [&](auto& field) {
archive(field);
});
}
#endif
}Metadata
Metadata
Assignees
Labels
No labels