- Notifications
You must be signed in to change notification settings - Fork31
TONL (Token-Optimized Notation Language)
License
tonl-dev/tonl
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
TONL is a production-ready data platform that combines compact serialization with powerful query, modification, indexing, and streaming capabilities. Designed for LLM token efficiency while providing a rich API for data access and manipulation.
- 216 new tests - Total 698 tests across 162 suites with 100% pass rate
- Browser documentation - Complete docs/BROWSER.md and docs/ERROR_HANDLING.md
- 4 browser examples - React 18 and Vue 3 interactive demos
- 5 security fixes - All npm vulnerabilities resolved
- Updated dependencies - All packages at latest versions
- 8 critical bug fixes including DoS prevention and async handling
- Enterprise security hardening and optimization module
- 698 Comprehensive Tests - All passing with 100% success rate
- 96 Security Tests - Covering all attack vectors
- Concurrency Tests - Thread safety validation
- Browser Tests - Cross-platform compatibility
🏠 Homepage:tonl.dev📦 GitHub:github.com/tonl-dev/tonl📖 Documentation:Complete Guides
- Why TONL?
- Quick Start
- Format Overview
- Feature Set
- Performance
- Security
- Use Cases
- Browser Usage
- API Reference
- Development
- Roadmap
- Documentation
- Contributing
- License
🗜️Up to 60% Smaller - Reduce JSON size and LLM token costs👁️Human-Readable - Clear text format, not binary🚀Blazingly Fast - 10-1600x faster than targets🔒Production Secure - 100% security hardened (v2.0.3)🛠️TypeScript-First - Full type safety & IntelliSense📦Zero Dependencies - Pure TypeScript, no bloat🌐Browser Ready - 10.5 KB gzipped bundle (IIFE/UMD)✅100% Tested - 496/496 tests passing (core functionality)
npm install tonl
import{TONLDocument,encodeTONL,decodeTONL}from'tonl';// Create from JSONconstdoc=TONLDocument.fromJSON({users:[{id:1,name:"Alice",role:"admin",age:30},{id:2,name:"Bob",role:"user",age:25}]});// Query with JSONPath-like syntaxdoc.get('users[0].name');// 'Alice'doc.query('users[*].name');// ['Alice', 'Bob']doc.query('users[?(@.role == "admin")]');// [{ id: 1, ... }]doc.query('$..age');// All ages recursively// Aggregation (v2.4.0)doc.count('users[*]');// 2doc.sum('users[*]','age');// 55doc.avg('users[*]','age');// 27.5doc.groupBy('users[*]','role');// { admin: [...], user: [...] }doc.aggregate('users[*]').stats('age');// { count, sum, avg, min, max, stdDev }// Fuzzy Matching (v2.4.0)import{fuzzySearch,soundsLike}from'tonl/query';fuzzySearch('Jon',['John','Jane','Bob']);// [{ value: 'John', score: 0.75 }]soundsLike('Smith','Smyth');// true// Temporal Queries (v2.4.0)import{parseTemporalLiteral,isDaysAgo}from'tonl/query';parseTemporalLiteral('@now-7d');// 7 days agoisDaysAgo(someDate,30);// within last 30 days?// Modify datadoc.set('users[0].age',31);doc.push('users',{id:3,name:"Carol",role:"editor",age:28});// Navigate and iteratefor(const[key,value]ofdoc.entries()){console.log(key,value);}doc.walk((path,value,depth)=>{console.log(`${path}:${value}`);});// Exportconsttonl=doc.toTONL();constjson=doc.toJSON();awaitdoc.save('output.tonl');// Classic API (encode/decode)constdata={users:[{id:1,name:"Alice"}]};consttonlText=encodeTONL(data);constrestored=decodeTONL(tonlText);// Advanced Optimization (v2.0.1+)import{AdaptiveOptimizer,BitPacker,DeltaEncoder}from'tonl/optimization';// Automatic optimizationconstoptimizer=newAdaptiveOptimizer();constresult=optimizer.optimize(data);// Auto-selects best strategies// Specific optimizersconstpacker=newBitPacker();constpacked=packer.packBooleans([true,false,true]);constdelta=newDeltaEncoder();consttimestamps=[1704067200000,1704067201000,1704067202000];constcompressed=delta.encode(timestamps,'timestamp');
# Interactive stats dashboardtonl stats data.json --interactivetonl stats data.json -i --theme neon# File comparison modetonl stats data.json --compare --theme matrix# Interactive explorationtonl stats --interactive# Launch without file for menu-driven exploration
# Get started (shows help)tonl# Version infotonl --version# Encode JSON to TONL (perfect round-trip, quotes special keys)tonl encode data.json --out data.tonl --smart --stats# Encode with preprocessing (clean, readable keys)tonl encode data.json --preprocess --out data.tonl# Decode TONL to JSONtonl decode data.tonl --out data.json# Query datatonl query users.tonl"users[?(@.role == 'admin')]"tonl get data.json"user.profile.email"# Validate against schematonl validate users.tonl --schema users.schema.tonl# Format and prettifytonl format data.tonl --pretty --out formatted.tonl# Compare token coststonl stats data.json --tokenizer gpt-5
# Available themes: default, neon, matrix, cyberpunktonl stats data.json -i --theme neon# Bright neon colorstonl stats data.json -i --theme matrix# Green matrix styletonl stats data.json -i --theme cyberpunk# Cyan/purple cyberpunktonl stats data.json -i --theme default# Clean terminal colors
# Compare JSON and TONL files side-by-sidetonl stats data.json --comparetonl stats data.json --compare --theme neon# Interactive comparison modetonl stats data.json -i --compare
JSON (245 bytes, 89 tokens):
{"users": [ {"id":1,"name":"Alice","role":"admin" }, {"id":2,"name":"Bob, Jr.","role":"user" }, {"id":3,"name":"Carol","role":"editor" } ]}TONL (158 bytes, 49 tokens -45% reduction):
#version 1.0users[3]{id:u32,name:str,role:str}: 1, Alice, admin 2, "Bob, Jr.", user 3, Carol, editorJSON:
{"user": {"id":1,"name":"Alice","contact": {"email":"alice@example.com","phone":"+123456789" },"roles": ["admin","editor"] }}TONL:
#version 1.0user{id:u32,name:str,contact:obj,roles:list}: id: 1 name: Alice contact{email:str,phone:str}: email: alice@example.com phone: +123456789 roles[2]: admin, editor- Compact Format - 32-45% smaller than JSON (bytes + tokens)
- Human-Readable - Clear text format with minimal syntax
- Round-Trip Safe - Perfect bidirectional JSON conversion
- Smart Encoding - Auto-selects optimal delimiters and formatting
- Type Hints - Optional schema information for validation
- JSONPath Queries -
users[?(@.age > 25)],$..email - Filter Expressions -
==,!=,>,<,&&,||,contains,matches - Wildcard Support -
users[*].name,**.email - Tree Traversal -
entries(),keys(),values(),walk() - LRU Cache - >90% cache hit rate on repeated queries
- CRUD Operations -
set(),get(),delete(),push(),pop() - Bulk Operations -
merge(),update(),removeAll() - Change Tracking -
diff()with detailed change reports - Snapshots - Document versioning and comparison
- Atomic File Edits - Safe saves with automatic backups
- Hash Index - O(1) exact match lookups
- BTree Index - O(log n) range queries
- Compound Index - Multi-field indexing
- Stream Processing - Handle multi-GB files with <100MB memory
- Pipeline Operations - Chainable filter/map/reduce transformations
- Dictionary Encoding - Value compression via lookup tables (30-50% savings)
- Delta Encoding - Sequential data compression (40-60% savings)
- Run-Length Encoding - Repetitive value compression (50-80% savings)
- Bit Packing - Boolean and small integer bit-level compression (87.5% savings)
- Numeric Quantization - Precision reduction for floating-point numbers (20-40% savings)
- Schema Inheritance - Reusable column schemas across data blocks (20-40% savings)
- Hierarchical Grouping - Common field extraction for nested structures (15-30% savings)
- Tokenizer-Aware - LLM tokenizer optimization for minimal token usage (5-15% savings)
- Column Reordering - Entropy-based ordering for better compression
- Adaptive Optimizer - Automatic strategy selection based on data patterns
- Schema Definition -
.schema.tonlfiles with TSL (TONL Schema Language) - 13 Constraints -
required,min,max,pattern,unique,email, etc. - TypeScript Generation - Auto-generate types from schemas
- Runtime Validation - Validate data programmatically or via CLI
- Strict Mode - Enforce schema compliance
- 🎮 Interactive CLI Dashboard - Real-time file analysis with themes and progress visualization
- ⚖️ File Comparison System - Side-by-side JSON/TONL comparison with detailed metrics
- 🎨 Visual Customization - Multiple terminal themes (default, neon, matrix, cyberpunk)
- Interactive REPL - Explore data interactively in terminal
- Modular CLI Suite -
encode,decode,query,validate,format,statswith Command Pattern architecture - Browser Support - ESM, UMD, IIFE builds (8.84 KB gzipped)
- VS Code Extension - Syntax highlighting for
.tonlfiles - TypeScript-First - Full IntelliSense and type safety
| Metric | JSON | TONL | TONL Smart | Improvement |
|---|---|---|---|---|
| Size (bytes) | 245 | 167 | 158 | 36% smaller |
| Tokens (GPT-5) | 89 | 54 | 49 | 45% fewer |
| Encoding Speed | 1.0x | 15x | 12x | 12-15x faster |
| Decoding Speed | 1.0x | 10x | 10x | 10x faster |
| Query Speed | - | - | 1600x | Target: <1ms |
Benchmarks based on typical e-commerce product catalog data
✅ Tests: 698+ tests passing (100% coverage)✅ Security: All vulnerabilities fixed (100%)✅ Security Tests: 96 security tests passing✅ Code Quality: TypeScript strict mode✅ Dependencies: 0 runtime dependencies✅ Bundle Size: 10.5 KB gzipped (browser)✅ Performance: 10-1600x faster than targets✅ Production: Ready & Fully SecureSecurity:
- ✅ ReDoS, Path Traversal, Buffer Overflow protection
- ✅ Prototype Pollution, Command Injection prevention
- ✅ Integer Overflow, Type Coercion fixes
- ✅ Comprehensive input validation and resource limits
SeeSECURITY.md andCHANGELOG.md for details.
Reduce token costs by 32-45% when including structured data in prompts:
constprompt=`Analyze this user data:\n${doc.toTONL()}`;// 45% fewer tokens = lower API costs
Human-readable configs that are compact yet clear:
config{env:str,database:obj,features:list}: env: production database{host:str,port:u32,ssl:bool}: host: db.example.com port: 5432 ssl: true features[3]: auth, analytics, cachingEfficient data transmission with schema validation:
app.get('/api/users',async(req,res)=>{constdoc=awaitTONLDocument.load('users.tonl');constfiltered=doc.query('users[?(@.active == true)]');res.type('text/tonl').send(encodeTONL(filtered));});
Stream processing for large datasets:
import{createEncodeStream,createDecodeStream}from'tonl/stream';createReadStream('huge.json').pipe(createDecodeStream()).pipe(transformStream).pipe(createEncodeStream({smart:true})).pipe(createWriteStream('output.tonl'));
Compact structured logs:
logs[1000]{timestamp:i64,level:str,message:str,metadata:obj}: 1699564800, INFO, "User login", {user_id:123,ip:"192.168.1.1"} 1699564801, ERROR, "DB timeout", {query:"SELECT...",duration:5000} ...<scripttype="module">import{encodeTONL,decodeTONL}from'https://cdn.jsdelivr.net/npm/tonl@2.4.1/+esm';constdata={users:[{id:1,name:"Alice"}]};consttonl=encodeTONL(data);console.log(tonl);</script>
<scriptsrc="https://unpkg.com/tonl@2.4.1/dist/browser/tonl.umd.js"></script><script>consttonl=TONL.encodeTONL({hello:"world"});console.log(tonl);</script>
Bundle Sizes:
- ESM: 15.5 KB gzipped
- UMD: 10.7 KB gzipped
- IIFE: 10.6 KB gzipped
Examples:Seeexamples/browser/ for interactive React and Vue examples.
// CreationTONLDocument.fromJSON(data)TONLDocument.parse(text)// Parse TONL stringTONLDocument.fromFile(filepath)// Async file loadTONLDocument.fromFileSync(filepath)// Sync file load// Querydoc.get(path:string)// Single valuedoc.query(query:string)// Multiple valuesdoc.exists(path:string)// Check existence// Modificationdoc.set(path:string,value: any)// Set valuedoc.delete(path:string)// Delete valuedoc.push(path:string,value: any)// Append to arraydoc.pop(path:string)// Remove last from arraydoc.merge(path:string,value:object)// Deep merge objects// Navigationdoc.entries()// Iterator<[key, value]>doc.keys()// Iterator<string>doc.values()// Iterator<any>doc.walk(callback:WalkCallback)// Tree traversaldoc.find(predicate:Predicate)// Find single valuedoc.findAll(predicate:Predicate)// Find all matchingdoc.some(predicate:Predicate)// Any matchdoc.every(predicate:Predicate)// All match// Indexingdoc.createIndex(name:string,path:string,type?)// Create indexdoc.dropIndex(name:string)// Remove indexdoc.getIndex(name:string)// Get index// Exportdoc.toTONL(options?:EncodeOptions)// Export as TONLdoc.toJSON()// Export as JSONdoc.save(filepath: string,options?)// Save to filedoc.size()// Size in bytesdoc.stats()// Statistics object
// EncodingencodeTONL(data: any,options?:{delimiter?:","|"|"|"\t"|";"; includeTypes?:boolean; version?:string; indent?:number; singleLinePrimitiveLists?:boolean;}):string// Smart encoding (auto-optimized)encodeSmart(data: any,options?:EncodeOptions):string// DecodingdecodeTONL(text:string,options?:{delimiter?:","|"|"|"\t"|";"; strict?:boolean;}):any
import{parseSchema,validateTONL}from'tonl/schema';// Parse schemaconstschema=parseSchema(schemaText: string);// Validate dataconstresult=validateTONL(data:any,schema:Schema);if(!result.valid){result.errors.forEach(err=>{console.error(`${err.field}:${err.message}`);});}
import{createEncodeStream,createDecodeStream,encodeIterator,decodeIterator}from'tonl/stream';// Node.js streamscreateReadStream('input.json').pipe(createEncodeStream({smart:true})).pipe(createWriteStream('output.tonl'));// Async iteratorsforawait(constlineofencodeIterator(dataStream)){console.log(line);}
Define schemas with the TONL Schema Language (TSL):
@schema v1@strict true@description "User management schema"# Define custom typesUser: obj id: u32 required username: str required min:3 max:20 pattern:^[a-zA-Z0-9_]+$ email: str required pattern:email lowercase:true age: u32? min:13 max:150 roles: list<str> required min:1 unique:true# Root schemausers: list<User> required min:1totalCount: u32 required13 Built-in Constraints:
required- Field must existmin/max- Numeric range or string/array lengthlength- Exact lengthpattern- Regex validation (or shortcuts:email,url,uuid)unique- Array elements must be uniquenonempty- String/array cannot be emptypositive/negative- Number signinteger- Must be integermultipleOf- Divisibility checklowercase/uppercase- String case enforcement
Seedocs/SCHEMA_SPECIFICATION.md for complete reference.
# Install dependenciesnpm install# Build TypeScriptnpm run build# Run all tests (698+ tests)npmtest# Watch modenpm run dev# Clean build artifactsnpm run clean
# Byte size comparisonnpm run bench# Token estimation (GPT-5, Claude 3.5, Gemini 2.0, Llama 4)npm run bench-tokens# Comprehensive performance analysisnpm run bench-comprehensive
# Install CLI locallynpm run link# Test commandstonl encode test.jsontonl query data.tonl"users[*].name"tonl format data.tonl --pretty# Test interactive features (v2.3.1+)tonl stats data.json --interactivetonl stats data.json -i --theme neontonl stats data.json --compare
✅ v2.5.1 - Complete (Latest)
- ✅ Critical bug fixes (Array expansion DoS, JSON.stringify vulnerability, async handling)
- ✅ 482 tests with 100% pass rate
- ✅ Enhanced stability and error handling
✅ v2.5.0 - Complete
- ✅ Aggregation Functions (count, sum, avg, groupBy, stats, median, percentile)
- ✅ Fuzzy String Matching (Levenshtein, Jaro-Winkler, Soundex, Metaphone)
- ✅ Temporal Queries (@now-7d, before, after, sameDay, daysAgo)
- ✅ 763+ comprehensive tests with 100% success rate
✅ v2.2+ - Complete
- ✅ Revolutionary Interactive CLI Dashboard with real-time analysis
- ✅ Complete Modular Architecture Transformation (735→75 lines)
- ✅ File Comparison System with side-by-side analysis
- ✅ Visual Themes (default, neon, matrix, cyberpunk)
✅ v2.0+ - Complete
- ✅ Advanced optimization module (60% additional compression)
- ✅ Complete query, modification, indexing, streaming APIs
- ✅ Schema validation & TypeScript generation
- ✅ Browser support (10.5 KB bundles)
- ✅ 100% test coverage & security hardening
🚀 Future
- Enhanced VS Code extension (IntelliSense, debugging)
- Web playground with live conversion
- Python, Go, Rust implementations
- Binary TONL format for extreme compression
SeeROADMAP.md for our comprehensive development vision.
- Getting Started Guide - Beginner-friendly tutorial with examples
- API Reference - Complete API documentation with examples
- CLI Documentation - Command-line tool guide
- Browser API - Browser usage with ESM, UMD, IIFE builds
- Error Handling - Error classes and troubleshooting
- Query API - JSONPath-like query syntax reference
- Modification API - CRUD operations guide
- Navigation API - Tree traversal methods
- Use Cases - Real-world scenarios and solutions
- Implementation Reference - Language-agnostic implementation guide
- Transformation Examples - 20+ JSON↔TONL conversion examples
- Format Specification - Technical format specification
- Schema Specification - TSL (TONL Schema Language) spec
Implementing TONL in Python, Go, Rust, or another language? Check out theImplementation Reference for complete algorithms, pseudo-code, and test requirements!
Contributions are welcome! Please readCONTRIBUTING.md for:
- Development setup
- Code style guidelines
- Testing requirements
- Pull request process
- Architecture overview
MIT License - seeLICENSE file for details.
- Website:tonl.dev
- npm Package:npmjs.com/package/tonl
- GitHub:github.com/tonl-dev/tonl
- Issues:github.com/tonl-dev/tonl/issues
- Discussions:github.com/tonl-dev/tonl/discussions
- VS Code Extension: [Coming Soon]
TONL: Making structured data LLM-friendly without sacrificing readability. 🚀
Built with ❤️ byErsin Koc
About
TONL (Token-Optimized Notation Language)
Topics
Resources
License
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.
