Skip to content

cloudboss/yaml-zig

Repository files navigation

yaml

A standards compliant YAML 1.2 parser and serializer for Zig. Follows an API similar to Zig's std.json.

Inspired by goccy/go-yaml and borrows heavily from the test suite. Passes the official YAML test suite.

API docs are at https://cloudboss.co/yaml-zig.

Usage

Add as a dependency:

zig fetch --save git+https://github.com/cloudboss/yaml-zig

Examples

const A = struct {
    x: []const u8,
    y: u16,
    b: B,
};

const B = struct {
    a: u8,
    b: u8,
    c: u8,
};

Deserialization

const yaml = @import("yaml");

pub fn main() !void {
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const doc =
        \\x: example
        \\y: 789
        \\b:
        \\  a: 1
        \\  b: 2
        \\  c: 3
    ;

    // `parsed` is of type `Parsed(A)`
    const parsed = try yaml.parseFromSlice(A, allocator, doc, .{});
    defer parsed.deinit()

    // access the `A` instance through `parsed.value`
    std.debug.print("{s}\n", .{parsed.value.x});
    std.debug.print("{d}\n", .{parsed.value.y});
    std.debug.print("{d}\n", .{parsed.value.b.a});
    std.debug.print("{d}\n", .{parsed.value.b.b});
    std.debug.print("{d}\n", .{parsed.value.b.c});
}

Output:

example
789
1
2
3

Serialization

const yaml = @import("yaml");

pub fn main() !void {
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const a = A{
        .x = "example",
        .y = 789,
        .b = .{ .a = 1, .b = 2, .c = 3 },
    };

    const string = try yaml.stringifyAlloc(allocator, a, .{});
    defer allocator.free(string);

    std.debug.print("{s}", .{string});
}

Output:

x: example
y: 789
b:
  a: 1
  b: 2
  c: 3

About

No description or website provided.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors