Table of Contents

Bodu.Text.Yaml.Document Namespace

Package

Bodu.Text.Yaml.Document

Purpose

Bodu.Text.Yaml.Document is the read-only document object model of Bodu.Text.Yaml, shaped after System.Text.Json's JsonDocument / JsonElement. A parse produces a disposable YamlDocument over a resolved node store; every YamlElement is a lightweight struct cursor into it. It is also the home of multi-document stream parsing (ParseAllDocuments). Prefer it over the mutable Bodu.Text.Yaml.Nodes tier for inspection; a member typed YamlElement binds through the YamlSerializer's DOM bridge.

Key types

  • YamlDocument - the disposable owner: Parse (from string or ReadOnlySpan<byte>), ParseAllDocuments for --- / ... delimited streams, and RootElement.
  • YamlElement - the value cursor: ValueKind, ScalarStyle (the original YamlScalarStyle), GetString / GetInt64 / GetDouble / GetBoolean, GetProperty / TryGetProperty, an integer indexer, GetSequenceLength, EnumerateSequence / EnumerateMapping, and WriteTo.
  • YamlElement.SequenceEnumerator / YamlElement.MappingEnumerator - the struct enumerators returned by EnumerateSequence / EnumerateMapping.
  • YamlProperty - a Name / Value pair yielded by EnumerateMapping.
  • YamlDocumentOptions - SpecVersion, DuplicateKeyBehavior, MergeKeyBehavior, and MaxDepth.

Example

using Bodu.Text.Yaml.Document;

using YamlDocument document = YamlDocument.Parse("host: localhost\nport: 8080\n");
YamlElement root = document.RootElement;

long port = root.GetProperty("port").GetInt64();
foreach (YamlProperty property in root.EnumerateMapping())
    Console.WriteLine($"{property.Name}: {property.Value.ValueKind}");

Notes

  • Aliases are transparent. An element reached through an alias resolves to its anchored target; the tree is guaranteed acyclic.
  • Multi-document streams. The single-document Parse reads the first document; ParseAllDocuments returns every document, each independently disposable.
  • See also: the Bodu.Text.Yaml introduction and the Using YAML guide (Pattern 5 - Inspect a document with the read-only DOM).

Classes

YamlDocument

Represents a parsed, read-only YAML document as an immutable tree of YamlElement views over a flat, index-linked node store, in the manner of System.Text.Json.JsonDocument.

Structs

YamlDocumentOptions

Provides options that configure how a YamlDocument parses YAML source.

YamlElement

Represents a single node within a YamlDocument as a lightweight, read-only view, in the manner of System.Text.Json.JsonElement.

YamlElement.MappingEnumerator

Provides forward-only enumeration over the key/value pairs of a mapping node.

YamlElement.SequenceEnumerator

Provides forward-only enumeration over the elements of a sequence node.

YamlProperty

Represents a single key/value pair within a mapping node of a YamlDocument.