dyaml.node

Node of a YAML document. Used to read YAML data once it's loaded, and to prepare data to emit.

class NodeException: dyaml.exception.YAMLException;

Exception thrown at node related errors.

struct YAMLNull;

Null YAML type. Used in nodes with null values.

const pure nothrow @safe string toString();

Used for string conversion.

struct Node;

YAML node.

This is a pseudo-dynamic type that can store any YAML value, including a sequence or mapping of nodes. You can get data from a Node directly or iterate over it if it's a collection.

struct Pair;

Key-value pair of YAML nodes, used in mappings.

Node key;

Key node.

Node value;

Value node.

@safe this(K, V)(K key, V value);

Construct a Pair from two values. Will be converted to Nodes if needed.

const @safe bool opEquals(ref const Pair rhs);

Equality test with another Pair.

nothrow @safe void opAssign(Pair rhs);

Assignment (shallow copy) by value.

nothrow @safe void opAssign(ref Pair rhs);

Assignment (shallow copy) by reference.

@trusted this(T)(T value, const string tag = null) if (isSomeString!T || !isArray!T && !isAssociativeArray!T);

Construct a Node from a value.

Any type except for Node can be stored in a Node, but default YAML types (integers, floats, strings, timestamps, etc.) will be stored more efficiently. To create a node representing a null value, construct it from YAMLNull.

Note that to emit any non-default types you store in a node, you need a Representer to represent them in YAML - otherwise emitting will fail.

Parameters:
T value Value to store in the node.
string tag Overrides tag of the node when emitted, regardless of tag determined by Representer. Representer uses this to determine YAML data type when a D data type maps to multiple different YAML data types. Tag must be in full form, e.g. "tag:yaml.org,2002:int", not a shortcut, like "!!int".
@safe this(T)(T[] array, const string tag = null) if (!isSomeString!(T[]));

Construct a node from an array.

If array is an array of nodes or pairs, it is stored directly. Otherwise, every value in the array is converted to a node, and those nodes are stored.

Parameters:
T[] array Values to store in the node.
string tag Overrides tag of the node when emitted, regardless of tag determined by Representer. Representer uses this to determine YAML data type when a D data type maps to multiple different YAML data types. This is used to differentiate between YAML sequences ("!!seq") and sets ("!!set"), which both are internally represented as an array_ of nodes. Tag must be in full form, e.g. "tag:yaml.org,2002:set", not a shortcut, like "!!set".
Examples:
//Will be emitted as a sequence (default for arrays)
auto seq = Node([1, 2, 3, 4, 5]);
//Will be emitted as a set (overriden tag)
auto set = Node([1, 2, 3, 4, 5], "tag:yaml.org,2002:set");
@safe this(K, V)(V[K] array, const string tag = null);

Construct a node from an associative array.

If keys and/or values of array are nodes, they stored directly. Otherwise they are converted to nodes and then stored.

Parameters:
V[K] array Values to store in the node.
string tag Overrides tag of the node when emitted, regardless of tag determined by Representer. Representer uses this to determine YAML data type when a D data type maps to multiple different YAML data types. This is used to differentiate between YAML unordered mappings ("!!map"), ordered mappings ("!!omap"), and pairs ("!!pairs") which are all internally represented as an array of node pairs. Tag must be in full form, e.g. "tag:yaml.org,2002:omap", not a shortcut, like "!!omap".
Examples:
//Will be emitted as an unordered mapping (default for mappings)
auto map   = Node([1 : "a", 2 : "b"]);
//Will be emitted as an ordered map (overriden tag)
auto omap  = Node([1 : "a", 2 : "b"], "tag:yaml.org,2002:omap");
//Will be emitted as pairs (overriden tag)
auto pairs = Node([1 : "a", 2 : "b"], "tag:yaml.org,2002:pairs");
@safe this(K, V)(K[] keys, V[] values, const string tag = null) if (!(isSomeString!(K[]) || isSomeString!(V[])));

Construct a node from arrays of keys and values.

Constructs a mapping node with key-value pairs from keys and values, keeping their order. Useful when order is important (ordered maps, pairs).

keys and values must have equal length.

If keys and/or values are nodes, they are stored directly/ Otherwise they are converted to nodes and then stored.

Parameters:
K[] keys Keys of the mapping, from first to last pair.
V[] values Values of the mapping, from first to last pair.
string tag Overrides tag of the node when emitted, regardless of tag determined by Representer. Representer uses this to determine YAML data type when a D data type maps to multiple different YAML data types. This is used to differentiate between YAML unordered mappings ("!!map"), ordered mappings ("!!omap"), and pairs ("!!pairs") which are all internally represented as an array of node pairs. Tag must be in full form, e.g. "tag:yaml.org,2002:omap", not a shortcut, like "!!omap".
Examples:
//Will be emitted as an unordered mapping (default for mappings)
auto map   = Node([1, 2], ["a", "b"]);
//Will be emitted as an ordered map (overriden tag)
auto omap  = Node([1, 2], ["a", "b"], "tag:yaml.org,2002:omap");
//Will be emitted as pairs (overriden tag)
auto pairs = Node([1, 2], ["a", "b"], "tag:yaml.org,2002:pairs");
const pure nothrow @property @safe bool isValid();

Is this node valid (initialized)?

const nothrow @property @safe bool isScalar();

Is this node a scalar value?

const nothrow @property @safe bool isSequence();

Is this node a sequence?

const nothrow @property @safe bool isMapping();

Is this node a mapping?

const nothrow @property @safe bool isUserType();

Is this node a user defined type?

const nothrow @property @safe bool isNull();

Is this node null?

const nothrow @property @safe string tag();

Return tag of the node.

const @safe bool opEquals(T)(auto ref const T rhs);

Equality test.

If T is Node, recursively compare all subnodes. This might be quite expensive if testing entire documents.
If T is not Node, get a value if type T from the node and test equality with that.
To test equality with a null YAML value, use YAMLNull.

Examples:
auto node = Node(42);

assert(node == 42);
assert(node != "42");
assert(node != "43");
Parameters:
T rhs Variable to test equality with.
Returns:
true if equal, false otherwise.
alias as = get(T, Flag stringConversion = Yes.stringConversion)() if (!is(T == const));

Shortcut for get().

@trusted T get(T, Flag!"stringConversion" stringConversion = Yes.stringConversion)() if (!is(T == const));

Get the value of the node as specified type.

If the specifed type does not match type in the node, conversion is attempted. The stringConversion template parameter can be used to disable conversion from non-string types to strings.
Numeric values are range checked, throwing if out of range of requested type.
Timestamps are stored as std.datetime.SysTime. Binary values are decoded and stored as ubyte[].
To get a null value, use get!YAMLNull . This is to prevent getting null values for types such as strings or classes.

Mapping default values:

The '=' key can be used to denote the default value of a mapping. This can be used when a node is scalar in early versions of a program, but is replaced by a mapping later. Even if the node is a mapping, the get method can be used as if it was a scalar if it has a default value. This way, new YAML files where the node is a mapping can still be read by old versions of the program, which expect the node to be a scalar.

Examples:
Automatic type conversion:
auto node = Node(42);

assert(node.as!int == 42);
assert(node.as!string == "42");
assert(node.as!double == 42.0);
Returns:
Value of the node as specified type.
Throws:
NodeException if unable to convert to specified type, or if the value is out of range of requested type.
const @trusted T get(T, Flag!"stringConversion" stringConversion = Yes.stringConversion)() if (is(T == const));

Ditto.

const @property @safe size_t length();

If this is a collection, return its length.

Otherwise, throw NodeException.

Returns:
Number of elements in a sequence or key-value pairs in a mapping.
Throws:
NodeException if this is not a sequence nor a mapping.
@trusted Node opIndex(T)(T index);

Get the element at specified index.

If the node is a sequence, index must be integral.

If the node is a mapping, return the value corresponding to the first key equal to index.
To get element at a null index, use YAMLNull for index.

Parameters:
T index Index to use.
Returns:
Value corresponding to the index.
Throws:
NodeException if the index could not be found, non-integral index is used with a sequence or the node is not a collection.
const @safe bool contains(T)(T rhs);

Determine if a collection contains specified value.

If the node is a sequence, check if it contains the specified value. If it's a mapping, check if it has a value that matches specified value.
To check for a null value, use YAMLNull for rhs.

Parameters:
T rhs Item to look for.
Returns:
true if rhs was found, false otherwise.
Throws:
NodeException if the node is not a collection.
const @safe bool containsKey(T)(T rhs);

Determine if a collection contains specified key.

If the node is a mapping, check if it has a key that matches specified key.
To check for a null key, use YAMLNull for rhs.

Parameters:
T rhs Item to look for.
Returns:
true if rhs was found, false otherwise.
Throws:
NodeException if the node is not a mapping.
nothrow @safe void opAssign(Node rhs);

Assignment (shallow copy) by value.

nothrow @trusted void opAssign(ref Node rhs);

Assignment (shallow copy) by reference.

@safe void opIndexAssign(K, V)(V value, K index);

Set element at specified index in a collection.

This method can only be called on collection nodes.
If the node is a sequence, index must be integral.
If the node is a mapping, sets the value corresponding to the first key matching index (including conversion, so e.g. "42" matches 42).
If the node is a mapping and no key matches index, a new key-value pair is added to the mapping. In sequences the index must be in range. This ensures behavior siilar to D arrays and associative arrays.
To set element at a null index, use YAMLNull for index.

Parameters:
K index Index of the value to set.
Throws:
NodeException if the node is not a collection, index is out of range or if a non-integral index is used on a sequence node.
@trusted int opApply(T)(int delegate(ref T) dg);

Foreach over a sequence, getting each element as T.

If T is Node, simply iterate over the nodes in the sequence. Otherwise, convert each node to T during iteration.

Throws:
NodeException if the node is not a sequence or an element could not be converted to specified type.
@trusted int opApply(K, V)(int delegate(ref K, ref V) dg);

Foreach over a mapping, getting each key/value as K/V.

If the K and/or V is Node, simply iterate over the nodes in the mapping. Otherwise, convert each key/value to T during iteration.

Throws:
NodeException if the node is not a mapping or an element could not be converted to specified type.
@safe void add(T)(T value);

Add an element to a sequence.

This method can only be called on sequence nodes.
If value is a node, it is copied to the sequence directly. Otherwise value is converted to a node and then stored in the sequence.

When emitting, all values in the sequence will be emitted. When using the !!set tag, the user needs to ensure that all elements in the sequence are unique, otherwise invalid YAML code will be emitted.

Parameters:
T value Value to add to the sequence.
@safe void add(K, V)(K key, V value);

Add a key-value pair to a mapping.

This method can only be called on mapping nodes.
If key and/or value is a node, it is copied to the mapping directly. Otherwise it is converted to a node and then stored in the mapping.

It is possible for the same key to be present more than once in a mapping. When emitting, all key-value pairs will be emitted. This is useful with the "!!pairs" tag, but will result in invalid YAML with "!!map" and "!!omap" tags.

Parameters:
K key Key to add.
V value Value to add.
@trusted Node* opBinaryRight(string op, K)(K key) if (op == "in");

Determine whether a key is in a mapping, and access its value.

This method can only be called on mapping nodes.

Parameters:
K key Key to search for.
Returns:
A pointer to the value (as a Node) corresponding to key, or null if not found.

Note:
Any modification to the node can invalidate the returned pointer.

See Also:
contains
@trusted void remove(T)(T rhs);

Remove first (if any) occurence of a value in a collection.

This method can only be called on collection nodes.
If the node is a sequence, the first node matching value is removed. If the node is a mapping, the first key-value pair where value matches specified value is removed.

Parameters:
T rhs Value to remove.
Throws:
NodeException if the node is not a collection.
@trusted void removeAt(T)(T index);

Remove element at the specified index of a collection.

This method can only be called on collection nodes.
If the node is a sequence, index must be integral.
If the node is a mapping, remove the first key-value pair where key matches index.
If the node is a mapping and no key matches index, nothing is removed and no exception is thrown. This ensures behavior siilar to D arrays and associative arrays.

Parameters:
T index Index to remove at.
Throws:
NodeException if the node is not a collection, index is out of range or if a non-integral index is used on a sequence node.
const @safe int opCmp(ref const Node node);

Compare with another node.