RStein.TOML
0.0.98
.NET 9.0
This package targets .NET 9.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.8
This package targets .NET Framework 4.8. The package is compatible with this framework or higher.
dotnet add package RStein.TOML --version 0.0.98
NuGet\Install-Package RStein.TOML -Version 0.0.98
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RStein.TOML" Version="0.0.98" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RStein.TOML" Version="0.0.98" />
<PackageReference Include="RStein.TOML" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RStein.TOML --version 0.0.98
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RStein.TOML, 0.0.98"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RStein.TOML@0.0.98
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RStein.TOML&version=0.0.98
#tool nuget:?package=RStein.TOML&version=0.0.98
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RStein.TOML
A simple asynchronous .NET library for parsing and generating TOML (Tom's Obvious Minimal Language) files.
- ✅ Supports .NET 10, .NET 9, .NET Framework 4.8, .NET Standard 2.0.
- ✅ Tested against TOML platform agnostic test suite (TOML 1.0.0 and 1.1.0 compliant).
Features
- ✅ Support for TOML 1.1 and TOML 1.0.
- ✅ Support for all TOML data types.
- ✅ Memory-efficient low-allocation parser.
- ✅ Asynchronous API with cancellation token.
- ✅ Nullable-aware library with proper annotations.
Installation
- NuGet Package
dotnet add package RStein.TOML
Quick Start
Deserializing TOML
From a String
using RStein.TOML;
string tomlContent = """
[package]
name = "MyApp"
version = "1.0.0"
""";
using var cts = new CancellationTokenSource();
TomlTable table = await TomlSerializer.DeserializeAsync(tomlContent, cts.Token);
From a Stream
using var stream = File.Open("config.toml", FileMode.Open, FileAccess.Read);
TomlTable table = await TomlSerializer.DeserializeAsync(stream);
Serializing TOML
To a String
var table = new TomlTable();
table.Add("name", "MyApp");
table.Add("version", "1.0.0");
string tomlOutput = await TomlSerializer.SerializeToStringAsync(table);
Console.WriteLine(tomlOutput);
// Output:
// name = "MyApp"
// version = "1.0.0"
To a Stream
var rootTable = new TomlTable();
// ... populate table ...
using var stream = File.Open("output.toml", FileMode.Create, FileAccess.Write);
await TomlSerializer.SerializeAsync(rootTable, stream);
Working with TOML Data
Accessing Values
TomlTable table = await TomlSerializer.DeserializeAsync(tomlContent);
// Access nested values
var packageNode = table["package"];
var nameNode = packageNode["name"];
// Cast to specific types using explicit cast operators
string name = (string?)nameNode ?? "unknown";
int count = ((int?)table["count"]) ?? 0;
bool enabled = ((bool?)table["enabled"]) ?? false;
Creating TOML Documents
var config = new TomlTable();
// Add simple key-value pairs
config.Add("title", "TOML Example");
config.Add("version", "1.0.0");
config.Add("count", 42);
config.Add("enabled", true);
// Add nested tables
var owner = new TomlTable("owner");
owner.Add("name", "Tom Preston-Werner");
owner.Add("email", "tom@example.com");
config.Add(owner.FullName, owner);
string output = await TomlSerializer.SerializeToStringAsync(config);
Console.WriteLine(output);
// Output:
// title = "TOML Example"
// version = "1.0.0"
// count = 42
// enabled = true
//
// [owner]
// name = "Tom Preston-Werner"
// email = "tom@example.com"
Example: Configuration File Handling
// appsettings.toml
string configContent = """
[app]
name = "MyApp"
[logging]
level = "info"
[server]
port = 8000
hosts = ["localhost", "127.0.0.1", "example.com"]
""";
// Read configuration
string configText = await File.ReadAllTextAsync("appsettings.toml");
TomlTable config = await TomlSerializer.DeserializeAsync(configText);
// Extract and cast values to specific types
var appNameNode = config["app"]["name"];
var logLevelNode = config["logging"]["level"];
var portNode = config["server"]["port"];
var hostsNode = config["server"]["hosts"];
// Use explicit cast operators to convert to int, string, etc.
string appName = (string?)appNameNode ?? "DefaultApp";
int port = ((int?)portNode) ?? 8000;
Console.WriteLine($"App: {appName}");
Console.WriteLine($"Port: {port}");
// Access array elements
if (hostsNode is TomlArray hostsArray)
{
Console.WriteLine("Hosts:");
foreach (var host in hostsArray)
{
Console.WriteLine($" - {(string?)host}");
}
}
// Modify and save
config["server"]["port"] = 8080;
string updatedConfig = await TomlSerializer.SerializeToStringAsync(config);
await File.WriteAllTextAsync("appsettings.toml", updatedConfig);
// Updated appsettings.toml:
// [app]
// name = "MyApp"
//
// [logging]
// level = "info"
//
// [server]
// port = 8080
// hosts = ["localhost", "127.0.0.1", "example.com"]
Public API Doc
License
The software is released under the MIT license. See LICENSE.md file for details.
Contributing
Contributions are welcome! Please feel free to submit a pull request.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 is compatible. 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. net11.0 is compatible. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.8
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
net10.0
- No dependencies.
-
net11.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.