Bodu.Text.Bencode.Nodes Namespace
- Package
-
Bodu.Text.Bencode 1.0.0
Purpose
Bodu.Text.Bencode.Nodes is the mutable document object model of Bodu.Text.Bencode, shaped after System.Text.Json.Nodes. Parse bytes into an editable tree, build one from scratch, change it, and write it back through the Utf8BencodeWriter. For read-only inspection with fewer allocations, use the sibling Bodu.Text.Bencode.Document tier; for typed binding, use the BencodeSerializer.
Key types
- BencodeNode - abstract base:
Parse,AsObject/AsArray/AsValue,GetValue<T>,GetValueKind,WriteTo,ToByteArray,DeepEquals, and the implicit conversions fromstring, integers, andbyte[]. - BencodeObject - a dictionary node keyed by string, with
Add/Remove/TryGetValueand an indexer. - BencodeArray - a list node implementing
IList<BencodeNode?>. - BencodeValue - an integer or byte-string leaf, created via
Createand read viaGetValue<T>/TryGetValue<T>. - BencodeNodeOptions - parsing options for
Parse.
Example
using Bodu.Text.Bencode.Nodes;
BencodeObject root = BencodeNode.Parse("d6:lengthi1024e4:name10:ubuntu.isoe"u8)!.AsObject();
root["length"] = 2048; // implicit conversion from long
root.Add("comment", "mirror copy"); // implicit conversion from string
byte[] back = root.ToByteArray(); // keys re-emitted in canonical order
Notes
- Canonical on write. Insertion order is kept in memory, but
WriteTo/ToByteArrayalways emit dictionary keys in bytewise order. - Parent tracking. A node belongs to at most one parent; adding an attached node elsewhere throws InvalidOperationException.
- See also: the Bodu.Text.Bencode introduction and the Using Bencode guide (Pattern 7 - Use a document model instead of a type).
Classes
- BencodeArray
Represents a mutable Bencode (BEP 3) list as an ordered, index-addressable collection of child nodes.
- BencodeNode
Represents a single node in a mutable Bencode (BEP 3) document object model, serving as the base for the three concrete node kinds - BencodeObject, BencodeArray, and BencodeValue.
- BencodeObject
Represents a mutable Bencode (BEP 3) dictionary as a string-keyed collection of child nodes.
- BencodeValue
Represents a scalar Bencode (BEP 3) value - either an integer or a byte string - within a node tree.
Structs
- BencodeNodeOptions
Defines the customizations applied to a mutable Bencode (BEP 3) node tree.