Skip to content

API Overview

Complete API reference for zon.zig — a document-based ZON library.

Document vs Type-Based API

zon.zig provides a document-based (DOM-like) API where you work with a Document object containing a Value tree. This differs from Zig's std.zon which deserializes directly into typed Zig structures.

Use zon.zig when you need to edit, search, or manipulate ZON files dynamically.

Module Functions

Top-level functions exposed by the zon module.

Document Creation

zig
const zon = @import("zon");

// Create empty document (aliases: init, new)
var doc = zon.create(allocator);

// Open file (aliases: load, fromFile)
var doc = try zon.open(allocator, "config.zon");

// Parse string (aliases: fromSource, parseString)
var doc = try zon.parse(allocator, source);

// Load or Create
var doc = try zon.loadOrCreate(allocator, "settings.zon", ".{}");

File Utilities

zig
// Check if file exists
if (zon.fileExists("config.zon")) { ... }

// Read file into allocator-owned buffer (caller frees)
const contents = try zon.readFile(allocator, "config.zon");
allocator.free(contents);

// Copy file (overwrite = true to replace destination)
try zon.copyFile("source.zon", "dest.zon", true);

// Move (rename) file (alias: renameFile)
try zon.moveFile("old.zon", "new.zon", true);

// Delete file (alias: removeFile)
try zon.deleteFile("temp.zon");

// Key Operations (alias: movePathInFile, copyPathInFile)
try zon.movePathInFile(allocator, "config.zon", "old.key", "new.key");

// Write atomically (writes to temp and renames)
try zon.writeFileAtomic(allocator, "out.zon", sourceData);

// Delete file
try zon.deleteFile("temp.zon");

Update Checking

zig
// Disable update notifications
zon.disableUpdateCheck();

// Enable update notifications
zon.enableUpdateCheck();

// Check if enabled
if (zon.isUpdateCheckEnabled()) { ... }

// Manual check
zon.checkForUpdates(allocator);

Version

zig
// Get version string
std.debug.print("Version: {s}\n", .{zon.version});

Document Methods

Getters

MethodReturnDescription
getString(path)?[]const u8Get string value
getIdentifier(path)?[]const u8Get identifier value
getBool(path)?boolGet boolean value
getInt(path)?i64Get integer value
getFloat(path)?f64Get float value
getNumber(path)?f64Alias for getFloat
getValue(path)?*const ValueGet raw Value

Setters

MethodDescription
setString(path, value)Set string value
setIdentifier(path, value)Set identifier (.value)
setBool(path, value)Set boolean value
setInt(path, value)Set integer value
setFloat(path, value)Set float value
setNumber(path, value)Alias for setFloat
setNull(path)Set to null
setObject(path)Create empty object
setArray(path)Create empty array
setValue(path, value)Set raw Value

Checkers

MethodReturnDescription
exists(path)boolCheck if path exists
isNull(path)boolCheck if value is null
isIdentifier(path)boolCheck if value is identifier
getType(path)?[]const u8Get base type name
getTypeName(path)?[]const u8Get precise type name
isNan(path)boolCheck if value is NaN
isInf(path)boolCheck if value is Inf
isEmpty()boolCheck if document empty

Modification

MethodReturnDescription
delete(path)boolDelete key
clear()voidClear all data
count()usizeNumber of root keys
keys()![][]const u8Get all root keys

Array Operations

MethodReturnDescription
arrayLen(path)?usizeGet array length
getArrayElement(path, index)?*const ValueGet element
getArrayString(path, index)?[]const u8Get string element
getArrayInt(path, index)?i64Get integer element
getArrayBool(path, index)?boolGet boolean element
appendToArray(path, string)!voidAppend string
appendIntToArray(path, int)!voidAppend integer
appendFloatToArray(path, float)!voidAppend float
appendBoolToArray(path, bool)!voidAppend boolean
removeFromArray(path, index)boolRemove item
insertStringIntoArray(path, idx, val)!voidInsert string
insertIntIntoArray(path, idx, val)!voidInsert integer
indexOf(path, value)?usizeFind string index
countAt(path)usizeCount items at path

Find & Replace

MethodReturnDescription
findString(needle)![][]const u8Find paths containing
findExact(needle)![][]const u8Find paths with exact match
replaceAll(find, replace)!usizeReplace all occurrences
replaceFirst(find, replace)!boolReplace first occurrence
replaceLast(find, replace)!boolReplace last occurrence

Merge & Clone

MethodReturnDescription
merge(other)!voidShallow merge document
mergeRecursive(other)!voidRecursive (deep) merge
clone()!DocumentCreate deep copy
eql(other)boolDeep equality check
diff(other)![][]const u8Get differing keys

Extra Conversions

MethodReturnDescription
getUint(path)?u64Get u64 value (hashes, etc.)
toBool(path)boolCoerce value to boolean

Output

MethodReturnDescription
toString()![]u84-space indent
toCompactString()![]u8No indentation
toPrettyString(indent)![]u8Custom indent
save()!voidSave to original path
saveAs(path)!voidSave to new path

Cleanup

zig
doc.deinit(); // Always call when done

Value Types

Value Union

zig
pub const Value = union(enum) {
    null_val,
    bool_val: bool,
    number: Number,
    string: []const u8,
    identifier: []const u8,
    object: Object,
    array: Array,
};

Value Methods

MethodReturnDescription
asString()?[]const u8Get as string
asIdentifier()?[]const u8Get as identifier
asBool()?boolGet as boolean
asInt()?i64Get as integer
asFloat()?f64Get as float
asObject()?*ObjectGet as object
asArray()?*ArrayGet as array
isNull()boolCheck if null
isIdentifier()boolCheck if identifier
clone(allocator)!ValueDeep copy
deinit(allocator)voidFree memory

Error Types

ParseError

zig
pub const ParseError = error{
    UnexpectedToken,
    InvalidNumber,
    InvalidString,
    UnterminatedString,
    OutOfMemory,
};

Type Name Strings

The getType() method returns these strings:

ValueType String
null"null"
true/false"bool"
Integer"int"
Float"float"
"string""string"
.identifier"identifier"
.{ ... } object"object"
.{ ... } array"array"

Quick Reference

zig
const std = @import("std");
const zon = @import("zon");

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

    zon.disableUpdateCheck();

    // Create
    var doc = zon.create(allocator);
    defer doc.deinit();

    // Set
    try doc.setIdentifier("name", "myapp");
    try doc.setString("version", "1.0.0");
    try doc.setInt("port", 8080);
    try doc.setBool("debug", true);
    try doc.setFloat("rate", 0.05);
    try doc.setNull("password");

    // Nested
    try doc.setString("db.host", "localhost");
    try doc.setInt("db.port", 5432);

    // Arrays
    try doc.setArray("paths");
    try doc.appendToArray("paths", "src");
    try doc.appendToArray("paths", "lib");

    // Get
    const name = doc.getIdentifier("name").?;
    const port = doc.getInt("port").?;
    const len = doc.arrayLen("paths").?;

    // Check
    _ = doc.exists("port");
    _ = doc.isNull("password");
    _ = doc.isIdentifier("name");
    _ = doc.getType("port");

    // Modify
    _ = doc.delete("debug");

    // Output
    const output = try doc.toString();
    defer allocator.free(output);

    // Save
    try doc.saveAs("config.zon");
}

Released under the MIT License.