Metadata-Version: 2.4
Name: toon-convertor
Version: 1.0.0
Summary: TOON – Token Optimised Object Notation. A compact, human-readable alternative to JSON.
Author: Ajinkya Sonar
License: MIT License
        
        Copyright (c) 2025 toon contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/ajinkyasonar7/toon-converter
Project-URL: Repository, https://github.com/ajinkyasonar7/toon-converter
Project-URL: Issues, https://github.com/ajinkyasonar7/toon-converter/issues
Keywords: json,serialization,toon,token,format,notation,parser,llm,ai
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Dynamic: license-file

# 🎯 TOON — Token Optimised Object Notation

[![PyPI version](https://badge.fury.io/py/toon-convertor.svg)](https://badge.fury.io/py/toon-convertor)
[![Python](https://img.shields.io/pypi/pyversions/toon-convertor.svg)](https://pypi.org/project/toon-convertor/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

**TOON** is a compact, human-readable data format designed as a token-efficient alternative to JSON.  
It is object-oriented, whitespace-aware, and fully round-trips with JSON.

---

## Why TOON?

| Feature | JSON | TOON |
|---|---|---|
| Key quotes required | ✅ Yes | ❌ No |
| Brace/comma noise | ✅ High | ❌ Minimal |
| Comments | ❌ No | ✅ Yes (`#`) |
| Typed objects | ❌ No | ✅ `@TypeName` |
| Multiline strings | ❌ Awkward | ✅ `|` block |
| Token count | Higher | **~15–30% fewer** |
| Round-trip with JSON | — | ✅ Lossless |

### Token comparison

```json
{"name": "Alice", "age": 30, "active": true, "city": "Berlin"}
```
vs
```toon
name: Alice
age: 30
active: true
city: Berlin
```

> TOON is especially useful in LLM prompts, config files, and API responses where token count matters.

---

## Installation

```bash
pip install toon-convertor
```

---

## Quick Start

```python
import toon

# --- Serialize Python → TOON ---
data = {
    "name": "Alice",
    "age": 30,
    "active": True,
    "skills": ["Python", "LangChain", "RAG"],
    "address": {"city": "Berlin", "country": "Germany"},
}

toon_str = toon.dumps(data)
print(toon_str)
```

Output:
```
name: Alice
age: 30
active: true
skills: [Python, LangChain, RAG]
address:
  city: Berlin
  country: Germany
```

```python
# --- Deserialize TOON → Python ---
obj = toon.loads(toon_str)
print(obj["name"])    # Alice
print(obj["age"])     # 25
print(obj["active"])  # True

# --- JSON ↔ TOON conversion ---
import json

json_str  = json.dumps(data)
toon_str  = toon.from_json(json_str)   # JSON → TOON
back_json = toon.to_json(toon_str)     # TOON → JSON
```

---

## TOON Format Specification

### Key-Value Pairs
```toon
name: Alice
age: 30
score: 98.6
active: true
nothing: null
```

### Strings (quotes only when needed)
```toon
city: Berlin
full_name: "Alice Smith"
bio: "Software Engineer"
```

### Nested Objects
```toon
# Indented block
address:
  city: Berlin
  pin: 10115

# Inline object
point: {x: 10, y: 20}
```

### Arrays
```toon
# Inline (simple values)
tags: [python, llm, rag]
scores: [98, 87, 76]

# Multi-line
skills:
  - Python
  - LangChain
  - FastAPI
```

### Typed Objects (OO feature)
```toon
@Person
  name: Alice
  age: 30
  role: "Engineer"
```

Parsed as:
```python
{"__type__": "Person", "name": "Alice", "age": 30, "role": "Engineer"}
```

### Comments
```toon
# Full-line comment
name: Alice  # inline comment
```

### Multiline Strings
```toon
bio: |
  Software Engineer based in Berlin.
  Loves open source and clean code.
  Building great tools daily.
```

### Type Inference

| TOON value | Python type |
|---|---|
| `42` | `int` |
| `3.14` | `float` |
| `true` / `false` | `bool` |
| `null` / `none` / `~` | `None` |
| `"hello"` | `str` |
| `hello` (bare) | `str` |

---

## API Reference

### `toon.loads(s)`
Parse a TOON string → Python object.

### `toon.load(fp)`
Parse TOON from a file-like object.

### `toon.dumps(obj, *, indent=2)`
Serialize Python object → TOON string.

### `toon.dump(obj, fp, *, indent=2)`
Serialize Python object → write to file.

### `toon.from_json(json_str, *, indent=2)`
Convert JSON string → TOON string.

### `toon.to_json(toon_str, *, indent=None, sort_keys=False)`
Convert TOON string → JSON string.

---

## CLI

TOON ships with a command-line tool:

```bash
# Convert TOON → JSON
toon to-json data.toon
toon to-json data.toon --indent 4
toon to-json data.toon --compact

# Convert JSON → TOON
toon from-json data.json

# Pipe support
cat data.json | toon from-json
cat data.toon | toon to-json

# Validate TOON
toon validate data.toon
```

---

## Use Cases

- **LLM prompts** — fewer tokens, same information
- **Config files** — more readable than JSON, less strict than YAML  
- **API responses** — compact alternative where bandwidth matters
- **Data interchange** — anywhere JSON is used but verbosity is a cost

---

## Contributing

1. Fork the repo
2. `pip install -e ".[dev]"`
3. Run tests: `pytest`
4. Submit a PR!

---

## License

MIT © toon contributors
