toons: High-Performance TOON Parser and Serializer

0.5.4 · active · verified Thu Apr 09

toons is a high-performance Python library for parsing and serializing TOON (Token Oriented Object Notation), implemented in Rust for speed. It offers an API that mirrors Python's built-in `json` module, making it easy to convert Python objects to TOON strings and vice-versa. TOON is a token-efficient data serialization format specifically designed to reduce token counts for Large Language Models. The current version is 0.5.4, with frequent minor releases addressing fixes and specification compliance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to serialize a Python dictionary and a list of dictionaries (tabular data) into TOON format, and then parse a TOON string back into a Python object using `toons.dumps` and `toons.loads`, mirroring the standard `json` library API.

import toons

# Sample Python data
data = {
    "name": "Alice",
    "age": 30,
    "tags": ["python", "rust", "toon"]
}

# Serialize Python data to TOON string
toon_string = toons.dumps(data)
print("--- TOON String ---")
print(toon_string)
# Expected output (simplified, actual format may vary slightly based on spec):
# name: Alice
# age: 30
# tags[3]: python,rust,toon

# Parse TOON string back to Python data
parsed_data = toons.loads(toon_string)
print("\n--- Parsed Python Data ---")
print(parsed_data)
# Expected output:
# {'name': 'Alice', 'age': 30, 'tags': ['python', 'rust', 'toon']}

# Example with tabular data (common for LLMs)
users = [
    {"id": 1, "name": "Bob", "active": True},
    {"id": 2, "name": "Charlie", "active": False}
]
tabular_toon = toons.dumps(users)
print("\n--- Tabular TOON String ---")
print(tabular_toon)
# Expected output (simplified):
# [2]{id,name,active}:
# 1,Bob,true
# 2,Charlie,false

view raw JSON →