Skip to content

Commit 4056140

Browse files
chore: initial commit
0 parents  commit 4056140

File tree

13 files changed

+420
-0
lines changed

13 files changed

+420
-0
lines changed

.gitignore

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
pip-wheel-metadata/
20+
share/python-wheels/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
MANIFEST
25+
26+
# Virtual environments
27+
.env
28+
.venv
29+
env/
30+
venv/
31+
ENV/
32+
env.bak/
33+
venv.bak/
34+
35+
# IDEs
36+
.vscode/
37+
.idea/
38+
*.swp
39+
*.swo
40+
*~
41+
.DS_Store
42+
43+
# Testing
44+
.pytest_cache/
45+
.coverage
46+
htmlcov/
47+
.tox/
48+
.nox/
49+
50+
# Type checking
51+
.mypy_cache/
52+
.pytype/
53+
.pyre/
54+
.pyright/
55+
56+
# uv
57+
.uv/
58+
uv.lock

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.14

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025-PRESENT Johann Schopplich
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# TOON Format for Python
2+
3+
[![PyPI version](https://img.shields.io/pypi/v/toon-format.svg)](https://pypi.org/project/toon-format/)
4+
[![Python versions](https://img.shields.io/pypi/pyversions/toon-format.svg)](https://pypi.org/project/toon-format/)
5+
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
6+
7+
**Token-Oriented Object Notation** is a compact, human-readable format designed for passing structured data to Large Language Models with significantly reduced token usage.
8+
9+
## Status
10+
11+
🚧 **This package is currently a namespace reservation.** Full implementation coming soon!
12+
13+
### Example
14+
15+
**JSON** (verbose):
16+
```json
17+
{
18+
"users": [
19+
{ "id": 1, "name": "Alice", "role": "admin" },
20+
{ "id": 2, "name": "Bob", "role": "user" }
21+
]
22+
}
23+
```
24+
25+
**TOON** (compact):
26+
```
27+
users[2]{id,name,role}:
28+
1,Alice,admin
29+
2,Bob,user
30+
```
31+
32+
## Resources
33+
34+
- [TOON Specification](https://github.com/johannschopplich/toon/blob/main/SPEC.md)
35+
- [Main Repository](https://github.com/johannschopplich/toon)
36+
- [Benchmarks & Performance](https://github.com/johannschopplich/toon#benchmarks)
37+
- [Other Language Implementations](https://github.com/johannschopplich/toon#other-implementations)
38+
39+
## Future Usage
40+
41+
Once implemented, the package will provide:
42+
43+
```python
44+
from toon_format import encode, decode
45+
46+
data = # your data structure
47+
toon_string = encode(data)
48+
decoded = decode(toon_string)
49+
```
50+
51+
## Contributing
52+
53+
Interested in implementing TOON for Python? Check out the [specification](https://github.com/johannschopplich/toon/blob/main/SPEC.md) and feel free to contribute!
54+
55+
## License
56+
57+
MIT License © 2025-PRESENT [Johann Schopplich](https://github.com/johannschopplich)

pyproject.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[project]
2+
name = "toon-format"
3+
version = "0.1.0"
4+
description = "Token-Oriented Object Notation – a token-efficient JSON alternative for LLM prompts"
5+
readme = "README.md"
6+
authors = [
7+
{ name = "Johann Schopplich", email = "[email protected]" }
8+
]
9+
requires-python = ">=3.10"
10+
dependencies = []
11+
license = { text = "MIT" }
12+
keywords = ["toon", "serialization", "llm", "data-format", "token-efficient"]
13+
classifiers = [
14+
"Development Status :: 3 - Alpha",
15+
"Intended Audience :: Developers",
16+
"License :: OSI Approved :: MIT License",
17+
"Programming Language :: Python :: 3",
18+
"Programming Language :: Python :: 3.10",
19+
"Programming Language :: Python :: 3.11",
20+
"Programming Language :: Python :: 3.12",
21+
"Programming Language :: Python :: 3.13",
22+
"Programming Language :: Python :: 3.14",
23+
"Topic :: Software Development :: Libraries :: Python Modules",
24+
]
25+
26+
[project.urls]
27+
Homepage = "https://toonformat.dev"
28+
Repository = "https://github.com/toon-format/toon-python"
29+
Documentation = "https://github.com/toon-format/toon"
30+
"Bug Tracker" = "https://github.com/toon-format/toon-python/issues"
31+
32+
[dependency-groups]
33+
dev = ["pytest>=8.0.0"]
34+
35+
[tool.pytest.ini_options]
36+
testpaths = ["tests"]
37+
python_files = ["test_*.py"]
38+
python_functions = ["test_*"]
39+
40+
[build-system]
41+
requires = ["uv_build>=0.9.7,<0.10.0"]
42+
build-backend = "uv_build"

src/toon_format/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Token-Oriented Object Notation (TOON) for Python.
3+
4+
A compact, human-readable format designed for passing structured data
5+
to Large Language Models with significantly reduced token usage.
6+
"""
7+
8+
from toon_format.decoder import decode
9+
from toon_format.encoder import encode
10+
from toon_format.types import DecodeOptions, EncodeOptions
11+
12+
__version__ = "0.1.0"
13+
__all__ = ["encode", "decode", "EncodeOptions", "DecodeOptions"]

src/toon_format/decoder.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""TOON decoder implementation."""
2+
3+
from toon_format.types import DecodeOptions, JsonValue
4+
5+
6+
def decode(input: str, options: DecodeOptions | None = None) -> JsonValue:
7+
"""Convert a TOON-formatted string to a Python value.
8+
9+
Args:
10+
input: A TOON-formatted string to parse
11+
options: Optional decoding options:
12+
- indent: Expected number of spaces per indentation level (default: 2)
13+
- strict: Enable strict validation (default: True)
14+
15+
Returns:
16+
A Python value (dict, list, or primitive) representing the parsed TOON data.
17+
18+
Raises:
19+
ValueError: If the input is malformed (when strict=True)
20+
21+
Examples:
22+
>>> decode('items[2]{sku,qty}:\\n A1,2\\n B2,1')
23+
{'items': [{'sku': 'A1', 'qty': 2}, {'sku': 'B2', 'qty': 1}]}
24+
25+
>>> decode('tags[2]: foo,bar')
26+
{'tags': ['foo', 'bar']}
27+
28+
>>> decode('[3]: 1,2,3')
29+
[1, 2, 3]
30+
"""
31+
raise NotImplementedError("TOON decoder is not yet implemented")

src/toon_format/encoder.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""TOON encoder implementation."""
2+
3+
from typing import Any
4+
5+
from toon_format.types import EncodeOptions
6+
7+
8+
def encode(value: Any, options: EncodeOptions | None = None) -> str:
9+
"""Convert a value to TOON format.
10+
11+
Args:
12+
value: Any JSON-serializable value (object, array, primitive, or nested structure).
13+
Non-JSON-serializable values (functions, undefined, non-finite numbers) are
14+
converted to null. Dates are converted to ISO strings, and BigInts are emitted
15+
as decimal integers.
16+
options: Optional encoding options:
17+
- indent: Number of spaces per indentation level (default: 2)
18+
- delimiter: Delimiter for array values and tabular rows (default: ',')
19+
- length_marker: Optional marker to prefix array lengths (default: False)
20+
21+
Returns:
22+
A TOON-formatted string with no trailing newline or spaces.
23+
24+
Examples:
25+
>>> encode({"items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1}]})
26+
'items[2]{sku,qty}:\\n A1,2\\n B2,1'
27+
28+
>>> encode({"tags": ["foo", "bar"]}, {"delimiter": "\\t"})
29+
'tags[2 ]: foo bar'
30+
31+
>>> encode([1, 2, 3], {"length_marker": "#"})
32+
'[#3]: 1,2,3'
33+
"""
34+
raise NotImplementedError("TOON encoder is not yet implemented")

src/toon_format/py.typed

Whitespace-only changes.

src/toon_format/types.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Type definitions for TOON encoder and decoder."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any, Literal, TypeAlias, TypedDict
6+
7+
# JSON-compatible types
8+
JsonPrimitive: TypeAlias = str | int | float | bool | None
9+
JsonValue: TypeAlias = JsonPrimitive | dict[str, "JsonValue"] | list["JsonValue"]
10+
JsonObject: TypeAlias = dict[str, JsonValue]
11+
JsonArray: TypeAlias = list[JsonValue]
12+
13+
14+
class EncodeOptions(TypedDict, total=False):
15+
"""Options for encoding values to TOON format.
16+
17+
Attributes:
18+
indent: Number of spaces per indentation level (default: 2)
19+
delimiter: Delimiter for array values and tabular rows (default: ',')
20+
length_marker: Optional marker to prefix array lengths (default: False)
21+
"""
22+
23+
indent: int
24+
delimiter: Literal[",", "\t", "|"]
25+
length_marker: Literal["#", False]
26+
27+
28+
class DecodeOptions(TypedDict, total=False):
29+
"""Options for decoding TOON format to values.
30+
31+
Attributes:
32+
indent: Expected number of spaces per indentation level (default: 2)
33+
strict: Enable strict validation (default: True)
34+
"""
35+
36+
indent: int
37+
strict: bool

0 commit comments

Comments
 (0)