SolidCode.Extensions.Configuration.Yaml 0.9.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package SolidCode.Extensions.Configuration.Yaml --version 0.9.1                
NuGet\Install-Package SolidCode.Extensions.Configuration.Yaml -Version 0.9.1                
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="SolidCode.Extensions.Configuration.Yaml" Version="0.9.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SolidCode.Extensions.Configuration.Yaml --version 0.9.1                
#r "nuget: SolidCode.Extensions.Configuration.Yaml, 0.9.1"                
#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.
// Install SolidCode.Extensions.Configuration.Yaml as a Cake Addin
#addin nuget:?package=SolidCode.Extensions.Configuration.Yaml&version=0.9.1

// Install SolidCode.Extensions.Configuration.Yaml as a Cake Tool
#tool nuget:?package=SolidCode.Extensions.Configuration.Yaml&version=0.9.1                

SolidCode.Extensions.Configuration.Yaml

SolidCode.Extensions.Configuration.Yaml is a library that provides YAML configuration support for Microsoft.Extensions.Configuration.

YAML is often used for configuration files due to its human-readable format. But often not all its features are required for describing configuration settings. For example: tags, including JSON inside YAML, document stream, etc.

This library aims to achieve performance and low memory consumption by supporting YAMLite - a subset of YAML features most used in configuration files.

Currently supported features:

  • Mappings
  • Sequences
  • Scalars (including multi-line strings)
  • Comments

Considering to be supported in the future:

  • Flow style for sequences

Not supported features:

  • Tags
  • Anchors
  • Aliases
  • Document stream
  • Full flow style support (e.g. JSON inside YAML)

Installation

You can install the SolidCode.Extensions.Configuration.Yaml library using the following command:

dotnet add package SolidCode.Extensions.Configuration.Yaml 

Or via NuGet Package Manager in Visual Studio:

PM> Install-Package SolidCode.Extensions.Configuration.Yaml

Usage

To use the SolidCode.Extensions.Configuration.Yaml library, follow these steps:

  1. After installation, the library will be referenced in your project.

  2. Create a YAML configuration file in your project, for example appsettings.yaml, and define your configuration settings in YAML format.

  3. In your code, call the AddYamlFile() method to add a YAML configuration file into the application configuration alongside with other configuration sources. For example:

IHostBuilder appBuilder = Host.CreateDefaultBuilder()
	.ConfigureAppConfiguration((hostingContext, config) => {
		config.AddYamlFile("appsettings.yaml", reloadOnChange: true, configurationOptions: options);
	});

IHost host = appBuilder.Build();
  1. Access your configuration settings using the IConfiguration interface and/or options patterns as usual.

Documentation

AddYamlFile() method

To add a configuration file to your application configuration, please use the AddYamlFile() method:

AddYamlFile(this IConfigurationBuilder builder, string path, bool optional = false, bool reloadOnChange = true, YamlConfigurationOptions? configurationOptions = null)

Parameters:

  • path - the path to the YAML configuration file. The path can be relative to the application root directory or an absolute path.
  • optional - whether the file is optional.
  • reloadOnChange - whether the configuration should be reloaded if the YAML file changes.
  • configurationOptions - allows to specify options for parsing the YAML configuration file. Options represented by the YamlConfigurationOptions class described below.

YamlConfigurationOptions class

This class has following properties:

  • BufferSize - specifies the size of the buffer (in characters) used to read the YAML file. The default value is equal to the configuration file size.
  • EndOfLineType - specifies the type of end-of-line character(s) used in the configuration values. The default value is the default end-of-line character(s) for the current operating system.
  • EndingLineBreaks - defines how to handle the final line break in the multiline string. Options are:
    • keep as is
    • remove any ending line breaks
    • normalize (default value): keep only 1 line break regardless of how many line breaks were in the YAML file.

Exceptions

  • YamlConfigurationException - the base exception for all exceptions thrown by the library.
    • YamlConfigurationReadingException - thrown if there is an error while reading the YAML configuration.
    • YamlConfigurationParsingException - thrown if there is an error while parsing the YAML configuration.

Benchmarks

Here is example of benchmark results for parsing a YAML files (see SolidCode.Extensions.Configuration.Yaml.Benchmark project source code for YAML files):

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3593/23H2/2023Update/SunValley3)
11th Gen Intel Core i9-11900H 2.50GHz, 1 CPU, 16 logical and 8 physical cores

| Method                                              | Mean      | Error     | StdDev   | Ratio | Gen0    | Gen1   | Allocated | Alloc Ratio |
|---------------------------------------------------- |----------:|----------:|---------:|------:|--------:|-------:|----------:|------------:|
| 'YamlConfigurationParser. File: example_large.yaml' |  35.14 us | 0.695 us  | 0.854 us |  0.07 |  5.7983 | 0.8545 |  71.32 KB |        0.19 |
| 'YamlDotNet-based parser. File: example_large.yaml' | 479.12 us | 9.365 us  | 8.760 us |  1.00 | 30.7617 | 9.7656 | 378.55 KB |        1.00 |

| Method                                              | Mean      | Error     | StdDev    | Ratio | Gen0   | Gen1   | Allocated | Alloc Ratio |
|---------------------------------------------------- |----------:|----------:|----------:|------:|-------:|-------:|----------:|------------:|
| 'YamlConfigurationParser. File: example_small.yaml' |  7.885 us | 0.1552 us | 0.2225 us |  0.10 | 1.0834 | 0.0305 |   13.3 KB |        0.21 |
| 'YamlDotNet-based parser. File: example_small.yaml' | 77.751 us | 1.5481 us | 1.7207 us |  1.00 | 5.0049 | 0.3662 |  62.14 KB |        1.00 |

YamlDotNet library version: 15.1.4

License

This library is licensed under the MIT License.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.10.0 33 11/15/2024
0.9.3 119 8/9/2024
0.9.2 112 5/28/2024
0.9.1 106 5/26/2024
0.9.0 114 5/25/2024
0.8.0 106 5/25/2024