deserializeYaml

Deserialize YAML document to a scpecified type.

T
deserializeYaml
(
T
)
(
scope const(char)[] text
,
string fileName = "<unknown>"
)
if (
isMutable!T
)

Parameters

T

type of the value

text const(char)[]

UTF-8 text (without BOM)

Return Value

Type: T

value of type T

Examples

import mir.test: should;

static struct S
{
    string foo;
    uint bar;
}

`{foo: str, bar: 4}`.deserializeYaml!S.should == S("str", 4);

Tags (annotations) support

import mir.test: should;
import mir.algebraic: Variant;
import mir.serde: serdeAlgebraicAnnotation, serdeAnnotation, serdeOptional;

@serdeAlgebraicAnnotation("!S")
static struct S
{
    string foo;
    uint bar;
}

@serdeAlgebraicAnnotation("rgb")
static struct RGB
{
    @serdeAnnotation @serdeOptional
    string name;

    ubyte r, g, b;
}

alias V = Variant!(S, RGB);

`!S {foo: str, bar: 4}`.deserializeYaml!V.should == V("str", 4);

// Multiple Ion annotations represented in a single tag using `::`.
`!<rgb::dark_blue> {r: 23, g: 25, b: 55}`.deserializeYaml!V.should == V(RGB("dark_blue", 23, 25, 55));

YAML-specific deserialization

import mir.algebraic_alias.yaml: YamlAlgebraic, YamlMap;
import mir.test: should;

auto yaml = `{foo: str, bar: 4}`;

auto value = yaml.deserializeYaml!YamlAlgebraic("test.yml");
value.object["bar"].should == 4;
value.tag.should == `tag:yaml.org,2002:map`;
value.startMark.file.should == "test.yml";


// `YamlMap`, `YamlAlgebraic[]`, and `Annotated!YamlAlgebraic`
// are YAML specific types as well
auto object = yaml.deserializeYaml!YamlMap("test.yml");
object["bar"].should == 4;
object["bar"].tag.should == `tag:yaml.org,2002:int`;

assert(value == object);

YAML-user-specific deserialization

import mir.test: should;
import mir.algebraic_alias.yaml: YamlAlgebraic;

static struct MyYamlStruct
{
    YamlAlgebraic node;

    this(YamlAlgebraic node) @safe pure
    {
        this.node = node;
    }
}

auto s = `{foo: str, bar: 4}`.deserializeYaml!MyYamlStruct("test.yml");
s.node.object["bar"].should == 4;
s.node.tag.should == `tag:yaml.org,2002:map`;

Meta