Value of the node as specified type.
NodeException if unable to convert to specified type, or if the value is out of range of requested type.
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(scope const Node node) @safe { // `std.array.split` is not marked as taking a `scope` range, // but we don't escape a reference. scope parts = () @trusted { return 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
1 import dyaml.dumper : dumper; 2 import dyaml.loader : Loader; 3 4 static class MyClass 5 { 6 int x, y, z; 7 8 this(int x, int y, int z) 9 { 10 this.x = x; 11 this.y = y; 12 this.z = z; 13 } 14 15 this(scope const Node node) @safe inout 16 { 17 // `std.array.split` is not marked as taking a `scope` range, 18 // but we don't escape a reference. 19 scope parts = () @trusted { return 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 format("MyClass(%s, %s, %s)", x, y, z); 29 } 30 31 Node opCast(T: Node)() @safe 32 { 33 //Using custom scalar format, x:y:z. 34 auto scalar = format("%s:%s:%s", 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));
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. )