TOON API Reference¶
toon
¶
TOON encoder/decoder — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Drop-in replacement for toon_format core functionality (encode/decode).
TOON is a compact, human-readable serialization format designed for LLM contexts, achieving 30-60% token reduction vs JSON. It combines YAML-like indentation with CSV-like tabular arrays.
Example::
encode({"name": "Alice", "age": 30})
# 'name: Alice
age: 30' decode("name: Alice age: 30") # {'name': 'Alice', 'age': 30}
EncodeOptions
¶
Bases: TypedDict
Options for TOON encoding.
Attributes:
| Name | Type | Description |
|---|---|---|
indent |
int
|
Number of spaces per indentation level (default: 2). |
delimiter |
str
|
Delimiter character for arrays (default: ","). |
lengthMarker |
Union[Literal['#'], Literal[False]]
|
Marker to prefix array lengths (default: False). |
Source code in toon/toon.py
DecodeOptions
¶
Bases: TypedDict
Options for TOON decoding.
Attributes:
| Name | Type | Description |
|---|---|---|
indent |
int
|
Number of spaces per indentation level (default: 2). |
strict |
bool
|
Enable strict validation (default: True). |
Source code in toon/toon.py
ToonDecodeError
¶
encode(value, options=None)
¶
Encode a Python value into TOON format.
Args:
value: The value to encode (must be JSON-serializable or a supported
Python type such as datetime, Decimal, set, Path).
options: Optional encoding options (indent, delimiter, lengthMarker).
Returns:
TOON-formatted string.
Examples::
encode({"name": "Alice", "age": 30})
# 'name: Alice
age: 30' encode([{"id": 1, "name": "A"}, {"id": 2, "name": "B"}]) # '[2,]{id,name}: 1,A 2,B'
Source code in toon/toon.py
decode(text, options=None)
¶
Decode a TOON-formatted string to a Python value.
Args:
text: TOON-formatted string.
options: Optional decoding options (indent, strict).
Returns:
Decoded Python value.
Raises:
ToonDecodeError: If the input is malformed.
Examples::
decode("name: Alice
age: 30") # {'name': 'Alice', 'age': 30} decode("[3]: 1,2,3") # [1, 2, 3]