1

I have the following yaml structure:

TestPaySignService:
    Test:
        TestString:
            settings-key: TestStringValue
            types:
                - type1
                - type2

        TestString2:
            settings-key: TestStringValue2
            types:
                - type1
                - type2

I want to parse this yaml and store only "settings-key" and "types" together in c# collection, for example List, like the following:

[
  {
     name: TestStringValue,
     types: ["type1", "type2"]
  },
  {
     name: TestStringValue2,
     types: ["type1", "type2"]
  }
]

I've followed the follwin question and tried the accepted answer's code, but as it seems it's obsolete for now: Deserialize a YAML "Table" of data

P.S. my yaml schema changes every time, but "settings-key" and "types" are always contained, so I want to create something like "dynamic parser"

David Maisuradze
  • 824
  • 2
  • 12
  • 27
  • What did you try? What do you mean `obsolete`? You can load the structure in whatever way you want and then flatten/transform it using LINQ – Panagiotis Kanavos Jul 26 '18 at 11:37
  • "You can load the structure in whatever way you want and then flatten/transform it using LINQ" please look at my second question: https://stackoverflow.com/questions/51535791/c-sharp-get-all-property-conditionally-from-dynamic-collection-which-looks-like?noredirect=1#comment90039457_51535791 – David Maisuradze Jul 26 '18 at 11:48
  • Possible duplicate of [Deserializing YAML using YamlDotNet when the root node of each object is named using it's ID?](https://stackoverflow.com/questions/41146291/deserializing-yaml-using-yamldotnet-when-the-root-node-of-each-object-is-named-u) – IteratioN7T Jul 26 '18 at 11:48

1 Answers1

0

You can deserialize the yaml to a dynamic object then walk a bit. Something like the following:

Try it Online!

public static void Main()
{
    var r = new StringReader(@"TestPaySignService:
Test:
    TestString:
        settings-key: TestStringValue
        types:
            - type1
            - type2

    TestString2:
        settings-key: TestStringValue2
        types:
            - type1
            - type2"); 
    var deserializer = new Deserializer();
    var yamlObject = deserializer.Deserialize<dynamic>(r)["TestPaySignService"]["Test"].Values;

    // just to print the json
    var serializer = new JsonSerializer();
    serializer.Serialize(Console.Out, yamlObject);
}

Output

[{
    "settings-key": "TestStringValue",
    "types": [
        "type1",
        "type2"
    ]
},
{
    "settings-key": "TestStringValue2",
    "types": [
        "type1",
        "type2"
    ]
}]

If you need to use TestString, you should deserialize it into an object.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • thanks, but when yaml schema is more "deeper" then it's a bit complicated. please review my second question: https://stackoverflow.com/questions/51535791/c-sharp-get-all-property-conditionally-from-dynamic-collection-which-looks-like?noredirect=1#comment90039457_51535791 – David Maisuradze Jul 26 '18 at 11:51