Is it possible to provide serialization and deserialization of single file header c++ structs (classes) #213
-
and support common stl types(std::vector,std::string, etc struct hero{
int hp;
std::string name;
....
} If using json then the field names will be kept in the binary file. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm sorry I don't entirely understand the question. Common stl types should automatically be supported. And custom containers written with an stl style api should also work. If you are asking to drop the field names an serialize into an array then that is possible by using glz::array instead of glz::object struct V3
{
double x{3.14};
double y{2.7};
double z{6.5};
bool operator==(const V3& rhs) const { return (x == rhs.x) && (y == rhs.y) && (z == rhs.z); }
};
template <>
struct glz::meta<V3>
{
static constexpr auto value = array(&V3::x, &V3::y, &V3::z);
};
// Here is some test code using this in a boost ut suite
"user array"_test = [] {
V3 v3{9.1, 7.2, 1.9};
std::string buffer{};
glz::write_json(v3, buffer);
expect(buffer == "[9.1,7.2,1.9]");
expect(glz::read_json(v3, "[42.1,99.2,55.3]") == glz::error_code::none);
expect(v3.x == 42.1 && v3.y == 99.2 && v3.z == 55.3);
}; Technically we could try to do this form of serialization without a meta on aggregate types if we used a similar trick to https://github.com/apolukhin/magic_get But in general we need the meta until we have static reflection. |
Beta Was this translation helpful? Give feedback.
-
While we could have automatic serialization to |
Beta Was this translation helpful? Give feedback.
While we could have automatic serialization to
glz::array
from plain old data type structs, we are looking forward to reflection where we would prefer C++ structs to be handled as objects. So, it seems like the best path forward is to require the user to useglz::array
inglz::meta
explicitly.