Bodu.Text.Yaml.Nodes Namespace
- Package
-
Bodu.Text.Yaml 1.3.0
Purpose
Bodu.Text.Yaml.Nodes is the mutable document object model of Bodu.Text.Yaml, shaped after System.Text.Json.Nodes. Parse YAML text into an editable tree, build one from scratch, change it, and write it back through the Utf8YamlWriter. For read-only inspection with fewer allocations, use the sibling Bodu.Text.Yaml.Document tier; for typed binding, use the YamlSerializer. A member typed YamlNode binds through the serializer's DOM bridge.
Key types
- YamlNode - abstract base:
Parse,AsObject/AsArray/AsValue, string and integer indexers,WriteTo, andToYamlString. - YamlObject - a mapping node keyed by string, with
Add/Remove/TryGetValueand an indexer. - YamlArray - a sequence node with
Add/Remove/RemoveAtand an integer indexer. - YamlValue - a scalar leaf (string, integer, float, or boolean), created via
Createand read viaGetValue<T>.
Example
using Bodu.Text.Yaml.Nodes;
YamlObject root = YamlNode.Parse("host: localhost\nport: 8080\n")!.AsObject();
root["port"] = YamlValue.Create(9090);
root.Add("tls", YamlValue.Create(true));
string yaml = root.ToYamlString();
Notes
- Explicit scalar creation. Unlike the TOML and Bencode node DOMs there are no implicit conversions; wrap scalars with
YamlValue.Createso the value kind is unambiguous under YAML's implicit typing. - A node appears at most once. Adding, inserting, or assigning a node that already belongs to a container, the same one included, throws InvalidOperationException, as does adding a container to itself or to one of its own descendants; removing or replacing a node detaches it so that it can be added elsewhere.
- Presentation is normalized. Comments, anchors, aliases, and the original scalar styles are not retained;
ToYamlStringre-emits the tree in block style. - See also: the Bodu.Text.Yaml introduction and the Using YAML guide (Pattern 4 - Edit a document with the mutable DOM).
Classes
- YamlNode
Represents a node in a mutable YAML document object model, in the manner of System.Text.Json.Nodes.JsonNode.
- YamlObject
Represents a mutable YAML mapping node whose entries preserve insertion order.
- YamlValue
Represents a scalar node in a mutable YAML document object model.