YamlDotNet
YamlDotNet copied to clipboard
ScalarStyle.Literal not rendering as I would expect?
Firstly, great library, thanks 👍
I am persisting various objects to YAML files, some of which have string properties that are multi-line. My LiteralMultilineEventEmitter is working to a degree, however the format of the YAML multi-line indicator isn't what I was expecting/wanting.
Demo code;
using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.EventEmitters;
namespace TestApp1
{
class Program
{
static void Main(string[] args)
{
var obj = new
{
someString = "hello world",
someMultiLineString = @"hello
world
123"
};
var serializer = new SerializerBuilder()
.WithEventEmitter(e => new LiteralMultilineEventEmitter(e))
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
.DisableAliases()
.Build();
var str = serializer.Serialize(obj);
File.WriteAllText("c:/temp/test.yml", str);
}
}
class LiteralMultilineEventEmitter : ChainedEventEmitter
{
public LiteralMultilineEventEmitter(IEventEmitter nextEmitter) : base(nextEmitter) { }
public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
if (eventInfo.Source.Type == typeof(string) && eventInfo.Source.Value is string value && value.Contains("\n"))
eventInfo.Style = ScalarStyle.Literal;
base.Emit(eventInfo, emitter);
}
}
}
The above code outputs;
someString: hello world
someMultiLineString: |-
hello
world
123
However I was expecting;
someString: hello world
someMultiLineString: |
hello
world
123
i.e. I would like to to have | (not |- with the extra hyphen), is there an override possible here or am I missing a configuration option?
There is not a way of omitting the chomp indicator (which is what the +/- after the | is). I'll add it as an enhancement later on down the road.