Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalarStyle.Literal not rendering as I would expect? #520

Open
f2calv opened this issue Aug 19, 2020 · 1 comment
Open

ScalarStyle.Literal not rendering as I would expect? #520

f2calv opened this issue Aug 19, 2020 · 1 comment

Comments

@f2calv
Copy link

f2calv commented Aug 19, 2020

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?

@EdwardCooke
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants