PTOT: Put this over there
This is a general purpose serialization library for Zig types. I created this over time for my in progress mmo and then separated it out into it’s own library. Hopefully, this can be of use to you as well.
Features:
- Types must be added to your Lexicon for them to be serialized. I didn’t want to accidentally sent data to the client that was unnecessary and that I didn’t realize I was sending
- Allocation is only required when a type contains a pointer. This is assert-able via
Lexicon.assert_no_allocation_required. it even gives a nice little crumble trail for where the allocation is required. - You can register manually created
Codecsto your Lexicon for fine control over how the data is serialized.
For Example:
pub const StrRefCodec = struct {
// StrRef is a enum(u32) into an intern pool. If I send a StrRef to the client I expect the receiving field to be a []const u8, so the codec isn't called client side
pub fn serialize(Lexicon: type, self: common.StrRef, w: *std.Io.Writer) C.SError!void {
_ = @import("build_options").role == .Server or unreachable;
try Lexicon.serialize([]const u8, self.slice(), w);
}
pub fn deserialize(Lexicon: type, _: *std.Io.Reader, _: std.mem.Allocator) C.DError!common.StrRef {
unreachable;
}
};
I recently added a byte slice optimization: If a type has a safe and stable memory layout it can be serialized as bytes in 1 writeAll call instead of generating more generic functions. In the future I want to support allocating to a type controlled Arena for simpler deallocation and a niche optimization for types that contain many optionals.
Supported Zig versions
0.16.0