Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

delphi-toon

TOON Format for Delphi

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

TOON Format for Delphi

⚠️ Beta Status (v0.9.x): This library is in active development and working towards spec compliance.

Compact, human-readable serialization format for LLM contexts with 30-60% token reduction vs JSON. Combines YAML-like indentation with CSV-like tabular arrays. Working towards full compatibility with the official TOON specification.

Key Features: Minimal syntax • Tabular arrays for uniform data • Array length validation • Delphi 11+ compatible • Comprehensive test coverage.

Installation

  1. Clone or download this repository
  2. Add the src folder to your Delphi search path
  3. Add Toon.Format to your uses clause

Quick Start

pascal
uses
  Toon.Format;

var
  Encoder: TToonEncoder;
  Decoder: TToonDecoder;
  JsonValue: TJSONValue;
  ToonStr: string;
begin
  // Create encoder
  Encoder := TToonEncoder.Create;
  try
    // Simple object
    JsonValue := TJSONObject.ParseJSONValue('{"name": "Alice", "age": 30}');
    try
      ToonStr := Encoder.Encode(JsonValue);
      // name: Alice
      // age: 30
    finally
      JsonValue.Free;
    end;
  finally
    Encoder.Free;
  end;

  // Create decoder
  Decoder := TToonDecoder.Create;
  try
    // Decode back to JSON
    JsonValue := Decoder.Decode('name: Alice' + sLineBreak + 'age: 30');
    try
      // Use JsonValue...
    finally
      JsonValue.Free;
    end;
  finally
    Decoder.Free;
  end;
end;

Format Specification

Type Example Input TOON Output
Object {"name": "Alice", "age": 30} name: Alice
age: 30
Primitive Array [1, 2, 3] [3]: 1,2,3
Tabular Array [{"id": 1, "name": "A"}, {"id": 2, "name": "B"}] [2,]{id,name}:
  1,A
  2,B
Mixed Array [{"x": 1}, 42, "hi"] [3]:
  - x: 1
  - 42
  - hi

API Reference

TToonEncoder

pascal
TToonEncoder = class
public
  constructor Create(AOptions: TToonEncodeOptions = nil);
  destructor Destroy; override;

  function Encode(Value: TJSONValue): string;

  property Options: TToonEncodeOptions read FOptions write FOptions;
end;

TToonDecoder

pascal
TToonDecoder = class
public
  constructor Create(AOptions: TToonDecodeOptions = nil);
  destructor Destroy; override;

  function Decode(const Input: string): TJSONValue;

  property Options: TToonDecodeOptions read FOptions write FOptions;
end;

Options

pascal
// Encoding options
TToonEncodeOptions = class
  property Indent: Integer;       // Spaces per level (default: 2)
  property Delimiter: TDelimiter; // comma, tab, or pipe (default: comma)
  property LengthMarker: Boolean; // Prefix array lengths with # (default: False)
end;

// Decoding options
TToonDecodeOptions = class
  property Indent: Integer;   // Expected indent size (default: 2)
  property Strict: Boolean;   // Validate syntax (default: True)
end;

Format Examples

Objects

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

Primitive Arrays

pascal
[3]: 1,2,3

Tabular Arrays (Uniform Objects)

pascal
[2,]{id,name,active}:
  1,Alice,true
  2,Bob,false

Mixed/Non-uniform Arrays

pascal
[3]:
  - type: user
    id: 1
  - 42
  - hello world

Project Status & Roadmap

Following semantic versioning towards 1.0.0:

  • v0.8.x - Initial code set, tests, documentation ✅
  • v0.9.x - Encoder/decoder implementation, spec compliance testing (current)
  • v1.0.0-rc.x - Release candidates for production readiness
  • v1.0.0 - First stable release with full spec compliance

Documentation

Contributors

This Delphi implementation is a community-driven port based on the Python reference implementation.

License

MIT License – see LICENSE for details.