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.
import 'package:toon_plus/toon_plus.dart';
void main() {
print('=== TOON Converter Example ===\n');
// Example 1: Simple object
print('Example 1: Simple Object');
print('-------------------------');
final simpleData = {
'name': 'Alice',
'age': 30,
'city': 'New York',
'premium': true,
};
final simpleToon = encode(simpleData);
print('Encoded:');
print(simpleToon);
final simpleDecoded = decode(simpleToon);
print('Decoded:');
print(simpleDecoded);
// Example 2: Nested object
print('Example 2: Nested Object');
print('-------------------------');
final nestedData = {
'user': {
'name': 'Bob',
'age': 25,
'address': {
'city': 'San Francisco',
'zip': '94102',
},
},
};
final nestedToon = encode(nestedData);
print('Encoded:');
print(nestedToon);
final nestedDecoded = decode(nestedToon);
print('Decoded:');
print(nestedDecoded);
// Example 3: Arrays
print('Example 3: Arrays');
print('-----------------');
final arrayData = {
'numbers': [1, 2, 3, 4, 5],
'colors': ['red', 'green', 'blue'],
'users': [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
{'name': 'Charlie', 'age': 35},
],
};
final arrayToon = encode(arrayData);
print('Encoded:');
print(arrayToon);
final arrayDecoded = decode(arrayToon);
print('Decoded:');
print(arrayDecoded);
// Example 4: Complex structure
print('Example 4: Complex Structure');
print('----------------------------');
final complexData = {
'project': 'TOON Converter',
'version': '1.0.0',
'author': {
'name': 'Vijay Bheda',
'email': 'vijay@example.com',
},
'features': [
'Encoding',
'Decoding',
'Pure Dart',
'Lightweight',
],
'stats': {
'downloads': 1000,
'stars': 50,
'forks': 10,
},
'tags': ['dart', 'flutter', 'serialization', 'toon'],
};
final complexToon = encode(complexData);
print('Encoded:');
print(complexToon);
final complexDecoded = decode(complexToon);
print('Decoded:');
print(complexDecoded);
print('\n');
// Example 5: Custom options
print('Example 5: Custom Options');
print('-------------------------');
final customData = {
'name': 'Test',
'values': [1, 2, 3],
};
// 4-space indentation
final custom1 = encode(customData, indent: 4);
print('4-space indentation:');
print(custom1);
// Pipe delimiter
final custom2 = encode(customData, delimiter: '|');
print('Pipe delimiter:');
print(custom2);
// Example 6: Round-trip conversion
print('Example 6: Round-trip Conversion');
print('--------------------------------');
final originalData = {
'test': 'data',
'number': 42,
'nested': {
'value': true,
},
'list': [1, 2, 3],
};
print('Original data:');
print(originalData);
final encoded = encode(originalData);
print('Encoded to TOON:');
print(encoded);
final decoded = decode(encoded);
print('Decoded back to Dart:');
print(decoded);
print('Data matches: ${_deepEquals(originalData, decoded)}');
}
/// Deep equality check for maps and lists
bool _deepEquals(dynamic a, dynamic b) {
if (a == b) return true;
if (a is Map && b is Map) {
if (a.length != b.length) return false;
for (final key in a.keys) {
if (!b.containsKey(key)) return false;
if (!_deepEquals(a[key], b[key])) return false;
}
return true;
}
if (a is List && b is List) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (!_deepEquals(a[i], b[i])) return false;
}
return true;
}
return false;
}