json2toon_rs
Fast, optimized JSON to TOON format converter based on the TOON v2.0 specification.
What is TOON?
TOON (Token-Oriented Object Notation) is a line-oriented, indentation-based text format that encodes JSON data with explicit structure and minimal quoting. It's particularly efficient for:
- Arrays of uniform objects (tabular data)
- Deterministic, human-readable representations
- LLM prompts and structured data interchange
- Configuration files with nested structures
Features
- โ Fully spec-compliant with TOON v2.0
- โ๏ธ Bidirectional conversion - encode JSON to TOON and decode back
- ๐ Optimized for performance with minimal allocations
- ๐ฆ Zero unsafe code - fully safe Rust
- โจ Robust Error Handling - provides detailed, structured errors for easier debugging.
- ๐ฏ Automatic format detection - tabular vs expanded arrays
- ๐ง Configurable delimiters (comma, tab, pipe)
- โ๏ธ Strict mode - optional validation of structure and counts
- ๐งช Comprehensive tests covering all spec requirements
- ๐ Well-documented with inline comments
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Encoding (JSON โ TOON)
use ;
use json;
Output:
users[2]{id,name,active}:
1,Alice,true
2,Bob,false
Decoding (TOON โ JSON)
use ;
Round-Trip
use ;
use json;
Examples
Simple Object
let data = json!;
TOON output:
name: Alice
age: 30
active: true
Nested Objects
let data = json!;
TOON output:
user:
id: 123
name: Bob
Primitive Arrays
let data = json!;
TOON output:
tags[3]: admin,user,dev
Tabular Arrays
Arrays of uniform objects with primitive values are automatically formatted as tables:
let data = json!;
TOON output:
items[2]{sku,qty,price}:
A1,2,9.99
B2,1,14.5
Mixed Arrays
Arrays with non-uniform content use expanded list format:
let data = json!;
TOON output:
items[3]:
- 42
- text
- key: value
Custom Delimiters
use ;
let options = EncoderOptions ;
let data = json!;
let toon = encode;
TOON output (with tabs):
items[2 ]{id name}:
1 A
2 B
Configuration Options
Encoder Options
Decoder Options
In strict mode, the decoder will:
- Enforce exact indentation multiples
- Validate array/row counts match declared lengths
- Reject invalid escape sequences
- Check delimiter consistency
Spec Compliance
This implementation follows the TOON v2.0 specification:
- โ Canonical number formatting (no exponents, no trailing zeros)
- โ Deterministic quoting rules
- โ
Escape sequences:
\\,\",\n,\r,\t - โ Tabular array detection
- โ Delimiter-aware quoting
- โ Object key preservation order
- โ UTF-8 support with Unicode and emoji
- โ Empty object/array handling
- โ Nested structure support
Testing
Run the test suite:
Run examples:
# Encoding examples
# Decoding examples
# Round-trip examples (encode + decode)
Decoder Features
The decoder implements the complete TOON v2.0 specification:
- Line-based parsing with depth tracking
- Array header parsing with delimiter detection (
[N],[N ],[N|]) - Tabular format decoding with field mapping
- Expanded list format for mixed arrays
- Object nesting with proper depth handling
- String unescaping with only valid escapes (
\\,\",\n,\r,\t) - Type inference (strings, numbers, booleans, null)
- Quoted string handling with escape sequence validation
- Strict mode validation (optional)
- Indentation must be exact multiples
- Array/row counts must match declared lengths
- Invalid escapes are rejected
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Based on the TOON v2.0 specification
- Built with serde and serde_json