Skip to content

Value Types

The Value type represents all possible ZON data types.

Type Definition

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

Number Type

zig
pub const Number = union(enum) {
    int: i128,
    float: f64,
};

Value Methods

Type Checking

MethodReturnDescription
isNull()boolCheck if value is null
isIdentifier()boolCheck if value is an identifier
isNan()boolCheck if value is NaN
isPositiveInf()boolCheck if positive infinity
isNegativeInf()boolCheck if negative infinity
isSpecialFloat()boolCheck if NaN or Infinity
typeName()[]const u8Get precise type name
toBool()boolCoerce value to boolean
hash()u64Stable content hash
checksum(A, &out)voidCrypto checksum
eql(Value)boolDeep equality check

Type Conversion

MethodReturnDescription
asString()?[]const u8Get as string (works for identifiers too)
asIdentifier()?[]const u8Get as identifier only
asBool()?boolGet as boolean
asInt()?i64Get as i64 (null if overflow)
asInt128()?i128Get as i128 (all ZON integers)
asUint()?u64Get as u64 (useful for fingerprints)
asFloat()?f64Get as float (converts int to float)
asObject()?*ObjectGet as object
asArray()?*ArrayGet as array

Memory Management

MethodDescription
MethodDescription
--------------------------------------------
deinit(allocator)Free all memory
clone(allocator)Create deep copy
eql(other)Deep equality check
toDebugString(alloc)Get debug representation

Object Type

Key-value map with string keys.

zig
pub const Object = struct {
    allocator: Allocator,
    entries: std.StringHashMapUnmanaged(Value),
};

Object Methods

MethodReturnDescription
init(allocator)ObjectCreate empty object
deinit()voidFree all memory
get(key)?*ValueGet value by key
fetch(key)?*ValueAlias for get
at(key)?*ValueAlias for get
put(key, value)!voidSet value
set(key, value)!voidAlias for put
insert(k, v)!voidAlias for put
remove(key)boolRemove key
delete(key)boolAlias for remove
unset(key)boolAlias for remove
count()usizeNumber of keys
size()usizeAlias for count
len()usizeAlias for count
keys(allocator)![][]const u8Get all keys (caller frees)
clear()voidRemove all entries
reset()voidAlias for clear
empty()voidAlias for clear
iterator()IteratorGet object iterator

Array Type

Ordered list of values.

zig
pub const Array = struct {
    allocator: Allocator,
    items: std.ArrayListUnmanaged(Value),
};

Array Methods

MethodReturnDescription
init(allocator)ArrayCreate empty array
deinit()voidFree all memory
append(value)!voidAdd value
add(value)!voidAlias for append
push(value)!voidAlias for append
get(index)?*ValueGet value at index
at(index)?*ValueAlias for get
len()usizeNumber of elements
size()usizeAlias for len
count()usizeAlias for len
clear()voidRemove all items
reset()voidAlias for clear
empty()voidAlias for clear
iterator()IteratorGet array iterator

Examples

Creating Values

zig
const allocator = std.heap.page_allocator;

// Null
var null_val: Value = .null_val;

// Boolean
var bool_val: Value = .{ .bool_val = true };

// Integer
var int_val: Value = .{ .number = .{ .int = 42 } };

// Float
var float_val: Value = .{ .number = .{ .float = 3.14 } };

// String (must be heap-allocated for proper cleanup)
const text = try allocator.dupe(u8, "hello");
var str_val: Value = .{ .string = text };
defer str_val.deinit(allocator);

// Identifier
const id = try allocator.dupe(u8, "my_package");
var id_val: Value = .{ .identifier = id };
defer id_val.deinit(allocator);

Reading Values

zig
const val: Value = .{ .number = .{ .int = 42 } };

// Safe access with optionals
if (val.asInt()) |i| {
    std.debug.print("Integer: {d}\n", .{i});
}

if (val.asString()) |s| {
    std.debug.print("String: {s}\n", .{s});
} else {
    std.debug.print("Not a string\n", .{});
}

Working with Objects

zig
const allocator = std.testing.allocator;

var obj = Value.Object.init(allocator);
defer obj.deinit();

// Add values
try obj.put("name", .{ .string = try allocator.dupe(u8, "test") });
try obj.put("enabled", .{ .bool_val = true });
try obj.put("count", .{ .number = .{ .int = 42 } });

// Read values
if (obj.get("name")) |val| {
    std.debug.print("Name: {s}\n", .{val.asString().?});
}

// Get all keys
const keys = try obj.keys(allocator);
defer allocator.free(keys);

for (keys) |key| {
    std.debug.print("Key: {s}\n", .{key});
}

// Remove
_ = obj.remove("count");

Working with Arrays

zig
const allocator = std.testing.allocator;

var arr = Value.Array.init(allocator);
defer arr.deinit();

// Add values
try arr.append(.{ .string = try allocator.dupe(u8, "first") });
try arr.append(.{ .string = try allocator.dupe(u8, "second") });
try arr.append(.{ .number = .{ .int = 3 } });

// Read values
for (0..arr.len()) |i| {
    if (arr.get(i)) |val| {
        if (val.asString()) |s| {
            std.debug.print("[{d}] String: {s}\n", .{i, s});
        } else if (val.asInt()) |n| {
            std.debug.print("[{d}] Int: {d}\n", .{i, n});
        }
    }
}

Cloning Values

zig
const allocator = std.testing.allocator;

var original: Value = .{ .string = try allocator.dupe(u8, "hello") };
defer original.deinit(allocator);

var cloned = try original.clone(allocator);
defer cloned.deinit(allocator);

// cloned is independent of original
try std.testing.expectEqualStrings("hello", cloned.asString().?);

ZON Representation

Value TypeZON SyntaxExample
nullnullnull
booltrue/falsetrue
intnumber42, 0xFF
floatnumber3.14
string"...""hello"
identifier.name.my_package
object.{ ... }.{ .key = value }
array.{ ... }.{ "a", "b" }

Released under the MIT License.