toon_plus 0.0.4
toon_plus: ^0.0.4 copied to clipboard
A compact, human-readable data serialization format that reduces token usage by 30-50% compared to JSON while maintaining readability. Perfect for LLM applications and API optimization.
TOON Plus #
A compact, human-readable data serialization format that reduces token usage by 30-50% compared to JSON. Perfect for LLM applications, API optimization, and anywhere you need efficient data encoding.
π― Key Features #
- π Compact: 30-50% fewer tokens than JSON
- π Human Readable: Easy to read, write, and debug
- π Bidirectional: Full encode/decode support with type preservation
- β‘ Pure Dart: Zero dependencies, works everywhere
- π― Type Safe: Preserves primitives, objects, and arrays
- π οΈ Customizable: Configure indentation and delimiters
- π― Well Tested: Comprehensive test coverage
- π Well Documented: Extensive examples and API documentation
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
toon_plus: ^0.0.4
Then run:
dart pub get
π Quick Start #
Encoding #
import 'package:toon_plus/toon_plus.dart';
void main() {
final data = {
'user': 'Alice',
'age': 30,
'active': true,
'scores': [95, 87, 92],
};
final toon = encode(data);
print(toon);
}
Output:
user: Alice
age: 30
active: true
scores[3]: 95,87,92
Decoding #
import 'package:toon_plus/toon_plus.dart';
void main() {
final toon = '''
user: Alice
age: 30
active: true
scores[3]: 95,87,92
''';
final data = decode(toon);
print(data);
// {user: Alice, age: 30, active: true, scores: [95, 87, 92]}
}
π Complete Examples #
Complex Nested Structures #
final data = {
'project': 'TOON Format',
'version': '0.0.4',
'author': {
'name': 'Vardon Bainbridge',
'email': 'vardon.bainbridge@gmail.com',
},
'features': ['encoding', 'decoding', 'compression'],
'stats': {
'stars': 100,
'downloads': 5000,
},
};
final toon = encode(data);
print(toon);
Output:
project: TOON Format
version: 0.0.4
author:
name: Vardon Bainbridge
email: vardon.bainbridge@gmail.com
features[3]: encoding,decoding,compression
stats:
stars: 100
downloads: 5000
Arrays of Objects #
final users = {
'users': [
{'name': 'Alice', 'role': 'admin'},
{'name': 'Bob', 'role': 'user'},
{'name': 'Charlie', 'role': 'moderator'},
],
};
print(encode(users));
Output:
users[3]:
- name: Alice
role: admin
- name: Bob
role: user
- name: Charlie
role: moderator
Custom Configuration #
// 4-space indentation
final toon1 = encode(data, indent: 4);
// Pipe-delimited arrays
final toon2 = encode(data, delimiter: '|');
// Custom decode indentation
final decoded = decode(toonString, indent: 4);
π‘ Why TOON? #
Token Efficiency #
TOON significantly reduces token count compared to JSON, crucial for LLM applications where token usage affects both cost and context window limits.
JSON (145 characters, ~40 tokens):
{
"name": "Alice",
"age": 30,
"city": "New York",
"hobbies": ["reading", "coding", "gaming"]
}
TOON (87 characters, ~24 tokens):
name: Alice
age: 30
city: New York
hobbies[3]: reading,coding,gaming
Savings: ~40% fewer tokens! π
Use Cases #
- LLM Prompts: Reduce token usage in prompts and responses
- API Payloads: Smaller request/response bodies
- Configuration Files: More readable than JSON, more compact than YAML
- Data Exchange: Efficient serialization for microservices
- Log Files: Compact, human-readable log entries
- Cache Keys: Smaller serialized values for caching
π TOON Format Specification #
Primitives #
- Strings: Unquoted when safe, double-quoted otherwise
- Numbers: As-is (integers and decimals)
- Booleans:
trueorfalse - Null:
null
Objects #
Key-value pairs with colon separator:
name: Alice
age: 30
Nested Objects #
Indentation-based nesting:
user:
name: Alice
profile:
bio: Software Engineer
location: NYC
Arrays #
Inline (for primitives):
numbers[5]: 1,2,3,4,5
colors[3]: red,green,blue
Multi-line (for complex items):
users[2]:
- name: Alice
- name: Bob
Quoted Strings #
Strings requiring quotes (contain special chars, numbers, or keywords):
"key with spaces": value
url: "https://example.com"
special: "value,with,commas"
numeric: "12345"
π§ API Reference #
encode(dynamic value, {int indent = 2, String delimiter = ','}) #
Encodes a Dart value into TOON format.
Parameters:
value: Map, List, or primitive to encodeindent: Spaces per indentation level (default: 2)delimiter: Array item delimiter (default: ',')
Returns: String in TOON format
Example:
final toon = encode({'name': 'Alice', 'age': 30});
final toon4 = encode(data, indent: 4);
final toonPipe = encode(data, delimiter: '|');
decode(String toonString, {int indent = 2}) #
Decodes TOON format into a Dart value.
Parameters:
toonString: TOON formatted stringindent: Expected indentation size (default: 2)
Returns: Decoded Map, List, or primitive
Example:
final data = decode('name: Alice\\nage: 30');
final data4 = decode(toonString, indent: 4);
Delimiters #
Available delimiters for array encoding:
Delimiters.comma-,(default)Delimiters.tab-\\tDelimiters.pipe-|
π§ͺ Testing #
Run the test suite:
dart test
Run the example:
dart run example/main.dart
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π€ Author #
Vardon Bainbridge
- Email: vardon.bainbridge@gmail.com
- GitHub: @vardonbainbridge
π Show Your Support #
Give a βοΈ if this project helped you!
π Changelog #
See CHANGELOG.md for a list of changes.
π Links #
π― Key Features #
- π Compact: 30-50% fewer tokens than JSON
- π Human Readable: Easy to read, write, and debug
- π Bidirectional: Full encode/decode support with type preservation
- β‘ Pure Dart: Zero dependencies, works everywhere
- π― Type Safe: Preserves primitives, objects, and arrays
- π οΈ Customizable: Configure indentation and delimiters
- π― Well Tested: Comprehensive test coverage
- π Well Documented: Extensive examples and API documentation
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
toon: ^0.0.1
Then run:
dart pub get
π Quick Start #
Encoding #
import 'package:toon/toon.dart';
void main() {
final data = {
'user': 'Alice',
'age': 30,
'active': true,
'scores': [95, 87, 92],
};
final toon = encode(data);
print(toon);
}
Output:
user: Alice
age: 30
active: true
scores[3]: 95,87,92
Decoding #
import 'package:toon/toon.dart';
void main() {
final toon = '''
user: Alice
age: 30
active: true
scores[3]: 95,87,92
''';
final data = decode(toon);
print(data);
// {user: Alice, age: 30, active: true, scores: [95, 87, 92]}
}
π Complete Examples #
Complex Nested Structures #
final data = {
'project': 'TOON Format',
'version': '0.0.1',
'author': {
'name': 'Vardon Bainbridge',
'email': 'vardon.bainbridge@gmail.com',
},
'features': ['encoding', 'decoding', 'compression'],
'stats': {
'stars': 100,
'downloads': 5000,
},
};
final toon = encode(data);
print(toon);
Output:
project: TOON Format
version: 1.0.0
author:
name: Vardon Bainbridge
email: vardon.bainbridge@gmail.com
features[3]: encoding,decoding,compression
stats:
stars: 100
downloads: 5000
Arrays of Objects #
final users = {
'users': [
{'name': 'Alice', 'role': 'admin'},
{'name': 'Bob', 'role': 'user'},
{'name': 'Charlie', 'role': 'moderator'},
],
};
print(encode(users));
Output:
users[3]:
- name: Alice
role: admin
- name: Bob
role: user
- name: Charlie
role: moderator
Custom Configuration #
// 4-space indentation
final toon1 = encode(data, indent: 4);
// Pipe-delimited arrays
final toon2 = encode(data, delimiter: '|');
// Custom decode indentation
final decoded = decode(toonString, indent: 4);
π‘ Why TOON? #
Token Efficiency #
TOON significantly reduces token count compared to JSON, crucial for LLM applications where token usage affects both cost and context window limits.
JSON (145 characters, ~40 tokens):
{
"name": "Alice",
"age": 30,
"city": "New York",
"hobbies": ["reading", "coding", "gaming"]
}
TOON (87 characters, ~24 tokens):
name: Alice
age: 30
city: New York
hobbies[3]: reading,coding,gaming
Savings: ~40% fewer tokens! π
Use Cases #
- LLM Prompts: Reduce token usage in prompts and responses
- API Payloads: Smaller request/response bodies
- Configuration Files: More readable than JSON, more compact than YAML
- Data Exchange: Efficient serialization for microservices
- Log Files: Compact, human-readable log entries
- Cache Keys: Smaller serialized values for caching
π TOON Format Specification #
Primitives #
- Strings: Unquoted when safe, double-quoted otherwise
- Numbers: As-is (integers and decimals)
- Booleans:
trueorfalse - Null:
null
Objects #
Key-value pairs with colon separator:
name: Alice
age: 30
Nested Objects #
Indentation-based nesting:
user:
name: Alice
profile:
bio: Software Engineer
location: NYC
Arrays #
Inline (for primitives):
numbers[5]: 1,2,3,4,5
colors[3]: red,green,blue
Multi-line (for complex items):
users[2]:
- name: Alice
- name: Bob
Quoted Strings #
Strings requiring quotes (contain special chars, numbers, or keywords):
"key with spaces": value
url: "https://example.com"
special: "value,with,commas"
numeric: "12345"
π§ API Reference #
encode(dynamic value, {int indent = 2, String delimiter = ','}) #
Encodes a Dart value into TOON format.
Parameters:
value: Map, List, or primitive to encodeindent: Spaces per indentation level (default: 2)delimiter: Array item delimiter (default: ',')
Returns: String in TOON format
Example:
final toon = encode({'name': 'Alice', 'age': 30});
final toon4 = encode(data, indent: 4);
final toonPipe = encode(data, delimiter: '|');
decode(String toonString, {int indent = 2}) #
Decodes TOON format into a Dart value.
Parameters:
toonString: TOON formatted stringindent: Expected indentation size (default: 2)
Returns: Decoded Map, List, or primitive
Example:
final data = decode('name: Alice\nage: 30');
final data4 = decode(toonString, indent: 4);
Delimiters #
Available delimiters for array encoding:
Delimiters.comma-,(default)Delimiters.tab-\tDelimiters.pipe-|
π§ͺ Testing #
Run the test suite:
dart test
Run the example:
dart run example/main.dart
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π€ Author #
Vardon Bainbridge
- Email: vardon.bainbridge@gmail.com
- GitHub: @vardonbainbridge
π Show Your Support #
Give a βοΈ if this project helped you!
π Changelog #
See CHANGELOG.md for a list of changes.