Representer.addRepresenter

Add a function to represent nodes with a specific data type.

The representer function takes references to a Node storing the data type and to the Representer. It returns the represented node and may throw a RepresenterException. See the example for more information.

More...
class Representer
void
addRepresenter
@trusted pure
(
T
)

Detailed Description

Only one function may be specified for one data type. Default data types already have representer functions unless disabled in the Representer constructor.

Structs and classes must implement the opCmp() operator for D:YAML support. The signature of the operator that must be implemented is const int opCmp(ref const MyStruct s) for structs where MyStruct is the struct type, and int opCmp(Object o) for classes. Note that the class opCmp() should not alter the compared values - it is not const for compatibility reasons.

Examples

Representing a simple struct:

1 import std.string;
2 
3 import dyaml.all;
4 
5 struct MyStruct
6 {
7    int x, y, z;
8 
9    //Any D:YAML type must have a custom opCmp operator.
10    //This is used for ordering in mappings.
11    const int opCmp(ref const MyStruct s)
12    {
13        if(x != s.x){return x - s.x;}
14        if(y != s.y){return y - s.y;}
15        if(z != s.z){return z - s.z;}
16        return 0;
17    }
18 }
19 
20 Node representMyStruct(ref Node node, Representer representer)
21 { 
22    //The node is guaranteed to be MyStruct as we add representer for MyStruct.
23    auto value = node.as!MyStruct;
24    //Using custom scalar format, x:y:z.
25    auto scalar = format("%s:%s:%s", value.x, value.y, value.z);
26    //Representing as a scalar, with custom tag to specify this data type.
27    return representer.representScalar("!mystruct.tag", scalar);
28 }
29 
30 void main()
31 {
32    auto dumper = Dumper("file.yaml");
33    auto representer = new Representer;
34    representer.addRepresenter!MyStruct(&representMyStruct);
35    dumper.representer = representer;
36    dumper.dump(Node(MyStruct(1,2,3)));
37 }

Representing a class:

1 import std.string;
2 
3 import dyaml.all;
4 
5 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    //Any D:YAML type must have a custom opCmp operator.
17    //This is used for ordering in mappings.
18    override int opCmp(Object o)
19    {
20        MyClass s = cast(MyClass)o;
21        if(s is null){return -1;}
22        if(x != s.x){return x - s.x;}
23        if(y != s.y){return y - s.y;}
24        if(z != s.z){return z - s.z;}
25        return 0;
26    }
27 
28    ///Useful for Node.as!string .
29    override string toString()
30    {
31        return format("MyClass(%s, %s, %s)", x, y, z);
32    }
33 }
34 
35 //Same as representMyStruct.
36 Node representMyClass(ref Node node, Representer representer)
37 { 
38    //The node is guaranteed to be MyClass as we add representer for MyClass.
39    auto value = node.as!MyClass;
40    //Using custom scalar format, x:y:z.
41    auto scalar = format("%s:%s:%s", value.x, value.y, value.z);
42    //Representing as a scalar, with custom tag to specify this data type.
43    return representer.representScalar("!myclass.tag", scalar);
44 }
45 
46 void main()
47 {
48    auto dumper = Dumper("file.yaml");
49    auto representer = new Representer;
50    representer.addRepresenter!MyClass(&representMyClass);
51    dumper.representer = representer;
52    dumper.dump(Node(new MyClass(1,2,3)));
53 }

Meta