Thanks to visit codestin.com
Credit goes to github.com

Skip to content

AIDotNet/Toon.NET

Repository files navigation

AIDotNet.Toon

.NET implementation of Token-Oriented Object Notation (TOON), aligned with the https://github.com/toon-format/toon specification, providing a consistent API experience and options model similar to System.Text.Json.

  • High-performance encoding: Object, inline atomic arrays, tabular object arrays, and other paths are implemented
  • Decoding pipeline: Scanning/parsing/validation is in progress, currently supports atomic value reading
  • Design follows "minimal allocation, readability first" engineering trade-offs

中文文档

C#.ToonSerializer · C#.ToonSerializerOptions · C#.Encoders.EncodeValue() · C#.Primitives.EncodePrimitive() · C#.LineWriter

Badges

Table of Contents

Installation

NuGet:

dotnet add package AIDotNet.Toon

Source code method:

  • Add the src/AIDotNet.Toon directory to your solution, or include it as a submodule
  • Enable package Readme in your csproj (already configured in this project): see src/AIDotNet.Toon/AIDotNet.Toon.csproj

Quick Start

Serialize to TOON:

using Toon;

var options = new ToonSerializerOptions
{
    Indent = 2,
    Delimiter = ToonDelimiter.Comma,
    Strict = true,
    LengthMarker = null
};

var data = new
{
    users = new[]
    {
        new { name = "alice", age = 30 },
        new { name = "bob", age = 25 }
    },
    tags = new[] { "a", "b", "c" },
    numbers = new[] { 1, 2, 3 }
};

string toonText = ToonSerializer.Serialize(data, options);
// users[2]{name,age}:
//   1,alice
//   2,bob
// tags[3]: a,b,c
// numbers[3]: 1,2,3

Deserialize from TOON to .NET (currently only atomic values are stable, other structures will improve as the decoder is enhanced):

using Toon;

var s = ToonSerializer.Deserialize<string>("hello", options);   // "hello"
var n = ToonSerializer.Deserialize<double>("3.1415", options);  // 3.1415

Related APIs: C#.ToonSerializer.Serialize() · C#.ToonSerializer.Deserialize()

API & Options

Public API:

Options model: C#.ToonSerializerOptions

  • Indent: Number of spaces per indentation level, default 2
  • Delimiter: Delimiter, enumeration C#.ToonDelimiter (Comma / Tab / Pipe)
  • Strict: Strict mode for decoding (indentation/blank lines/extra items validation), default true
  • LengthMarker: Array length marker, only supports # or null, default null
  • JsonOptions: Pass-through to System.Text.Json's C#.JsonSerializerOptions, defaults to enabling named float literals and registers converters that write NaN/±Infinity as null: C#.DoubleNamedFloatToNullConverter / C#.SingleNamedFloatToNullConverter

Default instance: C#.ToonSerializerOptions.Default

Encoding Rules & Format

Entry point: C#.Encoders.EncodeValue()

Objects:

Atomic arrays (inline):

Object arrays (tabular):

Mixed/complex arrays (fallback to list):

Atoms/strings:

Lines and indentation:

Example Snippets

Simple object:

var obj = new { a = 1, b = "x" };
var toon = ToonSerializer.Serialize(obj);
// a: 1
// b: x

Atomic array (default comma-separated):

var arr = new[] { 1, 2, 3 };
ToonSerializer.Serialize(arr); // "[3]: 1,2,3"

Object array (tabular):

var rows = new[] { new { id = 1, name = "alice" }, new { id = 2, name = "bob" } };
ToonSerializer.Serialize(rows);
// [2]{id,name}:
//   1,alice
//   2,bob

Bytes and streams:

// Bytes
var bytes = ToonSerializer.SerializeToUtf8Bytes(rows);
var rowsFromBytes = ToonSerializer.Deserialize<List<Dictionary<string, object>>>(bytes);

// Stream
using var ms = new MemoryStream();
ToonSerializer.Serialize(rows, ms);   // Write UTF-8 (no BOM), keep stream open
ms.Position = 0;
var rowsFromStream = ToonSerializer.Deserialize<List<Dictionary<string, object>>>(ms);

Special number handling:

ToonSerializer.Serialize(new { v = double.NaN });            // "v: null"
ToonSerializer.Serialize(new { v = double.PositiveInfinity }); // "v: null"
ToonSerializer.Serialize(new { v = BitConverter.Int64BitsToDouble(unchecked((long)0x8000000000000000)) }); // "v: 0"

Related assertions can be seen in tests: tests/AIDotNet.Toon.Tests/EncodeTests.cs

Performance & Implementation Details

This implementation minimizes allocations and unnecessary branches while maintaining output readability:

Publishing recommendations: Use Release build; consider R2R/ReadyToRun to improve startup performance.

Alignment with TypeScript Specification

Version Support

Roadmap

  • Decoding: Scanner / Parser / Validation / Decoders
  • Strict mode error model: Provide row/column and context-aware C#.ToonFormatException
  • Normalization strategy (Normalize): Cross-language consistency for dates/collections, etc.
  • Documentation and example improvements, publish NuGet package

Development & Testing

  • Run tests:
dotnet test
  • Local packaging:
dotnet pack -c Release

Contributing

Issues and PRs are welcome. Please try to:

  • Keep the public API consistent with the System.Text.Json style
  • Prioritize readability/real benefits when optimizing
  • Add unit tests for new paths and edge conditions

License

MIT © AIDotNet.Toon Contributors

Acknowledgments: Thanks to the upstream project https://github.com/toon-format/toon for design and implementation reference.