RTOON - Ruby Token Object Oriented Notation Parser
A Ruby gem for parsing and encoding Token Object Oriented Notation (TOON) — a human-readable data format combining tabular structure with JSON-like flexibility.'
Inspired by toon-format/toon which is written in TypeScript and for TypeScript projects.
What is TOON?
TOON is a data serialization format with schema declarations, tabular rows, and indentation-based nesting.
From the original project description:
Token-Oriented Object Notation is a compact, human-readable serialization format designed for passing structured data to Large Language Models with significantly reduced token usage. It's intended for LLM input as a lossless, drop-in representation of JSON data.
TOON's sweet spot is uniform arrays of objects – multiple fields per row, same structure across items. It borrows YAML's indentation-based structure for nested objects and CSV's tabular format for uniform data rows, then optimizes both for token efficiency in LLM contexts. For deeply nested or non-uniform data, JSON may be more efficient.
TOON achieves CSV-like compactness while adding explicit structure that helps LLMs parse and validate data reliably.
Example:
users[2]{id,name}:
1,Ada
2,Bob
Parses to:
{"users" => [{"id" => "1", "name" => "Ada"}, {"id" => "2", "name" => "Bob"}]}Installation
gem install rtoonOr add to Gemfile:
gem 'rtoon'Quick Start
require 'rtoon'
# Parse TOON → Ruby
result = Rtoon.parse("users[1]{id,name}:\n 1,Ada\n")
# => {"users" => [{"id" => "1", "name" => "Ada"}]}
# Encode Ruby → TOON
data = {"users" => [{"id" => "1", "name" => "Ada"}]}
toon = Rtoon.encode(data)
# => "users[1]{id,name}:\n 1,Ada\n"Syntax
Schema Declaration:
identifier[size]{field1,field2}:
value1,value2
value3,value4
Field Assignment:
name: John
age: 30
Nested Structure:
company[1]{depts,location}:
depts[2]{name,size}:
engineering,50
sales,20
location: NYC
API
Rtoon.parse(string) / Rtoon.decode(string)
Parses TOON-formatted string to Ruby Hash/Array.
Rtoon.encode(hash, indent_level = 0)
Converts Ruby data structure to TOON format.
Error Handling
begin
result = Rtoon.parse(invalid_toon)
rescue Rtoon::Parser::ParseError => e
puts "Parse error: #{e.message}"
endDevelopment
# Clone and setup
git clone https://github.com/zoobean/rtoon.git
cd rtoon
bundle install
# Run tests
ruby test/toon_test.rb
# Compile grammar (if modified)
racc -o lib/rtoon/parser.tab.rb lib/rtoon/parser.yLimitations
- Values returned as strings (no automatic type conversion)
- No string escaping (commas in values not supported)
- Fixed 2-space indentation
- No comment syntax
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new features
- Submit a Pull Request
See ARCHITECTURE.md for implementation details.
License
MIT License - See LICENSE.txt
Author
Antonio Chavez - Zoobean
Report bugs: GitHub Issues