A Swift decoder for TOON (Token-Oriented Object Notation), a compact format designed to reduce LLM token usage by 30–60% compared with JSON.
LLM tokens are expensive, and JSON is verbose. TOON saves tokens while remaining human-readable by using indentation for structure and a tabular format for uniform data:
JSON:
{
"users": [
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "user" }
]
}TOON:
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
For full details on TOON's design, benchmarks, and specification, see the TOON specification.
TOONDecoder conforms to TOON specification version 3.0 (2025-11-24)
and implements the following features:
- Correct escape sequence parsing (
\\,\",\n,\r,\t) - Three delimiter types: comma (default), tab, pipe
- Array length validation
- Tabular format parsing with field headers
- Inline format for primitive arrays
- Expanded list format for nested structures
- Path expansion to unfold dotted keys into nested objects (inverse of key folding)
- Detailed error reporting with line numbers
- Swift 6.0+ / Xcode 16+
- iOS 13.0+ / macOS 10.15+ / watchOS 6.0+ / tvOS 13.0+ / visionOS 1.0+ / Linux
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/alexey1312/TOONDecoder.git", from: "0.1.0")
]import TOONDecoder
struct User: Codable {
let id: Int
let name: String
let tags: [String]
let active: Bool
}
let toon = """
id: 123
name: Ada
tags[2]: reading,gaming
active: true
"""
let decoder = TOONDecoder()
let user = try decoder.decode(User.self, from: Data(toon.utf8))
print(user.name) // "Ada"struct Item: Codable {
let sku: String
let qty: Int
let price: Double
}
struct Order: Codable {
let items: [Item]
}
let toon = """
items[2]{sku,qty,price}:
A1,2,9.99
B2,1,14.5
"""
let decoder = TOONDecoder()
let order = try decoder.decode(Order.self, from: Data(toon.utf8))
print(order.items.count) // 2Path expansion unfolds dotted keys into nested objects — the inverse of TOONEncoder's key folding:
struct Config: Codable {
struct Database: Codable {
struct Connection: Codable {
let host: String
let port: Int
}
let connection: Connection
}
let database: Database
}
let toon = """
database.connection.host: localhost
database.connection.port: 5432
"""
let decoder = TOONDecoder()
decoder.expandPaths = .safe
let config = try decoder.decode(Config.self, from: Data(toon.utf8))
print(config.database.connection.host) // "localhost"Check the supported TOON specification version:
print(TOONDecoder.specVersion) // "3.0"This project is available under the MIT license. See the LICENSE file for more info.