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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty line in comments #926

Closed
imclint21 opened this issue May 26, 2024 · 1 comment
Closed

Empty line in comments #926

imclint21 opened this issue May 26, 2024 · 1 comment

Comments

@imclint21
Copy link

Hi,

I try to generate an empty line using ChainedObjectGraphVisitor, but does not works, any idea how to accomplish this?

I also tried to use [YamlMember(Description = "\nGeneral configuration")] but don't work either.

public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
{
	context.Emit(new Scalar(""));
	context.Emit(new Comment("Comment", false));
	return base.EnterMapping(key, value, context);
}

Regards

@EdwardCooke
Copy link
Collaborator

You would need to create your own Emitter that inherits the current emitter and expose the text writer and use that to output the new line. Here's an example:

using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.EventEmitters;

var serializer = new SerializerBuilder().WithEventEmitter((emitter) => new TestChainedEmitter(emitter), (registration)=>registration.OnTop()).Build();

var data = new List<TestClass>
{
    new TestClass() { Name = "name 1", Description = "description 1" },
    new TestClass() { Name = "name 2", Description = "description 2" }
};

var stringWriter = new StringWriter();
var emitterSettings = new EmitterSettings();
var myEmitter = new MyEmitter(stringWriter, emitterSettings);

serializer.Serialize(myEmitter, data);
Console.WriteLine(stringWriter.ToString());

class TestClass
{
    public string Name { get; set; }
    public string Description { get; set; }
}

class TestChainedEmitter : ChainedEventEmitter
{
    public TestChainedEmitter(IEventEmitter nextEmitter) : base(nextEmitter)
    {
    }
    public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
    {
        (emitter as MyEmitter)?.Output.WriteLine();

        base.Emit(eventInfo, emitter);
    }
}

class MyEmitter : Emitter
{
    public TextWriter Output { get; }

    public MyEmitter(TextWriter output) : base(output)
    {
        Output = output;
    }

    public MyEmitter(TextWriter output, int bestIndent) : base(output, bestIndent)
    {
        Output = output;
    }

    public MyEmitter(TextWriter output, EmitterSettings settings) : base(output, settings)
    {
        Output = output;
    }

    public MyEmitter(TextWriter output, int bestIndent, int bestWidth) : base(output, bestIndent, bestWidth)
    {
        Output = output;
    }

    public MyEmitter(TextWriter output, int bestIndent, int bestWidth, bool isCanonical) : base(output, bestIndent, bestWidth, isCanonical)
    {
        Output = output;
    }
}

Outputs:


- Name: name 1
  Description: description 1

- Name: name 2
  Description: description 2

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

No branches or pull requests

2 participants