comptime-serde

comptime-serde

Compile-time serialization and deserialization for Zig.

Define your struct once, automatically serialize/deserialize across JSON, TOML, YAML, and Protobuf — zero runtime overhead, all type dispatch happens at comptime via @typeInfo.

comptime-serde architecture

Installation

# Latest version
zig fetch --save git+https://github.com/jiacai2050/comptime-serde.git
# Tagged version
zig fetch --save git+https://github.com/jiacai2050/comptime-serde.git#v0.2.0
// build.zig
const serde_dep = b.dependency("comptime_serde", .{});
exe.root_module.addImport("comptime_serde", serde_dep.module("comptime_serde"));

Quick example

const serde = @import("comptime_serde");

const User = struct {
    name: []const u8,
    age: u32,
};

const json = serde.Serde(.json, User);

var buf: [512]u8 = undefined;
var writer = std.Io.Writer.fixed(&buf);
try json.serialize(&writer, .{ .name = "alice", .age = 30 });
// {"name":"alice","age":30}

var result = try json.deserialize(allocator, "{\"name\":\"alice\",\"age\":30}");
defer result.deinit();
// result.value is a User

serde-gen CLI

curl -fsSL https://jiacai2050.github.io/comptime-serde/install.sh | sh

Infers Zig struct definitions from JSON/TOML/YAML files:

serde-gen config.json

Documentation

License

MIT