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 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:

$(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()
  2. T get()
    struct Node
    const
    T
    get
    (
    T
    )
    ()
    if (
    hasIndirections!(Unqual!T) &&
    &&
    (!hasNodeConstructor!(inout(Unqual!T)))
    )

Return Value

Type: 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 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
    {
        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.
        auto scalar = format("%s:%s:%s", x, y, z);
        //Representing as a scalar, with custom tag to specify this data type.
        return Node(scalar, "!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));

Sequence node to struct and vice versa

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 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

import dyaml.dumper : dumper;
import dyaml.loader : Loader;

static class MyClass
{
    int x, y, z;

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

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

    ///Useful for Node.as!string.
    override string toString()
    {
        return format("MyClass(%s, %s, %s)", x, y, z);
    }

    Node opCast(T: Node)() @safe
    {
        //Using custom scalar format, x:y:z.
        auto scalar = format("%s:%s:%s", x, y, z);
        //Representing as a scalar, with custom tag to specify this data type.
        return Node(scalar, "!myclass.tag");
    }
    override bool opEquals(Object o)
    {
        if (auto other = cast(MyClass)o)
        {
            return (other.x == x) && (other.y == y) && (other.z == z);
        }
        return false;
    }
}
auto appender = new Appender!string;

// Dump class to yaml document
dumper().dump(appender, Node(new MyClass(1,2,3)));

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

Meta