Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting Started

Installation

Add comptime-serde as a dependency in your build.zig.zon:

# 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

Then in your build.zig:

const serde_dep = b.dependency("comptime_serde", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("comptime_serde", serde_dep.module("comptime_serde"));

Core API

The public API lives in the root module:

const serde = @import("comptime_serde");

Serde(format, T)

Returns a comptime-generated struct with serialize and deserialize methods for type T in the given format.

const json = serde.Serde(.json, MyStruct);
const toml = serde.Serde(.toml, MyStruct);
const yaml = serde.Serde(.yaml, MyStruct);

Parsed(T)

Wraps a deserialized value with its backing arena allocator. Always call deinit() when done.

var result = try json.deserialize(allocator, input);
defer result.deinit();
// use result.value

Format

Enum of supported formats: .json, .toml, .yaml, .protobuf.

Supported Types

comptime-serde supports all common Zig types:

Zig TypeJSONTOMLYAML
booltrue/falsetrue/falsetrue/false
u8u64, i8i64numberintegerinteger
f32, f64numberfloatfloat
[]const u8stringstringstring
?Tnull or valueomitted or valuenull or value
[N]Tarrayarraysequence
[]Tarrayarray of tablessequence
struct { ... }objecttablemapping
enum { ... }stringstringstring