Mir-ion:YAML Example

WebFreak001 d.forum at webfreak.org
Wed Apr 12 12:11:34 UTC 2023


On Wednesday, 12 April 2023 at 12:00:14 UTC, Vino wrote:
> Hi All,
>
>   Can some point me where i can find examples on how to use 
> mir-ion YAML
>
> From,
> Vino.B

documentation is very sparse, but essentially with mir-ion you 
import the different ser/deser packages that you would like to 
use. If you for example want to deserialize YAML to a custom 
struct, you use

```d
import mir.deser.yaml;

struct CustomStruct
{
     string foo;
     uint bar;
}

void main()
{
     string yamlString = `{foo: str, bar: 4}`;
     CustomStruct s = yamlString.deserializeYaml!CustomStruct;

     assert(s == S("str", 4));
}
```

If you want to deserialize arbitrary values and introspect them 
at runtime, you use `YamlAlgebraic` instead of `CustomStruct`, 
which you can think of like std.json : JSONValue, but utilizes 
mir's algebraic structures, which work more like std.sumtype.

To convert algebraic values to custom structs later, you use 
mir.ion.conv : serde, which basically just internally serializes 
the data and deserializes it again (but a little more efficiently)

more deserialization examples in the unittests: 
https://github.com/libmir/mir-ion/blob/62c476a6a00d0d5ddfb3585bdfbe520d825e872b/source/mir/deser/yaml.d

and for serialization you use `serializeYaml` from 
`mir.ser.yaml`, see 
https://github.com/libmir/mir-ion/blob/62c476a6a00d0d5ddfb3585bdfbe520d825e872b/source/mir/ser/yaml.d


More information about the Digitalmars-d-learn mailing list