Welcome to ToonNet
ToonNet is a .NET library for serializing and deserializing data in TOON (Token-Oriented Object Notation) format. It implements TOON spec v4.4, passes all 564 official conformance fixtures and writes the same text as the official .NET library.
What is TOON?β
TOON is a human-readable data format designed for:
- AI/LLM prompts - Fewer tokens than JSON, especially for arrays of uniform objects
- Configuration files - Clean, readable syntax
- Data exchange - Human and machine friendly
Quick Exampleβ
using ToonNet.Core.Serialization;
// Serialize
var person = new Person { Name = "Alice", Age = 30 };
string toon = ToonSerializer.Serialize(person); // Name: Alice\nAge: 30
// Deserialize
var restored = ToonSerializer.Deserialize<Person>(toon);
public class Person
{
public string Name { get; set; } = "";
public int Age { get; set; }
}
Key Featuresβ
- π Performance - Compiled accessors, cached type metadata, span-based parsing and pooled buffers
- π° Token Efficient - Tabular arrays and keyed tables declare field names once, nested field groups flatten uniform nested objects into the same rows (lower AI API costs)
- π Spec v4.4 - All 564 official conformance fixtures pass; output is identical to the official .NET library
- π― .NET 8, .NET 10 and .NET 11 - Builds for .NET 8 and .NET 10; tested on .NET 11 too
- π» Developer Friendly - System.Text.Json-style API
- π Streaming Support - Multi-document streaming from
IAsyncEnumerable<T>without materializing the whole dataset - π§ ASP.NET Core - Input/output formatters, configuration provider
- π¦ Format Extensions - JSON/YAML bidirectional conversion
- π― Source Generators - Optional generated
Serialize/Deserializemethods with the same output asToonSerializer
Beyond Serializationβ
Other TOON libraries convert values to and from text. ToonNet also lets you work with TOON data where it is, without converting it first:
| You want to | With ToonNet | Page |
|---|---|---|
| Find values: the rows over a limit, the users of a domain | JSONPath (RFC 9535, all 715 compliance cases) plus string, type and aggregate functions: $.orders[?sum(@.lines[*].price) > 1000] | Querying |
| Read one part of a large document | DeserializeAt<T>(toon, "$.data.orders") skips the rest without building it (about 20x faster for a small part) | Querying |
| Read only the rows that match | DeserializeAll<T>(toon, "$.orders[?@.total > 100]"), filtered on the text (2.3x faster, 13x less memory than LINQ after deserializing) | Querying |
| Stream a large array inside a response | DeserializeAsyncEnumerableAt<T>(stream, "$.data.orders") with flat memory | Streaming |
| Check business rules | ToonRuleSet: Schematron-style rules ("@.total >= 0", Unique, Forbidden("$..password")) with the path of every violation | Rules and Validation |
| Change a document | Indexers, SetValue/RemoveAll by path, DeepClone, DeepEquals, JSON Merge Patch | Editing Trees |
| Talk to a language model | Shortened data for prompts, answer templates, lenient and validated reading of answers | Language Models |
| Accept queries from clients | Limits on length, nesting, work and time (ToonPathOptions), linear-time regular expressions | Querying |
Documentation Guideβ
π Getting Startedβ
Start here if you're new to ToonNet:
- Installation - NuGet packages and requirements
- Migrating to 4.0 - Changed defaults and how to keep the old behavior
- Quick Start - 5-minute tutorial
- Basic Serialization - Fundamental examples
π― Core Featuresβ
Deep dive into ToonNet's core functionality:
- Serialization - Convert objects to TOON
- Deserialization - Convert TOON to objects
- Streaming - Multi-document files and the items of large arrays, one at a time
- Reader and Writer - Token-level reading and writing without a tree
- Querying - JSONPath with ToonNet functions, over trees, text and into objects
- Rules and Validation - Business rules written as paths
- Editing Trees - Navigate, change and patch documents
- Language Models - Prompts and answers
- Type System - ToonValue and subclasses
- Configuration - ToonSerializerOptions guide
π Format Extensionsβ
Convert between different data formats:
- JSON Integration - JSON β TOON conversion
- YAML Integration - YAML β TOON conversion
- Converter Examples - Converters for dates, identifiers and value objects
π ASP.NET Coreβ
Integrate ToonNet with ASP.NET Core:
- Dependency Injection - Service configuration
- Input Formatters - Handle TOON requests
- Output Formatters - Return TOON responses
- Configuration Provider - TOON config files
β‘ Advanced Topicsβ
Optimization and customization:
- Performance Tuning - Optimization strategies
- Custom Converters - Type-specific converters
- Source Generators - Compile-time code generation
- Security - Defaults and limits for untrusted input
π Referenceβ
Additional resources:
- API Guide - The API at a glance, in the style of System.Text.Json
- TOON Spec - TOON spec v4.4 conformance and implementation notes
- ToonNet vs Toon.Format - Features and measurements next to the official library
Quick Linksβ
| Topic | Link |
|---|---|
| Installation | Getting Started |
| First Example | Quick Start |
| API Overview | API Guide |
| Large Files | Streaming |
| Compared With Toon.Format | ToonNet vs Toon.Format |
| GitHub Repository | ToonNet on GitHub |
Community & Supportβ
- GitHub Issues: Report bugs or request features
- Discussions: Ask questions and share ideas
Licenseβ
ToonNet is open-source software licensed under the MIT License.
Thread-Safetyβ
ToonSerializer and a shared ToonEncoder are safe to use from several threads; see Configuration for the caches and options.