Skip to content
Standard libraryZig 0.17.1Official documentation
Sourcestd/zon.zig

Source line 1

ZON parsing and stringification.

ZON ("Zig Object Notation") is a textual file format. Outside of nan and inf literals, ZON's grammar is a subset of Zig's.

Supported Zig primitives:

  • boolean literals
  • number literals (including nan and inf)
  • character literals
  • enum literals
  • null literals
  • string literals
  • multiline string literals

Supported Zig container types:

  • anonymous struct literals
  • anonymous tuple literals

Here is an example ZON object:

.{
    .a = 1.5,
    .b = "hello, world!",
    .c = .{ true, false },
    .d = .{ 1, 2, 3 },
    .e = .{ .x = 13, .y = 67 },
}

Individual primitives are also valid ZON, for example:

"This string is a valid ZON object."

ZON may not contain type names.

ZON does not have syntax for pointers, but the parsers will allocate as needed to match the given Zig types. Similarly, the serializer will traverse pointers.

std.zon.fmt

function

Source line 47

Returns a formatter that formats the given value using stringify.

pub fn fmt(value: anytype, options: stringify.SerializeOptions) Formatter(@TypeOf(value))

Parameters

Example usage

test fmt {
    const expectFmt = std.testing.expectFmt;
    try expectFmt("123", "{f}", .{fmt(@as(u32, 123), .{})});
    try expectFmt(
        \\.{
        \\    .num = 927,
        \\    .msg = "hello",
        \\    .sub = .{ .mybool = true },
        \\}
    , "{f}", .{fmt(struct {
        num: u32,
        msg: []const u8,
        sub: struct {
            mybool: bool,
        },
    }{
        .num = 927,
        .msg = "hello",
        .sub = .{ .mybool = true },
    }, .{})});
}

std.zon.Formatter

type-function

Source line 74

Formats the given value using stringify.

pub fn Formatter(comptime T: type) type

Parameters

T: type

Fields

value: T
options: stringify.SerializeOptions

std.zon.Formatter.format

function

Source line 79

pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void

Parameters

self: @This()
writer: *std.Io.Writer

Errors

WriteFailed

See the Writer implementation for detailed diagnostics.

std.zon.parse

alias

Source line 42

Alias of std.zon.parse

std.zon.Serializer

alias

Source line 44

Alias of std.zon.Serializer

std.zon.std

alias

Source line 40

Alias of std

std.zon.stringify

alias

Source line 43

Alias of std.zon.stringify

Source code

std/zon.zig

//! ZON parsing and stringification.
//!
//! ZON ("Zig Object Notation") is a textual file format. Outside of `nan` and `inf` literals, ZON's
//! grammar is a subset of Zig's.
//!
//! Supported Zig primitives:
//! * boolean literals
//! * number literals (including `nan` and `inf`)
//! * character literals
//! * enum literals
//! * `null` literals
//! * string literals
//! * multiline string literals
//!
//! Supported Zig container types:
//! * anonymous struct literals
//! * anonymous tuple literals
//!
//! Here is an example ZON object:
//! ```
//! .{
//!     .a = 1.5,
//!     .b = "hello, world!",
//!     .c = .{ true, false },
//!     .d = .{ 1, 2, 3 },
//!     .e = .{ .x = 13, .y = 67 },
//! }
//! ```
//!
//! Individual primitives are also valid ZON, for example:
//! ```
//! "This string is a valid ZON object."
//! ```
//!
//! ZON may not contain type names.
//!
//! ZON does not have syntax for pointers, but the parsers will allocate as needed to match the
//! given Zig types. Similarly, the serializer will traverse pointers.

const std = @import("std");

pub const parse = @import("zon/parse.zig");
pub const stringify = @import("zon/stringify.zig");
pub const Serializer = @import("zon/Serializer.zig");

/// Returns a formatter that formats the given value using stringify.
pub fn fmt(value: anytype, options: stringify.SerializeOptions) Formatter(@TypeOf(value)) {
    return Formatter(@TypeOf(value)){ .value = value, .options = options };
}

test fmt {
    const expectFmt = std.testing.expectFmt;
    try expectFmt("123", "{f}", .{fmt(@as(u32, 123), .{})});
    try expectFmt(
        \\.{
        \\    .num = 927,
        \\    .msg = "hello",
        \\    .sub = .{ .mybool = true },
        \\}
    , "{f}", .{fmt(struct {
        num: u32,
        msg: []const u8,
        sub: struct {
            mybool: bool,
        },
    }{
        .num = 927,
        .msg = "hello",
        .sub = .{ .mybool = true },
    }, .{})});
}

/// Formats the given value using stringify.
pub fn Formatter(comptime T: type) type {
    return struct {
        value: T,
        options: stringify.SerializeOptions,

        pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
            try stringify.serialize(self.value, self.options, writer);
        }
    };
}

test {
    _ = parse;
    _ = stringify;
}