-
Notifications
You must be signed in to change notification settings - Fork 1
Serialization
zzxx edited this page Oct 4, 2021
·
1 revision
ZAF defines serialization/deserialization functions for a set of commonly-used data structures, including POD, containers and data structures in STL.
To implement a serialization/deserialization function for customized classes, we only need to write a pair of serialize/deserialize functions, e.g.,
void serialize(Serializer& s, const T& t) {
// serialize `t` into `s`
}
void deserialize(Deserializer& d, T& t) {
// read into `t` from `d`
}
// or a deserialize function that returns an object of `T` if `T` is not trivally-construtable.
// this function has higher priority than the above in-place deserialization function.
T deserialize(Deserializer& d) {
// read an object of `T` from `d`
}
Serializer and Deserializer themselves do not maintain a buffer. Instead, they write/read to/from an external buffer. Here is an example of using Serializer and Deserializer.
std::vector<char> bytes;
Serializer s(bytes);
s.write((int) 0).write(std::string("Hello World")).write(std::vector<int>{1,2,3});
Deserializer d(bytes);
auto i = d.read<int>();
auto s = d.read<std::string>();
auto v = d.read<std::vector<int>>();