Toon for Phyton: Python encoder/decoder for the TOON (Token-Oriented Object Notation) format.
Project description
Toon for Phyton
Python port of ToonNet: a dependency-free encoder/decoder for TOON (Token-Oriented Object Notation), aimed at reducing JSON verbosity while keeping deterministic structure for logs and LLM prompts.
Why TOON
- Indentation-driven structure instead of
{}and[]. - Tabular arrays declare schema and delimiter once (
users[2]{id,name}). - Optional length markers (
[#N]) make streamed data predictable. - Key folding (dotted paths) plus path expansion to cut repeated keys.
Installation
Install from PyPI (recommended) or work editable:
pip install toon-for-phyton
# or for local dev
pip install -e .
No external dependencies.
Quick start
from toonforphyton import (
encode,
decode,
ToonOptions,
ToonDecodeOptions,
ToonDelimiter,
KeyFoldingMode,
PathExpansionMode,
)
payload = {
"users": [
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
]
}
options = ToonOptions(delimiter=ToonDelimiter.COMMA, use_length_marker=False)
text = encode(payload, options)
print(text)
# users[2]{id,name,role}:
# 1,Alice,admin
# 2,Bob,user
data = decode(text, ToonDecodeOptions())
print(data["users"][0]["name"]) # Alice
Inline primitive arrays
encode({"tags": ["prod", "api", "v1"]})
# tags[3]: prod,api,v1
Key folding and path expansion
folded = encode({"api": {"v1": {"status": "ok"}}},
ToonOptions(key_folding=KeyFoldingMode.SAFE))
decoded = decode(folded) # dotted keys preserved
expanded = decode(folded, ToonDecodeOptions(expand_paths=PathExpansionMode.SAFE))
Core API
encode(value, options=None) -> str: serializes dicts, lists, dataclasses, namedtuples, and objects with__dict__.decode(text, options=None) -> Any: returns native Python structures (dict/list/str/int/bool/Decimal/None).
Encoding options (ToonOptions)
indent: spaces per level (default1).delimiter:ToonDelimiter.COMMA | TAB | PIPE.use_length_marker: emit[#N]for counts.key_folding:KeyFoldingMode.OFF | SAFE.flatten_depth: max segments for folding (default unlimited).new_line: newline sequence (default\n).default_converter: optional callable for custom types.
Decoding options (ToonDecodeOptions)
indent: expected spaces per level (default1).strict: validate declared counts (defaultTrue).expand_paths:PathExpansionMode.OFF | SAFE.length_mismatch_behavior:SILENT | WARN | ERROR.warning_sink: optional list receivingToonDecodeWarningwhenWARN.
Compatibility and notes
- No external dependencies; works on Python 3.10+.
- Numbers stay as
intorDecimalwhen possible;NaN/Infinityencode asnull. - For custom types use
default_converteror expose a clean__dict__/dataclass.
Project layout
Toon for Phyton/
├── README.md
├── src/
│ └── toonforphyton/
│ ├── __init__.py
│ ├── constants.py
│ ├── decoder.py
│ ├── encoder.py
│ ├── options.py
│ └── string_utils.py
Suggested roadmap
- Publish to PyPI with metadata and tests.
- Optional reconstruction of dataclasses on decode.
- CLI for quick JSON ↔︎ TOON conversion.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file toon_for_phyton-0.1.0.tar.gz.
File metadata
- Download URL: toon_for_phyton-0.1.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
599d1d0bcd9933862019ce9d77a3003e40802931148c055f59885b0b2ffff4e8
|
|
| MD5 |
69d0cca3a86f93f11f849588ed2e9c97
|
|
| BLAKE2b-256 |
d68da7dbba5cf8936d757763cfb892f8abb520e1beab032df2e48d92ffeb5336
|
File details
Details for the file toon_for_phyton-0.1.0-py3-none-any.whl.
File metadata
- Download URL: toon_for_phyton-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68c650baa76ddec23e87add30d73a818313228be6e0ee03d30783698426ab60e
|
|
| MD5 |
8d6edc5ea33df37400f4edb3962cc8ff
|
|
| BLAKE2b-256 |
52791f2bf07621fe786e3bab573e09d60a63e780fa653f5abfe946ed83e71e90
|