Here's the general structure:

public struct Choice
{
    public string result;
    public string nextBranch;
}

public struct Branch
{
    public Dictionary<string, Choice> choices;
}

public class ChoiceTree
{
    public Dictionary<string, Branch> branches;
    // some other properties

    // some methods
}

And I'm currently manually filling up my ChoiceTree object with textboxes because this is a WPF project. I'd like, instead, to parse them from a YAML file. I've made this as a starting point. I have no idea if it's set up correctly, but I did at least use an online checker to make sure the syntax is correct:

branches:
  - Key: "main1"
    Value:
      choices:
        - Key: "few minutes"
          Value:
            result: "S"
            nextBranch: "refuse"
        - Key: "hold on"
          Value:
            result: "D"
            nextBranch: "refuse"
        - Key: "of course"
          Value:
            result: "LS"
            nextBranch: "accept"
        - Key: "fine"
          Value:
            result: "HD"
            nextBranch: "accept"
  - Key: "refuse"
    Value:
      choices:
        - Key: "reluctant"
          Value:
            result: "HH"
            nextBranch: "accept"
        - Key: "happily"
          Value:
            result: "L"
            nextBranch: "accept"
# more branches below...

I think the closest I've gotten so far is following this example. And by "closest" I mean, at least the code below compiles. But it does not work, and I have no idea why.

public class YamlChoiceTreeeParser
{
    public YamlChoiceTreeeParser(string filePath)
    {
        if (!File.Exists(filePath))
        {
            throw new Exception("YAML file not found");
        }
        using (StreamReader sr = new StreamReader(filePath))
        {
            var deserializer = new DeserializerBuilder()
                .Build();
            var choiceTree = deserializer.Deserialize<ChoiceTree>(sr);
        }
    }
}

I've tried a bunch of different variations on the yaml file, and a bunch of different examples, but I can't even step into the last line without an exception. (with the current code above, the exception I'm getting is "Expected 'MappingStart', got 'SequenceStart'"

2 Replies 2

For a start, main1 has a different structure to refuse. Are you married to that structure?

@DavidG Sorry, there was a copy-paste error in there. Guess the yaml checker can't fix that. But I updated the post with the corrected format. I'm not exactly sure how to answer your question. I don't know what that means exactly.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.