0

I would like to convert a YAML document to an object graph using YamlDotNet deserializer. Below is a sample of yhe YAML file.

desks:
  - A5
  - A6
  - A7
  - A8
  - ...
employees:
  - E00
  - E10
  - E20
  - ...
schedule:
  0:
    E00: A0
    E10: A1
    E100: A10
    E20: A2
    E30: A3
    E40: A4
    E50: A5
    E60: B0
    E70: F3
    E80: A8
    E90: A9
  1:
    E00: A0
    E10: A1
    E100: A10
    E20: A2
    E30: A3
    E40: A4
    E50: A5
    E60: B0
    E70: F3
    E80: A8
    E90: A9
  ...

Following the guide I have linked earlier, I have created a Scheduler encapsulating all the nodes of my YAML document to be casted with

var deserializer = new DeserializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();

var order = deserializer.Deserialize<Scheduler>(input);
public class Scheduler
{
    public List<string> desks { get; set; }
    public List<string> employees { get; set; }

}

However, I don't know how to represent the schedule node, which is a nested dictionary of variable lengths.

  • The outer dictionary maps an integer to an inner dictionary.
  • The inner dictionaries maps a string to a string.

What kind of data structure could I use in C# to represent such a YAML structure?

pp492
  • 511
  • 1
  • 3
  • 12

1 Answers1

2

I got this working, but id be curious if there is a better way. I'm experienced with JSON deserialization but haven't had much YAML deserialization experience.

public class Scheduler
{
    public List<string> desks { get; set; }
    public List<string> employees { get; set; }
    public IDictionary<int, IDictionary<string,string>> schedule { get; set; }
}
Cory Melendez
  • 224
  • 1
  • 8