I want to create a YamlVisitor using C# yamldotnet lib. I need to customize the built-in YamlVisitorBase so that I can traverse each level (something like BFS visitor) of the document and if there are scalar values in that level (i.e scalar sequence items) I want to add them to a List<KeyValuePair<string,string>> that represents a group of “sibling” values (the Key would be the path to the value and the Value would be the scalar value). If there are mapping nodes with scalar values (keyvalue pairs with scalar values under the same level of a block mapping) I also want to consider their values as “sibling” values and add them to another sibling group where each KeyValuePair would have the path to the value from the root to the key of the mapping as the Key is and the scalar value as the Value. The visitor should return a collection with all of the groups created in the Yaml file.
The visitor should ensure that scalar siblings are grouped together correctly, even if there are mapping pairs between them.
example:
For the input:
sequence:
- scalar1
- scalar2
- scalar3
- mapping1:
key1: value1
key2: value2
- mapping2:
key3: value3
key4: value4
- scalar4
- scalar5```
The expected output groups would contain 3 groups of KeyValue pairs. The Values of the groups would be:
1. Group 1:
• scalar1
• scalar2
• scalar3
• scalar4
• scalar5
2. Group 2:
• value1
• value2
3. Group 3:
• value3
• value4
Examples of the KeyValuePairs in each group:
The KeyValue pair of the first item in Group 1 would be:
`Key: “sequence[0]”, Value: “scalar1”`
The KeyValue pair of the first item in Group 2 (sibling mapping items) would be:
`Key: “sequence.mapping1.key1”, Value: “value1”`
What is the most efficient way to do it?
DotNet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.