The goal is to serialize and deserialize a C class Mango that contains members of custom data types.
The suggested implementation includes the following functions:
std::vector serialize(Mango const& Man);
Mango deserialize(std::span data);
void serialize_to_stream(std::ostream& os, Mango const& Man);
void deserialize(std::istream& is, Mango& Man);
Customizations are required for all relevant types (including ValType, FuntionMango, MangoType, and Mango):
// Define `do_generate` and `do_parse` functions
// for custom data types.
void serialize_to_stream(std::ostream& os, Mango const& Man) {
do_generate(std::ostreambuf_iterator(os), Man);
}
void deserialize(std::istream& is, Mango& Man) {
Man = {}; // clear it!
std::istreambuf_iterator f(is), l{};
if (!do_parse(f, l, Man))
throw std::runtime_error("deserialize");
}
Live Example:
https://coliru.stacked-crooked.com/a/288829ec964a3ca9
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3