Utf8YamlReader Struct
Definition
Provides a forward-only, read-only token cursor over a parsed YAML document, surfacing its nodes as structural and scalar tokens in document order. The token surface mirrors Utf8JsonReader, but the underlying reader is buffered rather than a single-pass streaming scanner.
public ref struct Utf8YamlReader
- Inherited Members
Remarks
Unlike Utf8JsonReader, this type is not a low-allocation single-pass streaming
reader. YAML cannot be tokenized in a single forward pass because indentation context, back-referencing aliases,
merge keys, and document boundaries all require look-ahead and composition. The constructor therefore copies the
source and fully parses it into an in-memory node store; Read() then walks that store in document
order. In that respect the reader is the analogue of the sibling TomlDocumentReader cursor rather than the
streaming Utf8TomlReader scanner.
The reader honors the library's YAML 1.2 core, JSON-compatible tree profile: mapping keys must resolve to scalar strings, mapping keys must be unique, anchors must be unique and acyclic, and tabs are not permitted as indentation. Inputs that violate the profile are rejected with YamlFormatException.
The reader is a ref struct and cannot be boxed, stored on the heap, or captured by a lambda.
Constructors
Utf8YamlReader(ReadOnlySpan<byte>)
Initializes a new instance of the Utf8YamlReader struct over UTF-8 source.
public Utf8YamlReader(ReadOnlySpan<byte> utf8Yaml)
Parameters
utf8YamlReadOnlySpan<byte>The UTF-8 encoded YAML source.
Exceptions
- YamlFormatException
The source is not valid YAML.
Utf8YamlReader(ReadOnlySpan<byte>, YamlReaderOptions)
Initializes a new instance of the Utf8YamlReader struct over UTF-8 source with options.
public Utf8YamlReader(ReadOnlySpan<byte> utf8Yaml, YamlReaderOptions options)
Parameters
utf8YamlReadOnlySpan<byte>The UTF-8 encoded YAML source.
optionsYamlReaderOptionsThe reader options.
Exceptions
- YamlFormatException
The source is not valid YAML.
Properties
CurrentDepth
Gets the nesting depth of the current token, with the root at zero.
public readonly int CurrentDepth { get; }
Property Value
- int
The current container depth.
TokenType
Gets the type of the current token.
public readonly YamlTokenType TokenType { get; }
Property Value
- YamlTokenType
The current token type, or None before the first read.
Methods
GetBoolean()
Returns the boolean value of the current boolean scalar token.
public readonly bool GetBoolean()
Returns
- bool
The boolean value.
Exceptions
- InvalidOperationException
The current token is not a boolean scalar.
GetDouble()
Returns the floating-point value of the current numeric scalar token.
public readonly double GetDouble()
Returns
- double
The double-precision value.
Exceptions
- InvalidOperationException
The current token is not a numeric scalar.
GetInt64()
Returns the integer value of the current integer scalar token.
public readonly long GetInt64()
Returns
- long
The 64-bit integer value.
Exceptions
- InvalidOperationException
The current token is not an integer scalar.
GetString()
Returns the string value of the current property name or string scalar token.
public readonly string GetString()
Returns
- string
The decoded string.
Exceptions
- InvalidOperationException
The current token is not a property name or string scalar.
Read()
Advances the reader to the next token.
public bool Read()
Returns
Skip()
Advances the reader past the value at its current position without materializing it, so an unbound member's value is consumed and the enclosing loop resumes on the next token.
public void Skip()
Remarks
On a scalar token this is a no-op - the scalar is already fully consumed and the caller's next Read() moves past it. On a container start token the reader advances to the matching end token, balancing nested containers along the way.
ValueTextEquals(ReadOnlySpan<byte>)
Determines whether the current property name equals the given UTF-8 text.
public readonly bool ValueTextEquals(ReadOnlySpan<byte> utf8Text)
Parameters
utf8TextReadOnlySpan<byte>The UTF-8 text to compare against.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |