Constructor.addConstructorMapping

Add a constructor function from a mapping.

class Constructor
@safe nothrow
void
addConstructorMapping
(
T
)
(
const string tag
,
T function
(
ref Node
)
ctor
)

Examples

import std.string;

import dyaml.all;

struct MyStruct
{
    int x, y, z;

    //Any D:YAML type must have a custom opCmp operator.
    //This is used for ordering in mappings.
    const int opCmp(ref const MyStruct s)
    {
        if(x != s.x){return x - s.x;}
        if(y != s.y){return y - s.y;}
        if(z != s.z){return z - s.z;}
        return 0;
    }
}

MyStruct constructMyStructMapping(ref Node node)
{
    //node is guaranteed to be mapping.
    //!mystruct {"x": x, "y": y, "z": z}
    return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
}

void main()
{
    auto loader = Loader("file.yaml");
    auto constructor = new Constructor;
    constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
    loader.constructor = constructor;
    Node node = loader.load();
}

See Also

addConstructorScalar

Meta