serializeYaml

Ion serialization function.

@safe
string
serializeYaml
(
V
)
(
auto scope ref const V value
,,
int serdeTarget = SerdeTarget.yaml
)

Parameters

value V

value to serializa

params YamlSerializationParams

(optional) serialization param

serdeTarget int

(optional) serialization target ID

Return Value

Type: string

UTF-8 YAML text

Examples

    import mir.test: should;

    static struct S
    {
        string foo;
        uint bar;
    }

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

Tags (annotations) support

    import mir.test: should;
    import mir.algebraic: Variant;
    import mir.serde: serdeAlgebraicAnnotation;

    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("str", 4).serializeYaml.should == "{foo: str, bar: 4}\n";
    V("str", 4).serializeYaml.should == "!S {foo: str, bar: 4}\n";

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

YAML-specific serialization

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

auto value = YamlAlgebraic(["foo".YamlAlgebraic, 123.9.YamlAlgebraic, "bar".YamlAlgebraic]);

value.serializeYaml.should == "[foo, 123.9, bar]\n";

value.collectionStyle = YamlCollectionStyle.block;
value.serializeYaml.should == "- foo\n- 123.9\n- bar\n";

User API for YAML-specific serialization

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

static struct MyYamlStruct
{
    int b;

    // has to be scope const
    auto opCast(T : YamlAlgebraic)() scope const @safe pure
    {
        return b.YamlAlgebraic;
    }
}

MyYamlStruct(40).serializeYaml.should == "40\n";

Meta