Skip to main content

A high-performance TOON (Token Oriented Object Notation) parser and serializer for Python, implemented in Rust.

Project description

TOONS - Token Oriented Object Notation

A fast Rust-based parser and serializer for the TOON format (Token Oriented Object Notation), a token-efficient data serialization format designed specifically for Large Language Models.

This library provides a Python interface that mirrors the API of Python's standard json module, making it easy to work with TOON-formatted strings and files.

Architecture

The library is implemented in Rust using PyO3 for Python bindings, providing four core functions that parallel the standard library's json module:

  • load(file) - Parse TOON data from a file object
  • loads(string) - Parse TOON data from a string
  • dump(obj, file) - Serialize Python object to TOON format and write to file
  • dumps(obj) - Serialize Python object to TOON format string

The implementation follows the TOON Specification v1.3, ensuring proper support for:

  • Indentation-based structure
  • Array notation with element count (e.g., tags[3]: admin,ops,dev)
  • Tabular format for uniform object arrays
  • Configurable delimiters (comma, pipe, or tab)
  • Unquoted keys and string values

Features

  • Fast serialization/deserialization: Implemented in Rust with PyO3 bindings
  • TOON Spec v1.3 compliant: Full support for the official TOON specification
  • Token-efficient: 30-60% fewer tokens than equivalent JSON, ideal for LLM contexts
  • Familiar API: Mirrors Python's json module interface (load, loads, dump, dumps)
  • Python native types: Returns/accepts Python dict, list, str, int, float, bool, None
  • File and string operations: Complete support for both file I/O and string operations

Installation

Development Installation

# Install development dependencies
pip install -e ".[dev]"

# Build the Rust extension
maturin develop

Production Installation

# Build wheel
maturin build --release

# Install the wheel
pip install target/wheels/*.whl

Usage

The API mirrors Python's json module for easy adoption:

String Operations

import toons

# Parse TOON string (loads)
toon_string = """
name: John Doe
age: 30
tags[3]: admin,developer,ops
"""
data = toons.loads(toon_string)
print(data)  # {'name': 'John Doe', 'age': 30, 'tags': ['admin', 'developer', 'ops']}

# Serialize to TOON string (dumps)
data = {
    "name": "John Doe",
    "age": 30,
    "tags": ["admin", "developer", "ops"]
}
toon_output = toons.dumps(data)
print(toon_output)
# Output:
# name: John Doe
# age: 30
# tags[3]: admin,developer,ops

File Operations

import toons

# Parse TOON file (load)
with open('data.toon', 'r') as f:
    data = toons.load(f)
    print(data)

# Serialize to TOON file (dump)
data = {"users": [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]}
with open('output.toon', 'w') as f:
    toons.dump(data, f)

TOON Format Examples

Simple object:

name: John
age: 30
active: true

Array notation:

tags[3]: admin,ops,dev

Nested structure:

user:
  name: John
  contacts:
    email: john@example.com
    phone: 555-1234

Tabular format (uniform objects):

users[2]{name,age}:
  Alice,25
  Bob,30

Supported Data Types

Python Type TOON Format Example
dict Indented key-value pairs name: John\nage: 30
list Array notation with count tags[3]: a,b,c
str Unquoted (when safe) name: John
int Number literal age: 30
float Decimal literal price: 19.99
bool true/false active: true
None null value: null

Note: The TOON format is significantly more token-efficient than JSON, especially for arrays and nested structures, making it ideal for LLM applications.

API Reference

loads(s: str) -> Any

Parse a TOON-formatted string and return the corresponding Python object.

Arguments:

  • s (str): TOON-formatted string to parse

Returns:

  • Python object (dict, list, str, int, float, bool, or None)

Raises:

  • ValueError: If the string is not valid TOON format

load(fp: IO[str]) -> Any

Parse TOON data from a file object and return the corresponding Python object.

Arguments:

  • fp: File-like object supporting .read()

Returns:

  • Python object (dict, list, str, int, float, bool, or None)

Raises:

  • ValueError: If the file content is not valid TOON format

dumps(obj: Any) -> str

Serialize a Python object to a TOON-formatted string.

Arguments:

  • obj: Python object to serialize

Returns:

  • TOON-formatted string

Raises:

  • ValueError: If the object cannot be serialized to TOON format

dump(obj: Any, fp: IO[str]) -> None

Serialize a Python object to TOON format and write to a file object.

Arguments:

  • obj: Python object to serialize
  • fp: File-like object supporting .write()

Raises:

  • ValueError: If the object cannot be serialized to TOON format

Error Handling

The library raises ValueError with descriptive error messages for invalid TOON syntax:

try:
    result = toons.loads('invalid toon syntax')
except ValueError as e:
    print(f"Parse error: {e}")

Examples

See the examples/ directory for simple usage examples:

# String operations (loads/dumps)
python examples/string_example.py

# File operations (load/dump)
python examples/file_example.py

Testing

Run the test suite with pytest:

# Run all tests
pytest

# Run with coverage
pytest --cov=toons

# Run specific test file
pytest tests/unit/test_loads.py

# Run specific test
pytest tests/unit/test_loads.py -k test_loads_simple_object

Development

Prerequisites

  • Python 3.8+
  • Rust (latest stable)
  • maturin

Building

# Development build
maturin develop

# Release build
maturin build --release

Project Structure

toons/
├── src/
│   └── lib.rs              # Rust implementation
├── tests/
│   └── unit/               # Unit tests
│       ├── test_loads.py   # Tests for loads()
│       ├── test_dumps.py   # Tests for dumps()
│       ├── test_load.py    # Tests for load()
│       ├── test_dump.py    # Tests for dump()
│       └── test_roundtrip.py
├── examples/               # Usage examples
├── TOON_SPEC_1.3.md       # TOON specification reference
├── Cargo.toml             # Rust dependencies
├── pyproject.toml         # Python project config
└── README.md

TOON Format Specification

This library implements TOON Specification v1.3 (2025-10-31) using the rtoon Rust crate (v0.1.3).

Note: rtoon implements TOON Spec v1.2, which is fully compatible with v1.3. The specification is included in the repository as TOON_SPEC_1.3.md for reference.

Compliance Verification

The test suite in tests/unit/test_spec_compliance.py contains 40 tests verifying compliance with TOON Spec v1.3:

  • ✅ All primitive types (null, bool, int, float, string)
  • ✅ Object encoding with key-value pairs and 2-space indentation
  • ✅ Array notation with element count [N]:
  • ✅ Tabular format for uniform object arrays [N]{fields}:
  • ✅ Nested structures and complex data
  • ✅ Round-trip fidelity for all data types
  • ✅ File I/O operations

Run tests: pytest tests/unit/test_spec_compliance.py -v

Why rtoon?

The rtoon crate was chosen as the implementation backend because it provides:

  • Full round-trip support: Both encoding (serialization) and decoding (parsing) with complete fidelity
  • TOON Spec v1.2 compliance: Follows the official TOON specification
  • Robust parsing: Strict mode validation ensures data integrity
  • Active maintenance: Updated regularly with the latest specification changes
  • Well-tested: Comprehensive test suite ensures reliability

For more details on rtoon, see: https://github.com/shreyasbhat0/rtoon

License

This project is open source. See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toons-0.1.1.tar.gz (34.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

toons-0.1.1-cp37-abi3-win_amd64.whl (209.7 kB view details)

Uploaded CPython 3.7+Windows x86-64

toons-0.1.1-cp37-abi3-win32.whl (209.1 kB view details)

Uploaded CPython 3.7+Windows x86

toons-0.1.1-cp37-abi3-musllinux_1_2_x86_64.whl (527.0 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ x86-64

toons-0.1.1-cp37-abi3-musllinux_1_2_i686.whl (563.1 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ i686

toons-0.1.1-cp37-abi3-musllinux_1_2_armv7l.whl (631.7 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARMv7l

toons-0.1.1-cp37-abi3-musllinux_1_2_aarch64.whl (533.9 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

toons-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (368.6 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

toons-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (392.6 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ s390x

toons-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (483.9 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ppc64le

toons-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (368.2 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARMv7l

toons-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (354.4 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

toons-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (387.7 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.5+ i686

toons-0.1.1-cp37-abi3-macosx_11_0_arm64.whl (321.4 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

toons-0.1.1-cp37-abi3-macosx_10_12_x86_64.whl (329.5 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file toons-0.1.1.tar.gz.

File metadata

  • Download URL: toons-0.1.1.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for toons-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b46771015c8e9847c535f7881a96eaa6e62d128842e61db81345f4d2be922e42
MD5 de02366d5f0f4ee233428915d7bf81ac
BLAKE2b-256 bb9d898a176f5e14aa08a2edc06aaf77460e64afa9678cb1638dd4977160661c

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: toons-0.1.1-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 209.7 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for toons-0.1.1-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7f5fa00ca2b12b8dac8f2c47762c5877cdcdccf720c28f8b7a20ca87818c6423
MD5 c9a55c5d7c3113700ad1b8599389e523
BLAKE2b-256 f748f63c376fcf665bdaf24d114a35c8bec16c3d71a3ad5a835d840e1e4671d5

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-win32.whl.

File metadata

  • Download URL: toons-0.1.1-cp37-abi3-win32.whl
  • Upload date:
  • Size: 209.1 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for toons-0.1.1-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 d1913b0fe524f9ac3179acd7f41885e55bfd624d714bbbc3708612d076db3624
MD5 4c13715a396d564bc7bcafe3ea4d9e67
BLAKE2b-256 f3dc8e7858ec1f56e4c524c70fa729369438ca494b70ae2a1f1c3436ca723782

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4b01b32b43832eba00217e27d4e83c6e1a7ada0eaa3d6dd89d926b3802b3c265
MD5 7bcba8038dcc66aa2fa80812e1e57551
BLAKE2b-256 56d14bdfbb3ef01a31cabf338a27944dab4a351ed4177bd2ce95d5e0ff9d20a4

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 83910020f35acf6256c47d6b6d9f3e196560baa90ff1a9210a9897d4e41ca2a2
MD5 597147764473cf1e598b2d99edfd8831
BLAKE2b-256 dfe57ee72670156576b7b6ab90e8157357d30e6e0d975280b92fd9a57509f798

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 9361a94fca043305184042fb01c0d3ba5fd8869816be07c65fc60e07924c09c6
MD5 2de89ed3307aa9a9761c38fbe947f370
BLAKE2b-256 180eaafe6098a2e2992a92bbe746a26775e4e59c95cada754091da6bde171155

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d72817ee2a11314ec113ad6c33e908d817c944a0622706c362a7a478247639cc
MD5 e2ccddd956a0c868fcede5871c8ace85
BLAKE2b-256 15fbe966fdceaf46c213ea63d35c034dec1a7871d6808349eeb3d98780881fae

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 afd0bb474b3c05cecba2e497c0be19bc8c579925211fa744ac49356eec39ba3d
MD5 be3ab1a4b554b93b4d5373b3800d1299
BLAKE2b-256 26c7a85b24755c0bdbbbe0eb0fef365dc5abf634056956674947a50cb18d55dd

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 f45a93919bcb2c6ef0124aad75a221ff85cbea5fac5ac1950e56c2569b173ecd
MD5 412e97d800b5f628d9e7d5cf1df1ffa4
BLAKE2b-256 7e075ed8ca6045c4fb475fa02aea5862d820d523d79d13889f07b10cf0cfbf14

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 8732709d3e08dcbfe7a08e41e4e52e6bad52eeecc6f1656eb13804cfbe97f027
MD5 2fb67a6e052c19b0c00614998706e041
BLAKE2b-256 7a125820feb2923aae30bc70d20dd2f4e966936c476e18298595bcfd0ff73ed3

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 ce37944c979069c5c3bdd56aa5afafc64d26c757d871294a0775f674002b9f53
MD5 b7df551d779d0bf2f38a2aaaed75c9c9
BLAKE2b-256 e50ecbfaf4a76e9dbf13eeab846f5d2b4503b5ac5942136105a75a7faa191761

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1394fe33fc95ac85d5f5cf4a753a5f7cd11f26448c7b2a8adddfb8be95eb288f
MD5 00905d701d167e1d350c3f106d96e63b
BLAKE2b-256 181f2b47c0a2d7fa69659a91817897c3bb345ba7eb15cac40b1c587c4c2efb8c

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 152dab283e9f14da0bf2bd1ebb509ec4a5b990b3a1683540409f2de3dd91bedd
MD5 15c1267860ef3c93afdf2ccda31f8ea5
BLAKE2b-256 b104198bf0c6707abc4e20a86f321c9d6bedb06134020197ce7e8c31e7bcef60

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ea534d7d7750278de64b6da7c1558bd2b20023c9dc20aefd2fcb2c1ede218c40
MD5 5d03c901e4f0272408da89ece82c0a32
BLAKE2b-256 38bbac0f417e93a4e9e395e6c9c086bbdda109c08b29329acb6dc4a267d293ee

See more details on using hashes here.

File details

Details for the file toons-0.1.1-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for toons-0.1.1-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b2d293832e40999dbc11fd7b5ffcd4031b651fd9409c25b2e9efcc3849f56cc8
MD5 8ba8cd6dd4e4fd324a6b4c93efe47ae5
BLAKE2b-256 8369920356da0099d8c4e599c86401e5311017690eee9e04fa57d425d6b0d1a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page