Skip to main content

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

Project description

TOONS - a TOON Serializer

This is 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

Production Installation

pip install toons

Development Installation

# Install development dependencies
pip install -r requirements-dev.txt
pip insta

# Build the Rust extension
maturin develop

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
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.2.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.2-cp37-abi3-win_amd64.whl (208.6 kB view details)

Uploaded CPython 3.7+Windows x86-64

toons-0.1.2-cp37-abi3-win32.whl (208.8 kB view details)

Uploaded CPython 3.7+Windows x86

toons-0.1.2-cp37-abi3-musllinux_1_2_x86_64.whl (527.3 kB view details)

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

toons-0.1.2-cp37-abi3-musllinux_1_2_i686.whl (562.9 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ i686

toons-0.1.2-cp37-abi3-musllinux_1_2_armv7l.whl (630.9 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARMv7l

toons-0.1.2-cp37-abi3-musllinux_1_2_aarch64.whl (533.6 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

toons-0.1.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (368.2 kB view details)

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

toons-0.1.2-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (391.8 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ s390x

toons-0.1.2-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (483.7 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ppc64le

toons-0.1.2-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (367.5 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARMv7l

toons-0.1.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (354.0 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

toons-0.1.2-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (388.1 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.5+ i686

toons-0.1.2-cp37-abi3-macosx_11_0_arm64.whl (321.0 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

toons-0.1.2-cp37-abi3-macosx_10_12_x86_64.whl (329.1 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: toons-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 19b3ef8a8c081e0e2ac813d36b989706b3b6009dd30f7d084e5675e020ecbe08
MD5 5e093905cbe3fbfeedce89e96e48875c
BLAKE2b-256 7b55c201bc5aa1d5f1bd02f2ba7e77b8013b041bec3e5ffd4cb1c51e00ee3f0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: toons-0.1.2-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 208.6 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.2-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 26708bc0ba2c7b70c7e5c9726bec4a433cb2f2a3786e5747c2882ff1dfe0a75b
MD5 dc984d97b491ee9e1bc845100091e485
BLAKE2b-256 05b19cb1c3062ceb344916ec8eab784d3ac79014d89e6f25f602736470762fba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: toons-0.1.2-cp37-abi3-win32.whl
  • Upload date:
  • Size: 208.8 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.2-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 ec7eb5d7ac576fc9564218f7052377a295333e37f94f425041bbaeb5923a4648
MD5 6a1394dbacb2c70511af7f7aee269ce6
BLAKE2b-256 2a01e4e8a70556aff3da35c9bde0efc353e013c730d950dcc36877eeae1d1862

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fcb22eccfc8b2a9701c7046edd41bea71a0c2fa57a92711375f86e3ab8cf20b9
MD5 5fee2626b31404dcf75fbdd20a3aa0d5
BLAKE2b-256 e057dc13def3db2b0e56fe137e87ea3b2b12e39e279a283d8125f43fa1bac49e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 132e2e9e9bcbaa5599752a4be3ef43901ce0a5d560862f3c5b156bb8640e8f9e
MD5 4f789629e6de7bf968ab28fa375c3f20
BLAKE2b-256 1a4da9ce1b32d8719d8c7869b2d8f5f3877f5452d6d5f4189d2dbd8904b34e6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 88339ce3b5a7c5349517d4ff1735b4d71b8e9415ccbd5f286c343ab091debada
MD5 4fd02af5dcb5181b4a563f1f68ef46fd
BLAKE2b-256 27768b9084bf30befd0dcb7d1e905c3ee50df044fe88ff30b2ce4d9049c68160

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 271e532e1d3747fee40933fe3c54665f38b7935779a604158da56e8e5a699ea7
MD5 42cbbbc92edcef77deb0fe8f599f784c
BLAKE2b-256 f91b42581812acc64e333aced01ab273cc07037463cca7ccd9799326a6116491

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2dfe55c47337156afc9ec0c318c80b9380c206f12995d967aed979ee8c90bac9
MD5 e12cf51fee25e818af8b26127640d868
BLAKE2b-256 e869538bc831872833a3cc2454a16831dda7f52b24311e481720376d969610af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 f0deca2b35e064c4e46f0721157ca3d9229e6b335dee07016bca4a7152b78122
MD5 b76404cdcba7fe23d7afdd6c3fc2c387
BLAKE2b-256 cf6dec13feefa4a533be2c2f75a1203832b2b228af7e0ac3cf7b39153659d261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 4a12e81ffc3ff9ce3a88926722511183f15a8bdef0fac9ba84c3645cb8bacc24
MD5 2997554703962aabe93fadb70ee5d7bf
BLAKE2b-256 2d64bbe3f6d127659028c1490b9a0498689d1ab6808e783460bc8bf65a51a87c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 b46d3ab786168b1b2bc50ebcf2f1e09b831901e538c2d3e340b6f62596db9c3e
MD5 29b253ee44afadc319f4409c1e206c73
BLAKE2b-256 a7fb13819d4820ccb7e7ca5fb5f61129aeb0f8b7a3500de504c1b12272961226

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ce0eeaa3ae74ece28e1d26ff55db284993d2177623bd7b67f3db85abe0b3c8ce
MD5 e045dc3a8d02fb9becf02ea926b7d1ac
BLAKE2b-256 8a1ed0c16de9c02d463c8051b0333e0d323f5e96d607ea92c8a665f640fca511

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 c1a815b3b3340e006a16288904909c0b341599bded047c156ca4bc5b8e0ed6f7
MD5 c9bf3ee48ccff958b372e0ffdd6540c8
BLAKE2b-256 168e63f577eb012ae6f9216197bda35790170ea59e9402f21b58aa2d07e52864

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 05417428c86581c4dc590ab232c7c6894d0e7b65d8d0023fb3a01344a9675c41
MD5 145e6f3e92fcdc5f77bfc306e3ccbc9d
BLAKE2b-256 8625fb5e81ff704b65820c329d0a54261004d3cd7360f6d4319ce955485830db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for toons-0.1.2-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 212f90077bb86ac1b19d8a49bb41902b58878b0635922ea001049dc8529bb955
MD5 ca67a910806307118d998f7abbb6e9b2
BLAKE2b-256 f3e92c33be4ef7eac552efbed9eef4677ab980c0ac893403da107e706d725932

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