Skip to main content

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 (New!)

The library can now 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.

⚡ Async Usage

For non-blocking operations in async applications (e.g., FastAPI, AIOHTTP), use AsyncToonConverter.

import asyncio
from toon_parse import AsyncToonConverter

async def main():
    # Mixed text input
    text = 'Data: <user><name>Alice</name></user>'
    
    # Async conversion (XML -> TOON)
    toon_string = await AsyncToonConverter.from_xml(text)
    print(toon_string)
    # Output: Data: user:\n  name: "Alice"
    
    # Async parsing (TOON -> JSON String)
    # Set return_json=False to get a JSON dict instead of a string
    json_dict = await AsyncToonConverter.to_json(toon_string, return_json=False)
    print(json_dict)

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
  • Mixed Text: Supported for JSON, XML, and CSV. The converter will find all occurrences of the data format in the text and convert them to TOON, leaving surrounding text intact.
  • YAML & TOON: Input must be a valid, complete string. Mixed text is not supported for these formats.

🛠 API Reference

Core Converters

ToonConverter (Synchronous)

  • from_json(data): Converts dict, list, JSON string, or mixed text to TOON.
  • to_json(toon_string, return_json=False): Converts TOON to Python dict/list. Set return_json=True(default) to get a JSON string.
  • from_xml(xml_string): Converts XML string or mixed text to TOON.
  • from_csv(csv_string): Converts CSV string or mixed text to TOON.
  • from_yaml(yaml_string): Converts YAML string to TOON (no mixed text).
  • to_xml(toon_string): Converts TOON to XML.
  • to_csv(toon_string): Converts TOON to CSV.
  • to_yaml(toon_string): Converts TOON to YAML.
  • validate(toon_string): Validates TOON syntax. Returns {'is_valid': bool, 'error': str}.

AsyncToonConverter (Asynchronous)

  • Mirrors all methods of ToonConverter but as async functions.
  • Example: await AsyncToonConverter.from_json(data)

Utility Functions

You can also import extraction functions directly if you need to extract data without converting it.

from toon_parse import extract_json_from_string, extract_xml_from_string, extract_csv_from_string

text = "Some text {"key": "val"} more text"
json_str = extract_json_from_string(text)
# Returns: '{"key": "val"}'

📄 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toon_parse-1.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toon_parse-1.1.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file toon_parse-1.1.0.tar.gz.

File metadata

  • Download URL: toon_parse-1.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for toon_parse-1.1.0.tar.gz
Algorithm Hash digest
SHA256 130b1d6e5e9c2efa56eae3692bec18e0b45b0f1ecf46dbe2a8b5b366f614772c
MD5 2a27123cc990f6b0bdb03030a9d41403
BLAKE2b-256 af274024077f2b85486d0971ddf083cce0eed33413d5bbf1fe3c6d8afdceb71d

See more details on using hashes here.

File details

Details for the file toon_parse-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: toon_parse-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for toon_parse-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cacaf09a4b6efa9de5678be1cc03b9107d2f47fdd2a3a4c5e7674d2b83c8945d
MD5 9030338c0f570fbac739fa9a7dd074ef
BLAKE2b-256 a084d099e4054622b09f34699e4b07d9f5cdba2ff6a283b4b8211789d34cd146

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page