YamlDotNet
YamlDotNet copied to clipboard
Incrementally building YAML
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 (includingIEventEmitter) also quotes dictionary keys (e.g.,'name': 'foobar'). - In my second attempt, I looked into using
YamlStreamdirectly. But there, I'm missing some capabilities that I know fromSerializerBuilder, 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! ❤️