I have a deeply nested object model:
public class CheatSheet {
public string Leader { get; set; }
public List<Section> Sections { get; set; }
}
public class Section {
public string Title { get; set; }
public List<SubSection> SubSections { get; set; }
}
public class SubSection {
public string Title { get; set; }
public List<Cheat> Cheats { get; set; }
}
public class Cheat {
public string Affected { get; set; }
public string Text { get; set; }
public string Hint { get; set; }
public string Url { get; set; }
}
And I have serialized this to YAML without any problems:
var serializer = new YamlDotNet.Serialization.Serializer();
var sb = new StringBuilder();
var sw = new StringWriter(sb);
serializer.Serialize(sw, model);
string yaml = sb.ToString();
The yaml looks good, very similar to a JSON or HJSON representation.
I now want to deserialize it - n.b. I want to deserialize it into a dynamic object NOT into the original model (which is only being used in this example to generate the YAML in the first place, it won't exist in the final assembly).
var sr = new StringReader(yaml);
var deserializer = new YamlDotNet.Serialization.Deserializer();
dynamic expando = deserializer.Deserialize<ExpandoObject>(sr);
The problem is that the resulting expando is very difficult to use, containing many unnecessary levels of nesting. For example:
expando.Sections[0]["Title"]
expando.Sections[0]["SubSections"][0]["Title"]
expando.Sections[0]["SubSections"][0]["Cheats"][0]["Text"]
But I would like this to be
expando.Sections[0].Title
expando.Sections[0].SubSections[0].Title
expando.Sections[0].SubSections[0].Cheats[0].Text
Is this possible in any way?
There is a repro program available at https://github.com/PhilipDaniels/Lithogen in the project Gitcheatsheet.TestHarness, at commit 2db9a0491e8ab50bb07aee552ddec6697c4b8bfc