YamlObject class
Represents a mutable YAML object.
Namespace: System.Text.Yaml.Nodes · Assembly: Styml.dll
public sealed class YamlObject : YamlNode, IDictionary<string, YamlNode>, IList<KeyValuePair<string, YamlNode>>
Inheritance: object → YamlNode → YamlObject
Implements: IDictionary<string, YamlNode>, IList<KeyValuePair<string, YamlNode>>
Represents a mutable YAML object.
RemarksLink to this heading
It's safe to perform multiple concurrent read operations on a YamlObject, but issues can occur if the collection is modified while it's being read.
ConstructorsLink to this heading
Initializes a new instance of the YamlObject class that is empty.
public YamlObject(YamlNodeOptions? options = null);
Parameters
options: Options to control the behavior.
Initializes a new instance of the YamlObject class that contains the specified properties.
public YamlObject(IEnumerable<KeyValuePair<string, YamlNode?>> properties, YamlNodeOptions? options = null);
Parameters
properties: The properties to be added.options: Options to control the behavior.
PropertiesLink to this heading
CollectionKeysLink to this heading
Gets the entries whose key is a collection (? [a, b], ? {x: 1}), in their order among this object's entries. The string-keyed members (the indexer, YamlObject.Count, enumeration, YamlObject.ContainsKey) do not see them; YamlNode.DeepEquals, YamlNode.DeepClone and writing do.
public IReadOnlyList<KeyValuePair<YamlNode, YamlNode?>> CollectionKeys { get; }
A parsed object keeps its source order across string keys and collection keys when written back. An entry added by YamlObject.AddCollectionKey goes after every existing entry, and a string key added later goes after it; each entry stays between the same string entries when one is inserted or removed. YamlDotNet's YamlMappingNode keys its children by node, so its count includes these entries.
An entry's key and value have this object as their parent. Their YamlNode.GetPath is …[?n] for the value and …[?n:key] for the key, where n is the entry's index in this list, and YamlNode.ReplaceWith replaces them in the entry (a key under the rules of YamlObject.AddCollectionKey). They have no property name.
Keys are checked for duplicates when an entry is added. A key changed afterwards (a node added to it, say) is the caller's to keep distinct: two deeply equal keys are written as they are, and reading them back is refused under the default YamlDocumentOptions.DuplicateKeyHandling.
CollectionStyleLink to this heading
Gets or sets the style this collection is written in by a block-style writer: YamlCollectionStyle.Flow for {key: value}, YamlCollectionStyle.Block otherwise. A node parsed from YAML or JSON has its source's style (JSON's collections are flow); a new node is block. Inside a flow collection every collection is flow, and a JSON-compatible writer writes flow whatever is set. A source's style is not written under YamlWriterOptions.IgnoreSourceStyles; a style set here is.
public YamlCollectionStyle CollectionStyle { get; set; }
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | The value is not a collection style. |
CountLink to this heading
Gets the number of elements contained in YamlObject.
public int Count { get; }
MethodsLink to this heading
AddLink to this heading
Adds an element with the provided property name and value to the YamlObject.
public void Add(string propertyName, YamlNode? value);
Parameters
propertyName: The property name of the element to add.value: The value of the element to add.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyNameis null. |
| ArgumentException | An element with the same property name already exists in the YamlObject. |
Adds the specified property to the YamlObject.
public void Add(KeyValuePair<string, YamlNode?> property);
Parameters
property: The KeyValuePair structure representing the property name and value to add to theYamlObject.
| Exception | Condition |
|---|---|
| ArgumentException | An element with the same property name already exists in the YamlObject. |
| ArgumentNullException | The property name of property is null. |
AddCollectionKeyLink to this heading
Adds an entry whose key is a collection, after every existing entry. Writing it needs a block-style writer.
public void AddCollectionKey(YamlNode key, YamlNode? value);
Parameters
key: The key: aYamlObjector aYamlArray.value: The value.
| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
| ArgumentException | key is not a collection (a scalar key is a string key: use the indexer or YamlObject.Add), or an entry with a deeply equal key exists. |
| InvalidOperationException | key or value already has a parent. |
The duplicate check is made here, against the keys as they are now. A key changed after it was added is the caller's to keep distinct from the others (see YamlObject.CollectionKeys).
ClearLink to this heading
Removes all elements from the YamlObject.
public void Clear();
ContainsKeyLink to this heading
Determines whether the YamlObject contains an element with the specified property name.
public bool ContainsKey(string propertyName);
Parameters
propertyName: The property name to locate in theYamlObject.
Returns: true if the YamlObject contains an element with the specified property name; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
CreateLink to this heading
Initializes a new instance of the YamlObject class that contains properties from the specified YamlElement.
public static YamlObject? Create(YamlElement element, YamlNodeOptions? options = null);
Parameters
element: TheYamlElement.options: Options to control the behavior.
Returns: The new instance of the YamlObject class that contains properties from the specified YamlElement.
GetAtLink to this heading
Gets the property the specified index.
public KeyValuePair<string, YamlNode?> GetAt(int index);
Parameters
index: The zero-based index of the pair to get.
Returns: The property at the specified index as a key/value pair.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than 0 or greater than or equal to YamlObject.Count. |
GetEnumeratorLink to this heading
Returns an enumerator that iterates through the YamlObject.
public IEnumerator<KeyValuePair<string, YamlNode?>> GetEnumerator();
Returns: An enumerator that iterates through the YamlObject.
IndexOfLink to this heading
Determines the index of a specific property name in the object.
public int IndexOf(string propertyName);
Parameters
propertyName: The property name to locate.
Returns: The index of propertyName if found; otherwise, -1.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
InsertLink to this heading
Inserts a property into the object at the specified index.
public void Insert(int index, string propertyName, YamlNode? value);
Parameters
index: The zero-based index at which the property should be inserted.propertyName: The property name to insert.value: The YAML value to insert.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
| ArgumentException | An element with the same key already exists in the YamlObject. |
| ArgumentOutOfRangeException | index is less than 0 or greater than YamlObject.Count. |
RemoveLink to this heading
Removes the element with the specified property name from the YamlObject.
public bool Remove(string propertyName);
Parameters
propertyName: The property name of the element to remove.
Returns: true if the element is successfully removed; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
RemoveAtLink to this heading
Removes the property at the specified index.
public void RemoveAt(int index);
Parameters
index: The zero-based index of the item to remove.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than 0 or greater than or equal to YamlObject.Count. |
RemoveCollectionKeyLink to this heading
Removes the entry whose collection key is deeply equal to key.
public bool RemoveCollectionKey(YamlNode key);
Parameters
key: The key to find.
Returns: true if an entry was removed.
| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
SetAtLink to this heading
Sets a new property at the specified index.
public void SetAt(int index, string propertyName, YamlNode? value);
Parameters
index: The zero-based index of the property to set.propertyName: The property name to store at the specified index.value: The YAML value to store at the specified index.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than 0 or greater than or equal to YamlObject.Count. |
| ArgumentException | propertyName is already specified in a different index. |
| InvalidOperationException | value already has a parent. |
Sets a new property value at the specified index.
public void SetAt(int index, YamlNode? value);
Parameters
index: The zero-based index of the property to set.value: The YAML value to store at the specified index.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than 0 or greater than or equal to YamlObject.Count. |
| InvalidOperationException | value already has a parent. |
TryAddLink to this heading
Adds an element with the provided name and value to the YamlObject, if a property named propertyName doesn't already exist.
public bool TryAdd(string propertyName, YamlNode? value);
Parameters
propertyName: The property name of the element to add.value: The value of the element to add.
Returns: true if the property didn't exist and the element was added; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
Adds an element with the provided name and value to the YamlObject, if a property named propertyName doesn't already exist.
public bool TryAdd(string propertyName, YamlNode? value, out int index);
Parameters
propertyName: The property name of the element to add.value: The value of the element to add.index: The index of the added or existingpropertyName. This is always a valid index into theYamlObject.
Returns: true if the property didn't exist and the element was added; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
TryGetPropertyValueLink to this heading
Returns the value of a property with the specified name.
public bool TryGetPropertyValue(string propertyName, out YamlNode? yamlNode);
Parameters
propertyName: The name of the property to return.yamlNode: The YAML value of the property with the specified name.
Returns: true if a property with the specified name was found; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
Gets the value associated with the specified property name.
public bool TryGetPropertyValue(string propertyName, out YamlNode? yamlNode, out int index);
Parameters
propertyName: The property name of the value to get.yamlNode: When this method returns, it contains the value associated with the specified property name, if the property name is found; otherwisenull.index: The index ofpropertyNameif found; otherwise, -1.
Returns: true if the YamlObject contains an element with the specified property name; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | propertyName is null. |
WriteToLink to this heading
Write the YamlNode into the provided Utf8YamlWriter as YAML.
public override void WriteTo(Utf8YamlWriter writer, YamlSerializerOptions? options = null);
Parameters
writer: TheUtf8YamlWriter.options: Options to control the serialization behavior.
| Exception | Condition |
|---|---|
| ArgumentNullException | The writer parameter is null. |
A block-style writer keeps the source's styles (each collection's flow or block style, each scalar's and key's quoting or block style) where it can hold the text in them, unless YamlWriterOptions.IgnoreSourceStyles is set, which writes in the writer's own styles.