I am using yamldotnet and c# to deserialize a file created by a third party software application. The following YAML file examples are both valid from the application:
#File1
Groups:
- Name: ATeam
FirstName, LastName, Age, Height:
- [Joe, Soap, 21, 184]
- [Mary, Ryan, 20, 169]
- [Alex, Dole, 24, 174]
#File2
Groups:
- Name: ATeam
FirstName, LastName, Height:
- [Joe, Soap, 184]
- [Mary, Ryan, 169]
- [Alex, Dole, 174]
Notice that File2 doesnt have any Age column but the deserializer must still recognise that the third value on each line is a height rather than an age. This data is supposed to represent a table of people. In the case of File1 for example, Mary Ryan is age 20 and is 169cm tall. The deserializer needs to understand the columns it has (for File2 it only has FirstName, LastName and Height) and store the data accordingly in the right objects : Mary Ryan is 169cm tall.
Similarly the program documentation states that the order of the columns is not important so File3 below is an equally valid way to represent the data in File2 even though Height is now first:
#File3
Groups:
- Name: ATeam
Height, FirstName, LastName:
- [184, Joe, Soap]
- [169, Mary, Ryan]
- [174, Alex, Dole]
I have a number of questions:
- Is this standard YAML? - I could not find anything about the use of a number of keys on the same line followed by a colon and lists of values to represent tables of data.
- How would I use yamldotnet to deserialize this? Are there modifications I can make to help it?
- If I can't use yamldotnet, how should I go about it?
As other answers stated, this is valid YAML. However, the structure of the document is specific to the application, and does not use any special feature of YAML to express tables.
You can easily parse this document using YamlDotNet. However you will run into two difficulties. The first is that, since the names of the columns are placed inside the key, you will need to use some custom serialization code to handle them. The second is that you will need to implement some kind of abstraction to be able to access the data in a tabular way.
I have put-up a proof of concept that will illustrate how to parse and read the data.
First, create a type to hold the information from the YAML document:
Then implement
IYamlTypeConverter
to parse theGroup
type:Last, register the converter into the deserializer and deserialize the document:
You can test the fully working example here
This is only a proof of concept, but it should serve as a guideline for you own implementation. Things that could be improved include:
Group
class. Maybe make it immutable, and also add an indexer.WriteYaml
method if serialization support is desired.