A lightweight library to convert between TOON (Token-Oriented Object Notation) and popular data formats (JSON, YAML, XML, CSV).
Project description
🚀 TOON Converter (Python)
A lightweight library to convert between TOON (Token-Oriented Object Notation) and popular data formats (JSON, YAML, XML, CSV).
Reduce your LLM token costs by up to 40% using the TOON format!
📦 Installation
pip install toon-parse
🚀 Quick Start
Basic Usage (Synchronous)
from toon_parse import ToonConverter
# 1. Python Object to TOON
data = {"name": "Alice", "age": 30, "active": True}
toon_string = ToonConverter.from_json(data)
print(toon_string)
# Output:
# name: "Alice"
# age: 30
# active: true
# 2. TOON to Python Object
json_output = ToonConverter.to_json(toon_string)
print(json_output)
# Output: {'name': 'Alice', 'age': 30, 'active': True}
Mixed Text Support
The library can automatically extract and convert JSON, XML, and CSV data embedded within normal text. This is perfect for processing LLM outputs.
from toon_parse import ToonConverter
# Text with embedded JSON
mixed_text = """
Here is the user profile you requested:
{
"id": 101,
"name": "Bob",
"roles": ["admin", "editor"]
}
Please verify this information.
"""
# Automatically finds JSON, converts it to TOON, and preserves surrounding text
result = ToonConverter.from_json(mixed_text)
print(result)
# Output:
# Here is the user profile you requested:
# id: 101
# name: "Bob"
# roles[2]: "admin", "editor"
# Please verify this information.
🔐 Secure Conversion Middleware (New!)
The ToonConverter can act as a secure middleware for processing encrypted data streams (e.g., from microservices). It handles the full Decrypt -> Convert -> Encrypt pipeline internally.
Supported Algorithms
- Fernet: High security (AES-128). Requires
cryptography. - XOR: Lightweight obfuscation.
- Base64: Encoding only.
Conversion Modes
"middleware": Encrypted Input → Encrypted Output (Decrypt → Convert → Re-encrypt)"ingestion": Encrypted Input → Plain Output (Decrypt → Convert)"export": Plain Input → Encrypted Output (Convert → Encrypt)"no_encryption": Standard conversion (default)
Example Workflow
from toon_parse import ToonConverter, Encryptor
from cryptography.fernet import Fernet
# Setup
key = Fernet.generate_key()
enc = Encryptor(key=key, algorithm='fernet')
converter = ToonConverter(encryptor=enc)
# --- Mode 1: Middleware (Encrypted -> Encrypted) ---
raw_data = '{"user": "Alice", "role": "admin"}'
encrypted_input = enc.encrypt(raw_data) # Simulate upstream encrypted data
# Converter decrypts, converts to TOON, and re-encrypts
encrypted_toon = converter.from_json(
encrypted_input,
conversion_mode="middleware"
)
print(f"Secure Result: {encrypted_toon}")
# --- Mode 2: Ingestion (Encrypted -> Plain) ---
plain_toon = converter.from_json(
encrypted_input,
conversion_mode="ingestion"
)
print(f"Decrypted TOON: {plain_toon}")
# --- Mode 3: Export (Plain -> Encrypted) ---
my_data = {"status": "ok"}
secure_packet = converter.from_json(
my_data,
conversion_mode="export"
)
print(f"Encrypted Output: {secure_packet}")
⚡ Async Usage
For non-blocking operations in async applications (e.g., FastAPI), use AsyncToonConverter.
import asyncio
from toon_parse import AsyncToonConverter, Encryptor
async def main():
# 1. Standard Async Usage
converter = AsyncToonConverter()
text = 'Data: <user><name>Alice</name></user>'
toon = await converter.from_xml(text)
print(toon)
# 2. Async with Secure Middleware
enc = Encryptor(algorithm='base64')
secure_converter = AsyncToonConverter(encryptor=enc)
# Decrypt -> Convert -> Encrypt (Middleware Mode)
encrypted_msg = "eyJrZXkiOiAidmFsIn0=" # Base64 for {"key": "val"}
# Use conversion_mode to specify pipeline behavior
result = await secure_converter.from_json(
encrypted_msg,
conversion_mode="middleware"
)
print(result)
asyncio.run(main())
📚 Features & Support
| Feature | JSON | XML | CSV | YAML | TOON |
|---|---|---|---|---|---|
| Python Dict/List Input | ✅ | N/A | N/A | N/A | N/A |
| Pure String Input | ✅ | ✅ | ✅ | ✅ | ✅ |
| Mixed Text Support | ✅ | ✅ | ✅ | ❌ | ❌ |
| Async Support | ✅ | ✅ | ✅ | ✅ | ✅ |
| Encryption Support | ✅ | ✅ | ✅ | ✅ | ✅ |
- Mixed Text: Finds occurrences of data formats in text (JSON, XML, CSV) and converts them in-place.
- Encryption: Supports Fernet, XOR, and Base64 middleware conversions.
⚙️ Static vs Instance Usage
Conversion Methods (from_json, to_json, etc.)
All conversion methods support both static and instance calling patterns:
from toon_parse import ToonConverter
# ✅ Static Usage (No Encryption)
toon = ToonConverter.from_json({"key": "value"})
# ✅ Instance Usage (Encryption Supported)
converter = ToonConverter(encryptor=enc)
toon = converter.from_json({"key": "value"}, conversion_mode="export")
Important:
- Static calls (
ToonConverter.from_json(...)) work but cannot use encryption features. - Instance calls are required to use
conversion_modeand encryption middleware.
The same applies to async methods.
Validate Method
The validate() method is strictly static and does not support encryption:
# ✅ Correct Usage
result = ToonConverter.validate('key: "value"')
# ❌ Will NOT work with encryption
converter = ToonConverter(encryptor=enc)
result = converter.validate(encrypted_data) # No decryption happens!
Why? Validation returns a dictionary (not a string), which cannot be encrypted. If you need to validate encrypted data, decrypt it first manually:
decrypted = enc.decrypt(encrypted_toon)
result = ToonConverter.validate(decrypted)
The same applies to AsyncToonConverter.validate().
🛠 API Reference
Core Converters
ToonConverter (Synchronous)
Constructor: ToonConverter(encryptor: Encryptor = None)
All conversion methods accept an optional conversion_mode argument:
-
conversion_mode:"no_encryption"(default),"middleware","ingestion","export". -
from_json(data, conversion_mode=...): Converts dict/list/string to TOON. -
to_json(toon_string, return_json=True, conversion_mode=...): Converts TOON to Python/JSON. -
from_xml,from_csv,from_yaml,to_xml,to_csv,to_yaml: Equivalent methods. -
validate(toon_string): Static Method Only. Validates TOON syntax. Does not support encryption.
AsyncToonConverter (Asynchronous)
Constructor: AsyncToonConverter(encryptor: Encryptor = None)
- Mirrors all
ToonConvertermethods asasyncfunctions (e.g.,await conv.from_json(...)). - Supports the same
conversion_modeparameters for encryption pipelines. validate(toon_string): Static Method Only. Async validation. No encryption support.
Encryption
Encryptor
Constructor: Encryptor(key=None, algorithm='fernet')
algorithm:'fernet'(default),'xor','base64'.key: Required for Fernet/XOR.encrypt(data),decrypt(data): Helper methods.
Utility Functions
from toon_parse import extract_json_from_string, extract_xml_from_string, extract_csv_from_string
# Direct access to extraction logic without conversion
📄 License
MIT License
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_parse-2.0.0.tar.gz.
File metadata
- Download URL: toon_parse-2.0.0.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03c8b2681369d2b16926d167a762b9a6f868597b8808aa2221ac7b8e174c6ee7
|
|
| MD5 |
5e045d3fdaef6f682256dbaf03bb2493
|
|
| BLAKE2b-256 |
1761660cd6b3efd3e8572f7be1c2a12a5bb9aba3ffe566d42e09cd23f7f11432
|
File details
Details for the file toon_parse-2.0.0-py3-none-any.whl.
File metadata
- Download URL: toon_parse-2.0.0-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ea04ce13139e07f0cdf7b8c4f8ce8de30063e707d7c0c002b90359824d6cc64
|
|
| MD5 |
51ae9991ee5e9b7c1d0c6dbbf3504782
|
|
| BLAKE2b-256 |
6a8e1d7a6eb382f5c6552409eb6b715e7c5317389aadb94e423e43d49c876ff8
|