ToonNet.AspNetCore
1.1.0
See the version list below for details.
dotnet add package ToonNet.AspNetCore --version 1.1.0
NuGet\Install-Package ToonNet.AspNetCore -Version 1.1.0
<PackageReference Include="ToonNet.AspNetCore" Version="1.1.0" />
<PackageVersion Include="ToonNet.AspNetCore" Version="1.1.0" />
<PackageReference Include="ToonNet.AspNetCore" />
paket add ToonNet.AspNetCore --version 1.1.0
#r "nuget: ToonNet.AspNetCore, 1.1.0"
#:package ToonNet.AspNetCore@1.1.0
#addin nuget:?package=ToonNet.AspNetCore&version=1.1.0
#tool nuget:?package=ToonNet.AspNetCore&version=1.1.0
ToonNet.AspNetCore
ASP.NET Core integration for ToonNet serialization
๐ฆ What is ToonNet.AspNetCore?
ToonNet.AspNetCore provides seamless integration of ToonNet serialization with ASP.NET Core:
- โ Dependency Injection - Register ToonParser, ToonEncoder, and options
- โ Configuration Binding - Load settings from appsettings.json
- โ Options Validation - Fail-fast on invalid configuration
- โ TOON Configuration Provider - Read TOON files as configuration source
- โ Middleware Ready - Foundation for MVC formatters and middleware
Perfect for:
- ๐ Web APIs - Serve TOON-formatted responses
- โ๏ธ Configuration - Load TOON config files
- ๐ง DI Integration - Inject ToonParser/Encoder into services
- ๐ Options Pattern - Configure ToonNet via appsettings.json
๐ Quick Start
Installation
# Core package (required)
dotnet add package ToonNet.Core
# ASP.NET Core integration
dotnet add package ToonNet.AspNetCore
# For MVC formatters (optional)
dotnet add package ToonNet.AspNetCore.Mvc
Basic Setup - Default Options
using ToonNet.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Register ToonNet services with default options
builder.Services.AddToon();
var app = builder.Build();
app.Run();
This registers:
ToonParser(singleton)ToonEncoder(singleton)ToonOptions(IOptions<ToonOptions>)ToonSerializerOptions(IOptions<ToonSerializerOptions>)
โ๏ธ Configuration
Using appsettings.json (Recommended)
appsettings.json:
{
"ToonNet": {
"ToonOptions": {
"IndentSize": 2,
"MaxDepth": 64,
"PreferInlineArrays": true,
"PreferInlineObjects": false,
"MaxInlineArrayLength": 80,
"Delimiter": ",",
"StrictMode": false,
"AllowExtendedLimits": false
},
"ToonSerializerOptions": {
"IncludeReadOnlyProperties": false,
"MaxDepth": 64,
"AllowExtendedLimits": false
}
}
}
Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Bind configuration from appsettings.json
builder.Services.AddToon(builder.Configuration);
var app = builder.Build();
app.Run();
Using Delegate Configuration
builder.Services.AddToon(toonOptions =>
{
toonOptions.IndentSize = 4;
toonOptions.PreferInlineArrays = true;
toonOptions.MaxDepth = 100;
}, serializerOptions =>
{
serializerOptions.IncludeReadOnlyProperties = false;
serializerOptions.MaxDepth = 100;
});
Hybrid Approach (Configuration + Delegate)
// Load from config + override specific values
builder.Services.AddToon(
builder.Configuration,
toonOptions =>
{
// Override specific settings
toonOptions.IndentSize = 4;
},
serializerOptions =>
{
serializerOptions.IncludeReadOnlyProperties = true;
}
);
๐ Configuration Options
ToonOptions
Controls TOON format encoding behavior:
| Option | Type | Default | Description |
|---|---|---|---|
IndentSize |
int |
2 |
Number of spaces per indentation level |
MaxDepth |
int |
64 |
Maximum nesting depth |
PreferInlineArrays |
bool |
true |
Use inline format for simple arrays |
PreferInlineObjects |
bool |
false |
Use inline format for simple objects |
MaxInlineArrayLength |
int |
80 |
Max character length for inline arrays |
Delimiter |
char |
, |
Array item delimiter |
StrictMode |
bool |
false |
Enable strict parsing rules |
AllowExtendedLimits |
bool |
false |
Allow depths beyond 64 levels |
ToonSerializerOptions
Controls C# object serialization behavior:
| Option | Type | Default | Description |
|---|---|---|---|
IncludeReadOnlyProperties |
bool |
false |
Include read-only properties |
MaxDepth |
int |
64 |
Maximum object graph depth |
AllowExtendedLimits |
bool |
false |
Allow depths beyond 64 levels |
๐ฏ Usage Patterns
Pattern 1: Inject ToonParser/Encoder
public class ToonService
{
private readonly ToonParser _parser;
private readonly ToonEncoder _encoder;
private readonly ILogger<ToonService> _logger;
public ToonService(
ToonParser parser,
ToonEncoder encoder,
ILogger<ToonService> logger)
{
_parser = parser;
_encoder = encoder;
_logger = logger;
}
public ToonDocument ParseToon(string toonString)
{
try
{
return _parser.Parse(toonString);
}
catch (ToonParseException ex)
{
_logger.LogError(ex, "Failed to parse TOON");
throw;
}
}
public string EncodeToon(ToonDocument document)
{
return _encoder.Encode(document);
}
}
// Register service
builder.Services.AddScoped<ToonService>();
Pattern 2: Inject Options
using Microsoft.Extensions.Options;
public class ConfigAnalyzer
{
private readonly ToonOptions _toonOptions;
private readonly ToonSerializerOptions _serializerOptions;
public ConfigAnalyzer(
IOptions<ToonOptions> toonOptions,
IOptions<ToonSerializerOptions> serializerOptions)
{
_toonOptions = toonOptions.Value;
_serializerOptions = serializerOptions.Value;
}
public void LogConfiguration()
{
Console.WriteLine($"Indent Size: {_toonOptions.IndentSize}");
Console.WriteLine($"Max Depth: {_toonOptions.MaxDepth}");
Console.WriteLine($"Include Read-Only: {_serializerOptions.IncludeReadOnlyProperties}");
}
}
Pattern 3: Use with ToonSerializer
using ToonNet.Core.Serialization;
using Microsoft.Extensions.Options;
public class DataService
{
private readonly ToonSerializerOptions _options;
public DataService(IOptions<ToonSerializerOptions> options)
{
_options = options.Value;
}
public string SerializeData<T>(T data)
{
// Use configured options
return ToonSerializer.Serialize(data, _options);
}
public T DeserializeData<T>(string toonString)
{
return ToonSerializer.Deserialize<T>(toonString, _options);
}
}
๐ TOON Configuration Provider
Load TOON files as ASP.NET Core configuration sources:
Create TOON Configuration File
appsettings.toon:
Database:
ConnectionString: Server=localhost;Database=mydb
Timeout: 30
EnableRetry: true
Logging:
Level: Information
Console:
Enabled: true
File:
Path: logs/app.log
MaxSize: 10485760
Features:
EnableCache: true
CacheExpiry: 3600
Register TOON Configuration Provider
var builder = WebApplication.CreateBuilder(args);
// Add TOON file as configuration source
builder.Configuration.AddToonFile("appsettings.toon", optional: false, reloadOnChange: true);
// Register ToonNet services
builder.Services.AddToon(builder.Configuration);
var app = builder.Build();
// Access configuration
var connectionString = builder.Configuration["Database:ConnectionString"];
var logLevel = builder.Configuration["Logging:Level"];
Configuration Provider Features
// Multiple TOON files
builder.Configuration
.AddToonFile("appsettings.toon")
.AddToonFile($"appsettings.{env}.toon", optional: true);
// With environment variables
builder.Configuration
.AddToonFile("config.toon")
.AddEnvironmentVariables();
// Reload on change
builder.Configuration.AddToonFile(
"settings.toon",
optional: false,
reloadOnChange: true // Auto-reload when file changes
);
โ Validation
ToonNet.AspNetCore uses Options Validation to ensure configuration is valid:
Automatic Validation
// Validation happens at startup
builder.Services.AddToon(builder.Configuration);
// If configuration is invalid, app will fail to start with clear error message
Custom Validation
builder.Services.AddToon(builder.Configuration)
.Validate(options =>
{
if (options.IndentSize < 1 || options.IndentSize > 8)
return false;
return true;
}, "IndentSize must be between 1 and 8");
Validation Rules (Built-in)
IndentSize: Must be 1-8MaxDepth: Must be 1-1024 (or 1-64 if AllowExtendedLimits=false)MaxInlineArrayLength: Must be 1-1024Delimiter: Must be a valid character
๐ Related Packages
Core:
ToonNet.Core- Core serialization (required)
Extensions:
ToonNet.Extensions.Json- JSON โ TOON conversionToonNet.Extensions.Yaml- YAML โ TOON conversion
Web Integration:
ToonNet.AspNetCore.Mvc- MVC input/output formatters
Development:
ToonNet.Demo- Sample applicationsToonNet.Tests- Test suite
๐ Documentation
- Main Documentation - Complete ToonNet guide
- API Guide - Detailed API reference
- Samples - Real-world examples
๐งช Testing
# Run ASP.NET Core integration tests
cd tests/ToonNet.Tests
dotnet test --filter "Category=AspNetCore"
๐ Requirements
- .NET 8.0 or later
- ASP.NET Core 8.0+
- ToonNet.Core
๐ 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
- ToonNet.Core (>= 1.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ToonNet.AspNetCore:
| Package | Downloads |
|---|---|
|
ToonNet.AspNetCore.Mvc
ASP.NET Core MVC integration for ToonNet. Provides input/output formatters for TOON format support in MVC controllers and Web APIs. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial 1.0.0 release with DI extensions.