YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Incrementally building YAML

Open matkoch opened this issue 11 months ago • 4 comments
trafficstars

Hi there! I'm seeking wisdom :)

I want to build an API that allows to incrementally build a YAML file using classes/strings/anonymous types. Imagine the following:

new SomeWrapper()
    .Set(new TypedYamlFragment())
    .Set(new { a = new { b = "c" } })
    .Set("name: foobar")
    .AddList("key", _ => _
        .Set("""
            - item1
            - item2
            """));

This should translate to:

this-is:
  the-typed: 'part'
a:
  b: 'c'
name: 'foobar'
key:
  - 'item1'
  - 'item2'

My endeavor so far:

  • I started using nested Dictionary<string, object> in my initial draft. This works well, except I don't know how to quote only strings. Everything I tried (including IEventEmitter) also quotes dictionary keys (e.g., 'name': 'foobar').
  • In my second attempt, I looked into using YamlStream directly. But there, I'm missing some capabilities that I know from SerializerBuilder, such as setting the naming convention and default value handling.

Obligatory question: which approach would you think gives the most benefits/freedom, and how can I overcome these issues? Preferably, I'd also like to apply a partial ordering. For instance, name should be the first item in the YAML, even if it's set last.

Thanks in advance! ❤️

matkoch avatar Dec 16 '24 23:12 matkoch