ToonNet.Extensions.Yaml
4.0.0
See the version list below for details.
dotnet add package ToonNet.Extensions.Yaml --version 4.0.0
NuGet\Install-Package ToonNet.Extensions.Yaml -Version 4.0.0
<PackageReference Include="ToonNet.Extensions.Yaml" Version="4.0.0" />
<PackageVersion Include="ToonNet.Extensions.Yaml" Version="4.0.0" />
<PackageReference Include="ToonNet.Extensions.Yaml" />
paket add ToonNet.Extensions.Yaml --version 4.0.0
#r "nuget: ToonNet.Extensions.Yaml, 4.0.0"
#:package ToonNet.Extensions.Yaml@4.0.0
#addin nuget:?package=ToonNet.Extensions.Yaml&version=4.0.0
#tool nuget:?package=ToonNet.Extensions.Yaml&version=4.0.0
ToonNet.Extensions.Yaml
YAML โ TOON format conversion extension
๐ฆ What is ToonNet.Extensions.Yaml?
ToonNet.Extensions.Yaml provides seamless bidirectional conversion between YAML and TOON formats:
- โ YAML โ TOON - Convert YAML strings/documents to TOON format
- โ TOON โ YAML - Convert TOON strings/documents to YAML format
- โ YamlDotNet integration - Industry-standard YAML parser
- โ Preserves structure - Round-trip conversions keep mappings, sequences and scalar values
- โ Common YAML features - Mappings, sequences, scalars, flow notation, anchors and aliases (expanded into copies)
Perfect for:
- โ๏ธ Configuration Files - Convert YAML configs to TOON format
- ๐ณ Docker/Kubernetes - Work with container configurations
- ๐ CI/CD - Transform GitHub Actions, GitLab CI, etc.
- ๐ OpenAPI/Swagger - Convert API specifications
- ๐ Cross-format - YAML โ TOON โ JSON workflows
๐ Quick Start
Installation
# Core package (required)
dotnet add package ToonNet.Core
# YAML extension
dotnet add package ToonNet.Extensions.Yaml
Basic Usage - Document Conversion
using ToonNet.Extensions.Yaml;
// YAML โ TOON document
var yaml = """
name: Alice
age: 30
tags:
- dev
- admin
settings:
theme: dark
notifications: true
""";
var toonDoc = ToonYamlConverter.FromYaml(yaml);
// Access data
var root = (ToonObject)toonDoc.Root;
var name = ((ToonString)root["name"]).Value; // "Alice"
var age = ((ToonNumber)root["age"]).Value; // 30
// TOON โ YAML document
var yamlOutput = ToonYamlConverter.ToYaml(toonDoc);
String Format Conversion (High-level API)
using ToonNet.Extensions.Yaml;
// YAML โ TOON string conversion
string yamlString = """
name: Alice
age: 30
hobbies:
- reading
- coding
""";
string toonString = ToonYamlConvert.FromYaml(yamlString);
// Output (TOON format):
// name: Alice
// age: 30
// hobbies[2]: reading,coding
// TOON โ YAML string conversion
string yamlBack = ToonYamlConvert.ToYaml(toonString);
๐ API Reference
String Format Conversion
// YAML string โ TOON string
string toon = ToonYamlConvert.FromYaml(yamlString);
string toon = ToonYamlConvert.FromYaml(yamlString, options); // ToonOptions
// TOON string โ YAML string
string yaml = ToonYamlConvert.ToYaml(toonString);
string yaml = ToonYamlConvert.ToYaml(toonString, options); // ToonOptions for parsing the TOON input
Document Conversion (Low-level)
using ToonNet.Extensions.Yaml;
// YAML string โ ToonDocument
ToonDocument doc = ToonYamlConverter.FromYaml(yamlString);
ToonDocument doc = ToonYamlConverter.FromYaml(yamlString, new ToonOptions { MaxDepth = 50 });
// ToonDocument โ YAML string
string yaml = ToonYamlConverter.ToYaml(document);
// ToonValue โ YAML string
string yaml = ToonYamlConverter.ToYaml(toonValue);
Architecture Note: ToonNet uses a layered approach for YAML interop:
ToonYamlConverter- Low-level conversion between YAML nodes โToonDocument/ToonValue. Used internally as the core conversion engine.ToonYamlConvert- High-level, developer-friendly API (similar toToonConvertfor JSON). Provides simple string-based conversions and internally usesToonYamlConverter.
This separation of concerns ensures clean architecture: ToonYamlConverter handles the conversion logic, while ToonYamlConvert provides an ergonomic interface familiar to .NET developers.
Limits
YAML input is checked before it is loaded:
- Nesting deeper than
ToonOptions.MaxDepth(default 100) throws aYamlException. - Aliases (
*anchor) are expanded into copies; input whose aliases expand to more thanToonYamlConverter.MaxAliasExpansionNodes(100,000) nodes, or copy more thanToonYamlConverter.MaxAliasExpansionCharscharacters (1,000,000, or ten times the length of the YAML when that is more), throws aYamlException. - Duplicate keys throw a
YamlException, also keys that differ only in their tag (aand!!str a). - The nesting limit counts the expanded aliases too, and an alias inside the node its anchor marks (a recursive
structure such as
&a [*a]) throws aYamlException.
Type Support
| YAML Type | TOON Type | Examples |
|---|---|---|
| Mapping (object) | ToonObject |
key: value |
| Sequence (array) | ToonArray |
- item1 |
| Scalar (string) | ToonString |
name: Alice |
| Scalar (number) | ToonNumber |
age: 30 |
| Scalar (boolean) | ToonBoolean |
enabled: true |
| Null | ToonNull |
value: null |
๐ฏ Real-World Examples
Example 1: Configuration File Conversion
using ToonNet.Extensions.Yaml;
// Load YAML configuration
string yamlConfig = """
database:
host: localhost
port: 5432
credentials:
username: admin
password: secret
logging:
level: info
outputs:
- console
- file
""";
// Convert to TOON format (more compact)
string toonConfig = ToonYamlConvert.FromYaml(yamlConfig);
// Output (TOON format):
// database:
// host: localhost
// port: 5432
// credentials:
// username: admin
// password: secret
// logging:
// level: info
// outputs[2]: console,file
// Convert back to YAML if needed
string yamlBack = ToonYamlConvert.ToYaml(toonConfig);
Example 2: Kubernetes Manifest Conversion
using ToonNet.Extensions.Yaml;
// Load Kubernetes YAML
var k8sYaml = """
apiVersion: v1
kind: Service
metadata:
name: my-service
labels:
app: myapp
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
""";
// Convert to TOON (more readable for analysis)
string toonManifest = ToonYamlConvert.FromYaml(k8sYaml);
// Analyze with TOON, then convert back
string modifiedYaml = ToonYamlConvert.ToYaml(toonManifest);
Example 3: Cross-Format Conversion (YAML โ JSON)
using ToonNet.Extensions.Json;
using ToonNet.Extensions.Yaml;
// YAML โ TOON โ JSON
var yaml = """
database:
host: localhost
port: 5432
""";
string toon = ToonYamlConvert.FromYaml(yaml);
string json = ToonConvert.ToJson(toon);
// JSON โ TOON โ YAML (reverse)
var jsonStr = """{"name":"Alice","tags":["dev","admin"]}""";
string toonFromJson = ToonConvert.FromJson(jsonStr);
string yamlOutput = ToonYamlConvert.ToYaml(toonFromJson);
Example 4: GitHub Actions Workflow Analysis
using ToonNet.Extensions.Yaml;
using ToonNet.Core.Serialization;
// Load GitHub Actions workflow
var workflowYaml = """
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: dotnet build
""";
// Convert to TOON format
string toonWorkflow = ToonYamlConvert.FromYaml(workflowYaml);
// Save as TOON for easier reading/editing
File.WriteAllText("workflow.toon", toonWorkflow);
// Convert back when needed
string yamlOutput = ToonYamlConvert.ToYaml(toonWorkflow);
// Or inspect it as a document
var doc = ToonYamlConverter.FromYaml(workflowYaml);
var jobs = (ToonObject)doc.AsObject()["jobs"]!;
var buildJob = (ToonObject)jobs["build"]!;
var steps = (ToonArray)buildJob["steps"]!;
Console.WriteLine($"Workflow has {steps.Items.Count} steps");
Keys that are not plain identifiers are quoted in TOON ("runs-on": ubuntu-latest).
โจ YAML Features Supported
Quoted and block scalars ("42", 'true', |, >) and scalars tagged !!str are strings, unless an explicit
core-schema tag says otherwise (!!int "42" is 42; a value that does not fit its tag is a YamlException). Plain scalars
are typed by the YAML 1.2 core schema: true/false (lowercase, capitalized or uppercase) become booleans, null,
~ and empty values become null, and integers and floats become numbers (exact when they fit in a decimal).
Anything else stays a string, including the YAML 1.1 booleans yes/no/on/off and .inf/.nan, which a TOON
number cannot hold. When converting back, strings that would otherwise read as another type are quoted ("42",
"true", "007", "", and "yes", "2001-12-14", "<<", "=" and text that starts with a byte order mark for
YAML 1.1 readers), so a TOON โ YAML โ TOON round trip keeps them strings.
Booleans
enabled: true # a boolean
disabled: FALSE # a boolean
legacy_yes: yes # the string "yes" (a boolean only in YAML 1.1)
switch_on: on # the string "on"
Number Formats
integer: 42
float: 3.14
short: .5 # 0.5
scientific: 1.5e-10
hex: 0xFF # 255
octal: 0o77 # 63
zip: 007 # 7; quote it ("007") to keep the string
infinity: .inf # the string ".inf"
quoted: "42" # a string
Complex Structures
# Nested objects
user:
profile:
settings:
theme: dark
# Mixed arrays
items:
- name: Item 1
price: 9.99
- name: Item 2
price: 19.99
# Inline notation
tags: [dev, admin, user]
coords: {x: 10, y: 20}
๐ Round-Trip Behavior
YAML โ TOON โ YAML conversions preserve structure:
var originalYaml = """
user:
name: Bob
roles:
- admin
- editor
""";
var toonDoc = ToonYamlConverter.FromYaml(originalYaml);
var roundtripYaml = ToonYamlConverter.ToYaml(toonDoc);
// Structure preserved (formatting may differ)
Note: Comments are not preserved, aliases are expanded into copies, and only the first document of a
multi-document stream is converted. Mapping keys must be scalars (a sequence or mapping used as a key throws a
YamlException); an empty key is kept as the TOON key "". Merge keys (<<: *base) are not expanded; << stays an
ordinary key.
๐ Thread-Safety
ToonSerializerand YAML conversion methods are safe to call concurrently across threads.- Shared metadata/name caches use
ConcurrentDictionaryfor concurrent access. - Cache entries are created on demand and retained for the process lifetime (no eviction).
- Do not mutate a single
ToonSerializerOptionsinstance concurrently across threads.
๐ Related Packages
Core:
ToonNet.Core- Core serialization (required)
Other Extensions:
ToonNet.Extensions.Json- JSON โ TOON conversion
Web Integration:
ToonNet.AspNetCore- TOON configuration provider and DI integrationToonNet.AspNetCore.Mvc- MVC formatters
Development:
ToonNet.Demo- Sample applicationsToonNet.Tests- YAML conversion test suite
๐ Documentation
- Main Documentation - Complete ToonNet guide
- API Guide - Detailed API reference
- Samples - Real-world examples
๐งช Testing
# Run YAML conversion tests
cd tests/ToonNet.Tests
dotnet test --filter "FullyQualifiedName~ToonYamlConverter"
๐ Requirements
- .NET 8.0 or later
- ToonNet.Core
- YamlDotNet 18.1.0+ (installed automatically)
๐ License
MIT License - See LICENSE file for details.
๐ค Contributing
Contributions welcome! Please read CONTRIBUTING.md first.
Part of the ToonNet serialization library family.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ToonNet.Core (>= 4.0.0)
- YamlDotNet (>= 18.1.0)
-
net8.0
- ToonNet.Core (>= 4.0.0)
- YamlDotNet (>= 18.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.