Can YamlDotNet Deserialize to an dynamic object graph?

Viewed 200

I have a large YAML document that I want to deserialize to a dynamic object (ExpandoObject). Can YamlDotNet deserialize directly to a dynamic object tree?

I can get there by going to Json first and then using NewtonSoft to deserialize to an ExpandoObject, but I'd rather skip that step.

using var input = new StreamReader("C:\\path\\to\\config.yaml");
var deserializer = new DeserializerBuilder()
    .Build();
var yamlObject = deserializer.Deserialize(input);
var serializer = new SerializerBuilder()
    .JsonCompatible()
    .Build();

var json = serializer.Serialize(yamlObject);
dynamic config = JsonConvert.DeserializeObject<ExpandoObject>(json, new ExpandoObjectConverter());
1 Answers

Hello from the future,

I once, long ago, used this exact feature. However due to a lack of users and an desire to maintain it, it was split off from the main YamlDotNet libray.

You can find the source code here: https://github.com/aaubry/YamlDotNet.Dynamic

It is unlikely to work anymore but maybe you can adapt it if you need.

I know this answer probably isn't any help to you now, but I personally hate to see good questions with no answers so I hope this helps someone else.

Related