Open imclint21 opened 1 month ago
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
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.Regards