Skip to content

πŸ”· Zig implementation of TOON (Token-Oriented Object Notation) β€” Compact, human-readable JSON encoding for LLM prompts with 30-60% token reduction. Spec-compliant v3.0 implementation.

License

Notifications You must be signed in to change notification settings

copyleftdev/toon-zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”· toon-zig

Zig implementation of TOON (Token-Oriented Object Notation)

Zig License: MIT TOON Spec

A spec-compliant Zig library for encoding and decoding TOON format β€” a compact, human-readable, line-oriented format for structured data, optimized for LLM prompts with 30-60% token reduction vs JSON.

Features β€’ Installation β€’ Usage β€’ API Reference β€’ Contributing


Features

  • Full TOON v3.0 Specification Compliance β€” Implements all normative requirements
  • Encoder: JSON β†’ TOON with configurable delimiters and indentation
  • Decoder: TOON β†’ JSON with strict mode validation
  • Zero Dependencies β€” Pure Zig, no external dependencies
  • Memory Safe β€” Explicit allocator-based memory management
  • v1.5 Features β€” Key folding and path expansion support

Quick Example

JSON (40 bytes):

{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}

TOON (28 bytes) β€” 30% smaller:

users[2]{id,name}:
  1,Alice
  2,Bob

Installation

Add to your build.zig.zon:

.dependencies = .{
    .toon = .{
        .url = "https://github.com/copyleftdev/toon-zig/archive/refs/tags/v0.1.0.tar.gz",
        .hash = "...",
    },
},

Then in build.zig:

const toon = b.dependency("toon", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("toon", toon.module("toon"));

Usage

Encoding (JSON β†’ TOON)

const std = @import("std");
const toon = @import("toon");

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

    // Create a value
    var obj = toon.JsonValue.initObject(allocator);
    defer obj.deinit(allocator);

    const name_key = try allocator.dupe(u8, "name");
    try obj.asObject().?.put(name_key, toon.JsonValue.initString("Alice"));

    const age_key = try allocator.dupe(u8, "age");
    try obj.asObject().?.put(age_key, toon.JsonValue.initInteger(30));

    // Encode to TOON
    const encoded = try toon.encode(allocator, obj, .{});
    defer allocator.free(encoded);

    std.debug.print("{s}\n", .{encoded});
    // Output:
    // name: Alice
    // age: 30
}

Decoding (TOON β†’ JSON)

const std = @import("std");
const toon = @import("toon");

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

    const input =
        \\users[2]{id,name}:
        \\  1,Alice
        \\  2,Bob
    ;

    var decoded = try toon.decode(allocator, input, .{});
    defer decoded.deinit(allocator);

    // Access the data
    const users = decoded.asConstObject().?.get("users").?;
    const first_user = users.asConstArray().?.items[0];
    const name = first_user.asConstObject().?.get("name").?.asString().?;

    std.debug.print("First user: {s}\n", .{name}); // Alice
}

Encoder Options

const options = toon.EncodeOptions{
    .indent = 4,                    // Spaces per level (default: 2)
    .delimiter = .tab,              // .comma (default), .tab, or .pipe
    .key_folding = .safe,           // .off (default) or .safe
    .flatten_depth = 3,             // Max folding depth (default: max)
};

const encoded = try toon.encode(allocator, value, options);

Decoder Options

const options = toon.DecodeOptions{
    .indent = 2,                    // Expected indent size (default: 2)
    .strict = true,                 // Strict validation (default: true)
    .expand_paths = .safe,          // .off (default) or .safe
};

var decoded = try toon.decode(allocator, input, options);

TOON Format Overview

TOON is designed for efficient structured data in LLM prompts:

Objects

name: Alice
age: 30
active: true

Nested Objects

user:
  id: 123
  profile:
    name: Ada

Primitive Arrays (Inline)

tags[3]: admin,ops,dev

Tabular Arrays

users[2]{id,name,role}:
  1,Alice,admin
  2,Bob,user

Arrays of Arrays

matrix[2]:
  - [3]: 1,2,3
  - [3]: 4,5,6

Mixed Arrays

items[3]:
  - 42
  - name: widget
  - hello

Delimiter Variations

# Tab-delimited
data[2	]{id	name}:
  1	Alice
  2	Bob

# Pipe-delimited
tags[3|]: a|b|c

API Reference

Types

  • JsonValue β€” Union type representing JSON values (null, bool, integer, float, string, array, object)
  • JsonArray β€” ArrayList(JsonValue)
  • JsonObject β€” StringArrayHashMap(JsonValue) with preserved insertion order
  • Delimiter β€” .comma, .tab, .pipe
  • ToonError β€” Error type for encoding/decoding failures

Functions

  • encode(allocator, value, options) β€” Encode JsonValue to TOON string
  • decode(allocator, input, options) β€” Decode TOON string to JsonValue
  • encodeDefault(allocator, value) β€” Encode with default options
  • decodeDefault(allocator, input) β€” Decode with default options

JsonValue Methods

  • initNull(), initBool(b), initInteger(i), initFloat(f), initString(s) β€” Create primitives
  • initArray(allocator), initObject(allocator) β€” Create containers
  • clone(allocator) β€” Deep copy
  • deinit(allocator) β€” Free memory
  • eql(other) β€” Deep equality comparison
  • parseJson(allocator, json_str) β€” Parse from JSON string
  • toJsonString(allocator) β€” Serialize to JSON string

Building & Testing

# Build library
zig build

# Run unit tests
zig build test

# Run fixture tests (requires test fixtures)
zig build test-fixtures

# Run all tests
zig build test-all

Specification Compliance

This implementation targets TOON Specification v3.0 and implements:

  • βœ… Canonical number formatting (no exponent, no trailing zeros)
  • βœ… String escaping (only \\, \", \n, \r, \t)
  • βœ… Quoting rules per Β§7.2
  • βœ… Key encoding per Β§7.3
  • βœ… Object encoding with preserved key order
  • βœ… Primitive arrays (inline)
  • βœ… Tabular arrays with field lists
  • βœ… Arrays of arrays (expanded)
  • βœ… Mixed arrays (expanded)
  • βœ… Objects as list items per Β§10
  • βœ… Delimiter handling (comma, tab, pipe)
  • βœ… Strict mode validation
  • βœ… Key folding (encoder)
  • βœ… Path expansion (decoder)

Resources

Contributing

Contributions are welcome! Please ensure:

  1. All tests pass (zig build test-all)
  2. Code follows Zig style conventions
  3. New features include tests
  4. Spec compliance is maintained

License

MIT License β€” see LICENSE for details.

About

πŸ”· Zig implementation of TOON (Token-Oriented Object Notation) β€” Compact, human-readable JSON encoding for LLM prompts with 30-60% token reduction. Spec-compliant v3.0 implementation.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors