Skip to main content

Token-efficient Structured Object Notation for LLMs

Project description

TSON for Python

Token-efficient Structured Object Notation - Python Implementation

License: MIT Python 3.7+ Tests Passing

Installation

cd python
pip install -e .

Or install from PyPI:

pip install tson

Quick Start

import tson

# Simple object
data = {"name": "Alice", "age": 30, "active": True}
encoded = tson.dumps(data)
print(encoded)
# Output: {@name,age,active|Alice,30,true}

# Perfect round-trip
decoded = tson.loads(encoded)
assert data == decoded  # True

API Reference

Serialization

import tson

# Serialize Python data to TSON string
tson_string = tson.dumps(data)

# Serialize to file
with open('data.tson', 'w') as f:
    tson.dump(data, f)

Deserialization

# Deserialize TSON string to Python data
data = tson.loads(tson_string)

# Deserialize from file
with open('data.tson', 'r') as f:
    data = tson.load(f)

Examples

1. Simple Objects

data = {"name": "Alice", "age": 30, "active": True}
encoded = tson.dumps(data)
# {@name,age,active|Alice,30,true}

2. Arrays

data = [1, 2, 3, 4, 5]
encoded = tson.dumps(data)
# [1,2,3,4,5]

3. Array of Objects (Tabular Format)

data = [
    {"id": 1, "name": "Alice", "email": "alice@example.com"},
    {"id": 2, "name": "Bob", "email": "bob@example.com"},
    {"id": 3, "name": "Carol", "email": "carol@example.com"}
]
encoded = tson.dumps(data)
# {@id,name,email#3|1,Alice,alice@example.com|2,Bob,bob@example.com|3,Carol,carol@example.com}

decoded = tson.loads(encoded)
assert data == decoded  # Perfect round-trip

4. Nested Schema Notation

data = [
    {"id": 1, "name": "Alice", "address": {"city": "NYC", "zip": "10001"}},
    {"id": 2, "name": "Bob", "address": {"city": "LA", "zip": "90001"}}
]
encoded = tson.dumps(data)
# {@id,name,address(@city,zip)#2|1,Alice,{NYC,"10001"}|2,Bob,{LA,"90001"}}

5. Complex Nested Structures

data = {
    "company": "Acme Corp",
    "employees": [
        {
            "id": 1,
            "name": "Alice",
            "skills": ["Python", "Go"],
            "contact": {"email": "alice@acme.com", "phone": "555-0101"}
        },
        {
            "id": 2,
            "name": "Bob",
            "skills": ["Java"],
            "contact": {"email": "bob@acme.com", "phone": "555-0102"}
        }
    ]
}

encoded = tson.dumps(data)
decoded = tson.loads(encoded)
assert data == decoded  # Perfect round-trip

6. Type Preservation

data = {
    "zip_string": "10001",  # String
    "zip_number": 10001,    # Number
    "version_string": "1.0", # String
    "version_number": 1.0   # Float
}

encoded = tson.dumps(data)
# {@zip_string,zip_number,version_string,version_number|"10001",10001,"1.0",1.0}

decoded = tson.loads(encoded)
assert isinstance(decoded["zip_string"], str)   # True
assert isinstance(decoded["zip_number"], int)   # True
assert isinstance(decoded["version_string"], str)  # True
assert isinstance(decoded["version_number"], float)  # True

7. Empty Values

data = {
    "empty_string": "",
    "empty_array": [],
    "empty_object": {},
    "null_value": None
}

encoded = tson.dumps(data)
# {@empty_string,empty_array,empty_object,null_value|"",[],{@},null}

8. Special Characters

data = {
    "comma": "hello, world",
    "pipe": "a|b|c",
    "quotes": 'She said "hello"',
    "newline": "line1\nline2"
}

encoded = tson.dumps(data)
decoded = tson.loads(encoded)
assert data == decoded  # Special characters preserved

Testing

Run the comprehensive test suite (13 tests):

python tests/test_round\ trip.py

All tests should pass:

[PASS] Simple object
[PASS] Simple array
[PASS] Array of objects (tabular)
[PASS] Nested object
[PASS] Mixed array
[PASS] Empty values
[PASS] Special characters
[PASS] Numeric strings
[PASS] Nested arrays
[PASS] Array with nested objects (nested schema)
[PASS] Complex structure
[PASS] Boolean values
[PASS] Numeric types

SUCCESS: All 13 tests passed!

Interactive Testing

Test TSON strings interactively:

python test_tson_string.py

This tool lets you:

  • Enter TSON strings and see the JSON conversion
  • Verify round-trip conversion
  • See token savings
  • Run example tests

LLM Integration

See llm_integration_example.py for complete examples of using TSON with LLMs.

Quick Example

import tson

# Prepare data for LLM
data = [
    {"date": "2025-01-01", "sales": 5000, "region": "North"},
    {"date": "2025-01-02", "sales": 6000, "region": "South"},
    {"date": "2025-01-03", "sales": 5500, "region": "East"},
]

tson_data = tson.dumps(data)

# Include TSON system prompt (see prompts.md)
system_prompt = """
TSON format (compact JSON):
• {@k1,k2|v1,v2} = object
• {@k1,k2#N|v1,v2|v1,v2} = array of objects
"""

user_prompt = f"Analyze this sales data: {tson_data}"

# Send to LLM API...
# Token savings: 30-50% compared to JSON

See ../prompts.md for complete LLM prompt templates.

Examples

Run the basic usage examples:

python examples/basic_usage.py

This demonstrates:

  • Simple objects
  • Array of objects (tabular format)
  • Nested schema notation
  • Mixed types
  • Real-world data structures
  • Special values

Type Support

TSON supports all JSON types:

Python Type TSON Representation Example
str String (quoted if needed) Alice or "Hello, World"
int Number 42, -17
float Number 3.14, -2.5
bool Boolean true, false
None Null null
list Array [1,2,3]
dict Object {@key|value}

Performance

TSON is optimized for:

  • Token efficiency - 30-55% savings vs JSON
  • Fast parsing - Simple delimiter-based parsing
  • Low memory - Minimal overhead
  • No dependencies - Pure Python implementation

Syntax Guide

See the main SPEC.md for complete syntax specification.

Quick reference:

Delimiter Purpose Example
{ } Object boundaries {@name|Alice}
[ ] Array boundaries [1,2,3]
@ Object marker {@key1,key2|...}
, Field/value separator name,age,city
| Row separator val1,val2|val1,val2
# Row count (optional) #3

Requirements

  • Python 3.7 or higher
  • No external dependencies

Development

Install in editable mode for development:

pip install -e .

Run tests:

python tests/test_round\ trip.py

Contributing

Contributions to the Python implementation are welcome! Please see ../CONTRIBUTING.md for guidelines.

Documentation

License

MIT License - see ../LICENSE file for details.


Version: 1.0.0 Status: Production Ready Python: 3.7+ Dependencies: None

Part of the TSON project by Zeno AI

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

tson-1.0.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

tson-1.0.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file tson-1.0.0.tar.gz.

File metadata

  • Download URL: tson-1.0.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for tson-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8a5c4bb0efecd9965de075d0a2c54c17ec74e28f19cf3e492a13f0845a8c3bfa
MD5 bf74f0effcfe898a7361f5aabfb98267
BLAKE2b-256 afb398ea248911e4210555b7f703a0a4b1a98ec705b494614ff15abb8800eb25

See more details on using hashes here.

File details

Details for the file tson-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tson-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for tson-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c181685763711849e60bbc8aa82c798df6a36b8c05460d0e535c5d30488392ff
MD5 8711a2eb5677ada51c6bff73ad19f967
BLAKE2b-256 5f1c0687becd296c69231fa3e1edb5e8b1ca3290ee613e46cc65bfac1283cc96

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