Node.get

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 mir.timestamp.Timestamp. 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:

$(PBR 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. )

  1. inout(T) get()
    struct Node
    inout
    @serdeIgnore
    inout(T)
    get
    (
    T
    Flag!"stringConversion" stringConversion = Yes.stringConversion
    )
    ()
    if (
    allowed!(Unqual!T) ||
    hasNodeConstructor!(inout(Unqual!T))
    ||
    (
    !hasIndirections!(Unqual!T) &&
    )
    )
  2. T get()

Return Value

Type: inout(T)

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.

Examples

Automatic type conversion

auto node = Node(42);

assert(node.get!int == 42);
assert(node.get!string == "42");
assert(node.get!double == 42.0);

Scalar node to struct and vice versa

import std.array: appender;
import mir.format: text;
import dyaml.dumper : dumper;
import dyaml.loader : Loader;
static struct MyStruct
{
    int x, y, z;

    this(int x, int y, int z) @safe
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    this(Node node) @safe
    {
        import std.string: split;
        auto parts = node.as!string().split(":");
        x = parts[0].to!int;
        y = parts[1].to!int;
        z = parts[2].to!int;
    }

    Node opCast(T: Node)() @safe
    {
        //Using custom scalar format, x:y:z.
        //Representing as a scalar, with custom tag to specify this data type.
        return Node(text!":"(x, y, z), "!mystruct.tag");
    }
}

auto app = appender!(char[]);

// Dump struct to yaml document
dumper().dump(app, Node(MyStruct(1,2,3)));

// Read yaml document back as a MyStruct
auto loader = Loader.fromString(app.data);
Node node = loader.load();
assert(node.as!MyStruct == MyStruct(1,2,3));

Sequence node to struct and vice versa

import std.array: Appender;
import dyaml.dumper : dumper;
import dyaml.loader : Loader;
static struct MyStruct
{
    int x, y, z;

    this(int x, int y, int z) @safe
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    this(Node node) @safe
    {
        x = node[0].as!int;
        y = node[1].as!int;
        z = node[2].as!int;
    }

    Node opCast(T: Node)()
    {
        return Node([x, y, z], "!mystruct.tag");
    }
}

auto appender = new Appender!string;

// Dump struct to yaml document
dumper().dump(appender, Node(MyStruct(1,2,3)));

// Read yaml document back as a MyStruct
auto loader = Loader.fromString(appender.data);
Node node = loader.load();
assert(node.as!MyStruct == MyStruct(1,2,3));

Mapping node to struct and vice versa

import std.array: Appender;
import dyaml.dumper : dumper;
import dyaml.loader : Loader;
static struct MyStruct
{
    int x, y, z;

    Node opCast(T: Node)()
    {
        auto pairs = [Node.Pair("x", x),
            Node.Pair("y", y),
            Node.Pair("z", z)];
        return Node(pairs, "!mystruct.tag");
    }

    this(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    this(Node node) @safe
    {
        x = node["x"].as!int;
        y = node["y"].as!int;
        z = node["z"].as!int;
    }
}

auto appender = new Appender!string;

// Dump struct to yaml document
dumper().dump(appender, Node(MyStruct(1,2,3)));

// Read yaml document back as a MyStruct
auto loader = Loader.fromString(appender.data);
Node node = loader.load();
assert(node.as!MyStruct == MyStruct(1,2,3));

Classes can be used too

1 import std.array: Appender;
2 import dyaml.dumper : dumper;
3 import dyaml.loader : Loader;
4 
5 static class MyClass
6 {
7     int x, y, z;
8 
9     this(int x, int y, int z)
10     {
11         this.x = x;
12         this.y = y;
13         this.z = z;
14     }
15 
16     this(Node node) @safe inout
17     {
18         import std.string: split;
19         auto parts = node.as!string().split(":");
20         x = parts[0].to!int;
21         y = parts[1].to!int;
22         z = parts[2].to!int;
23     }
24 
25     ///Useful for Node.as!string.
26     override string toString()
27     {
28         return text("MyClass(", x,", ", y,", ", z,")");
29     }
30 
31     Node opCast(T: Node)() @safe
32     {
33         //Using custom scalar format, x:y:z.
34         auto scalar = text!":"(x, y, z);
35         //Representing as a scalar, with custom tag to specify this data type.
36         return Node(scalar, "!myclass.tag");
37     }
38     override bool opEquals(Object o)
39     {
40         if (auto other = cast(MyClass)o)
41         {
42             return (other.x == x) && (other.y == y) && (other.z == z);
43         }
44         return false;
45     }
46 }
47 auto appender = new Appender!string;
48 
49 // Dump class to yaml document
50 dumper().dump(appender, Node(new MyClass(1,2,3)));
51 
52 // Read yaml document back as a MyClass
53 auto loader = Loader.fromString(appender.data);
54 Node node = loader.load();
55 assert(node.as!MyClass == new MyClass(1,2,3));

Meta