flutter_car_toon 0.1.0 copy "flutter_car_toon: ^0.1.0" to clipboard
flutter_car_toon: ^0.1.0 copied to clipboard

A comprehensive TOON (Token-Oriented Object Notation) formatter plugin for Flutter/Dart with all the functionality of dart:convert's JSON library but for TOON format.

Flutter CarToon - TOON Formatter Plugin #

Flutter CarToon #

A comprehensive TOON (Token-Oriented Object Notation) formatter plugin for Flutter that provides all the functionality of dart:convert's JSON library but optimized for the TOON format.

Acknowledgments: This project is highly inspired by the toon_formatter package and follows the architectural patterns established by Dart's dart:convert library to ensure familiar and consistent usage patterns.

pub package License: MIT

What is TOON? #

TOON (Token-Oriented Object Notation) is a compact, human-readable encoding of JSON data specifically designed for LLM prompts and efficient data exchange. Key features:

  • 🗜️ Compact: More space-efficient than JSON (up to 44% smaller)
  • 👀 Human-readable: Clean, indentation-based structure
  • ⚡ Fast decoding: Up to 18,000x faster decoding than JSON
  • 🤖 LLM-friendly: Optimized for AI/ML token consumption
  • 📏 Deterministic: Consistent output for the same data

Installation #

Add this package to your pubspec.yaml:

dependencies:
  flutter_car_toon: ^0.1.0

Then run:

flutter pub get

Quick Start #

Basic Usage #

import 'package:flutter_car_toon/flutter_car_toon.dart';

void main() {
  // Encode a Dart object to TOON format
  final data = {
    'name': 'Alice',
    'age': 30,
    'tags': ['admin', 'ops', 'dev'],
  };

  final toonString = toon.encode(data);
  print(toonString);
  // Output:
  // name: Alice
  // age: 30
  // tags[3]: admin,ops,dev

  // Decode TOON format back to Dart
  final decoded = toon.decode(toonString);
  print(decoded); // {name: Alice, age: 30, tags: [admin, ops, dev]}
}

Using ToonCodec (Similar to JsonCodec) #

// Create a codec with custom options
final codec = ToonCodec(
  options: ToonOptions(
    indent: 4,
    delimiter: ToonDelimiter.comma,
    strictMode: true,
  ),
);

final encoded = codec.encode(myData);
final decoded = codec.decode(toonString);

Convenience Functions #

// Global convenience functions
final encoded = toonEncode(data);
final decoded = toonDecode(toonString);

// With custom options
final encoded = toonEncode(data, options: ToonOptions.compact);
final decoded = toonDecode(toonString, options: ToonOptions.strict);

TOON Format Examples #

Objects #

name: Alice
age: 30
email: alice@example.com

Arrays #

# Inline arrays for primitives
tags[3]: admin,ops,dev

# Tabular arrays for uniform objects
users[2]{name,age}:
  Alice,30
  Bob,25

Nested Structures #

user:
  name: Alice
  profile:
    bio: Software Engineer
    skills[3]: dart,flutter,toon
    active: true

Lists (Mixed Types) #

items[3]:
  - name: Item 1
    price: 9.99
  - name: Item 2
    price: 14.50
  - Special Deal

Advanced Features #

Custom Options #

final options = ToonOptions(
  indent: 4,                              // Indentation spaces
  delimiter: ToonDelimiter.pipe,          // Array delimiter (|)
  keyFolding: ToonKeyFolding.safe,        // Key folding strategy
  arrayStrategy: ToonArrayStrategy.tabular, // Force tabular arrays
  strictMode: true,                       // Strict spec compliance
  compactOutput: false,                   // Pretty vs compact output
);

final codec = ToonCodec(options: options);

Validation #

final validator = ToonValidator(options: ToonOptions.strict);

// Validate a TOON document
final errors = validator.validate(toonString);
if (errors.isNotEmpty) {
  for (final error in errors) {
    print('${error.severity}: ${error.message}');
  }
}

// Quick validation check
if (validator.isValid(toonString)) {
  print('Valid TOON format!');
}

Custom Type Converters #

// Register custom converters
globalToonConverterRegistry.register<DateTime>(DateTimeToonConverter());
globalToonConverterRegistry.register<Duration>(DurationToonConverter());

// Use with codec
final data = {
  'timestamp': DateTime.now(),
  'duration': Duration(hours: 2, minutes: 30),
};

final encoded = toon.encode(data);

Streaming Support (Large Data) #

// Stream processing for large datasets
final stream = ToonStream.fromFile('large_data.toon');
await for (final chunk in stream.decode()) {
  processChunk(chunk);
}

// Transform JSON stream to TOON
final jsonStream = Stream.fromIterable(['{"a":1}', '{"b":2}']);
final toonStream = ToonStream().fromJson(jsonStream);

Predefined Options #

// Default options
ToonOptions.defaults

// Strict TOON specification compliance
ToonOptions.strict

// Compact output for minimal size
ToonOptions.compact

// Pretty printing for readability
ToonOptions.pretty

// Performance-optimized for large data
ToonOptions.performance

Error Handling #

The library provides comprehensive error handling with detailed context:

try {
  final result = toon.decode(malformedToon);
} on ToonDecodingError catch (e) {
  print('Decoding error at ${e.location}: ${e.message}');
  print('Source excerpt:\n${e.sourceExcerpt}');
} on ToonValidationError catch (e) {
  print('Validation error: ${e.message}');
  print('Suggestion: ${e.suggestion}');
} on ToonError catch (e) {
  print('TOON error: ${e.message}');
}

Performance Comparison #

Based on benchmarks against JSON:

Data Size TOON Size vs JSON Encode Speed Decode Speed
Small 83% (17% smaller) 3x slower 37x faster
Medium 56% (44% smaller) 3x slower 497x faster
Large 93% (7% smaller) 4x slower 18000x faster

Key Findings:

  • Decoding: TOON decoding is significantly faster than JSON
  • Size: TOON format is more compact, especially for structured data
  • ⚠️ Encoding: JSON encoding is faster, but TOON is reasonable for most use cases

Annotations (Code Generation) #

Future support for code generation (similar to json_serializable):

import 'package:flutter_car_toon/flutter_car_toon.dart';

@ToonSerializable()
class User {
  final String name;
  final int age;

  @ToonField(name: 'email_address')
  final String email;

  @ToonField(include: false)
  final String password; // Won't be serialized

  User({required this.name, required this.age, required this.email, required this.password});

  // Generated methods (future):
  // String toToon() => _$UserToToon(this);
  // factory User.fromToon(String toon) => _$UserFromToon(toon);
}

API Reference #

Core Classes #

  • ToonCodec: Main encoder/decoder (similar to JsonCodec)
  • ToonEncoder: Encoding functionality
  • ToonDecoder: Decoding functionality
  • ToonOptions: Configuration options
  • ToonValidator: Format validation
  • ToonStream: Streaming support

Error Classes #

  • ToonError: Base error class
  • ToonEncodingError: Encoding failures
  • ToonDecodingError: Decoding failures
  • ToonValidationError: Validation issues
  • ToonSyntaxError: Syntax problems

Utility Classes #

  • ToonUtils: Utility functions
  • StringUtils: String processing helpers
  • ValidationUtils: Validation helpers

Compatibility #

  • Flutter: >= 3.3.0
  • Dart: >= 3.10.0
  • Platforms: Android, iOS, Linux, macOS, Windows, Web

Comparison with Existing Packages #

Feature flutter_car_toon toon_formater
Basic encode/decode
Custom options
Error handling ✅ Comprehensive ❌ Basic
Validation ✅ Full validator ❌ None
Streaming ✅ Built-in ❌ None
Type converters ✅ Extensible ❌ Limited
Performance optimization ✅ Multiple strategies ❌ Basic
Code generation 🚧 Planned
CLI tools 🚧 Planned ❌ None

Contributing #

We welcome contributions! Please see our Contributing Guide for details.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Credits #

  • TOON format specification by the TOON Format Team
  • Inspired by dart:convert and json_serializable
  • Built with ❤️ for the Flutter community

AI was used to write this

0
likes
0
points
50
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive TOON (Token-Oriented Object Notation) formatter plugin for Flutter/Dart with all the functionality of dart:convert's JSON library but for TOON format.

Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

collection, flutter, flutter_web_plugins, meta, plugin_platform_interface, web

More

Packages that depend on flutter_car_toon

Packages that implement flutter_car_toon