Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Namespace

zon

Public Namespace

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.

Namespaces

Structs

Types

Values

Functions

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

Returns a formatter that formats the given value using stringify.

File

zon.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! 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;
}