Skip to content

YamlReaderOptions

Definition

Namespace: Corvus.Text.Json.Yaml
Assembly: Corvus.Text.Json.dll

Options for configuring the YAML to JSON converter.

public readonly struct YamlReaderOptions

Constructors

Constructor Description
YamlReaderOptions() Initializes a new instance of the YamlReaderOptions struct with default values.

Properties

Property Type Description
DocumentMode YamlDocumentMode Gets the document mode specifying how multi-document streams are handled. Defaults to SingleRequired.
DuplicateKeyBehavior DuplicateKeyBehavior Gets the behavior when duplicate mapping keys are encountered. Defaults to Error.
MaxAliasExpansionDepth int Gets the maximum depth for alias expansion to prevent exponential expansion attacks (billion laughs). Defaults to 64.
MaxAliasExpansionSize int Gets the maximum total number of nodes that can be produced by alias expansion. Defaults to 1,000,000.
Schema YamlSchema Gets the YAML schema to use for tag resolution and scalar type coercion. Defaults to Core.

Fields

Field Type Description
Default static YamlReaderOptions Gets the default options: Core schema, single-document required, error on duplicate keys, max alias expansion depth of 64, max alias expansion size of 1,000,000 nodes.

Examples

Configure YAML parsing behaviour with YamlReaderOptions. The default options use the Core schema, single-document mode, and error on duplicate keys.

using Corvus.Text.Json;
using Corvus.Text.Json.Yaml;

// Default options — Core schema, single document, duplicate keys error
using ParsedJsonDocument<JsonElement> doc = YamlDocument.Parse<JsonElement>(yaml);

Custom options

var options = new YamlReaderOptions
{
    Schema = YamlSchema.Json,
    DocumentMode = YamlDocumentMode.MultiAsArray,
    DuplicateKeyBehavior = DuplicateKeyBehavior.LastWins,
};

using ParsedJsonDocument<JsonElement> doc = YamlDocument.Parse<JsonElement>(yaml, options);

Schema modes

Schema Behaviour
YamlSchema.Core Default. Resolves true/false/null and numeric scalars.
YamlSchema.Json Like Core, but untagged scalars that aren't null/bool/number become strings.
YamlSchema.Failsafe All scalars are strings — no type resolution.
YamlSchema.Yaml11 YAML 1.1 rules — yes/no/on/off are booleans, octal 0777, etc.

Alias expansion limits

Control anchor/alias expansion depth and total size to prevent denial-of-service from deeply nested or exponentially expanding aliases:

var options = new YamlReaderOptions
{
    MaxAliasExpansionDepth = 10,
    MaxAliasExpansionSize = 1_000_000,
};

Applies To

Product Versions
.NET 9, 10
.NET Standard 2.0, 2.1

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests.

Open an issue