YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Add support for YAML inline syntax

Open yariker opened this issue 6 years ago • 4 comments
trafficstars

This is a request to add support for inline style for sequences and mappings to both Parser and Emitter.

An example of a flow style block: {name: John Smith, age: 33}

An example of a flow style sequence: [milk, pumpkin pie, eggs, juice]

This format can be extremely useful for logging when double quotation mark tax of JSON is inappropriate, or in any other cases when single-line representation of data is more preferable.

yariker avatar Nov 18 '19 23:11 yariker

The Parser and Emitter already support flow mappings and sequences. What is missing is to be able to specify the flow style for specific properties, probably through the YamlMember attribute. I will assume that this is the case based on our previous conversation and the linked question.

aaubry avatar Nov 23 '19 16:11 aaubry

Thanks for clarification, @aaubry. So it seems that we need something on the serializer level instead.

It would be great to have an ability to enforce a specific node style for the whole serializer, something like this:

var serializer = new SerializerBuilder().WithNodeStyle(YamlNodeStyle.Flow).Build();
var yaml = serializer.Serialize(new
{
    Receipt = "Oz-Ware Purchase Invoice",
    Customer = new
    {
        FirstName = "John",
        LastName = "Doe",
    },
    Items = new[]
    {
        new { Part = "A4786", Name = "Water Bucket (Filled)" },
        new { Part = "E1628", Name = "High Heeled \"Ruby\" Slippers" },
    },
    Address = "123 Tornado Alley\r\nSuite 16"
});

This would give:

{Receipt: 'Oz-Ware Purchase Invoice', Customer: {FirstName: John, LastName: Doe}, Items: [{Part: A4786, Name: 'Water Bucket (Filled)'}, {Part: E1628, Name: 'High Heeled "Ruby" Slippers'}], Address: "123 Tornado Alley\nSuite 16"}

And the new enum could be as follows:

enum YamlNodeStyle
{
    Block,
    Flow,
}

In addition, there could also be a YamlMemberAttribute property, let's say NodeStyle, which would allow to fine-tune the resulting format on per-field basis as you mentioned.

yariker avatar Nov 26 '19 22:11 yariker

Would this also solve the issue of a GitHub Action currently displaying as:

trigger:
- master
- develop

instead displaying on one line?:

trigger: [master,develop]

samsmithnz avatar Dec 07 '19 13:12 samsmithnz

@yariker Doing this globally is already possible with a custom event emitter.

Example: https://dotnetfiddle.net/FYU8rF

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.EventEmitters;
                    
public class Program
{
    public static void Main()
    {        
        var serializer = new SerializerBuilder()
            .WithEventEmitter(next => new FlowEverythingEmitter(next))
            .Build();
        var yaml = serializer.Serialize(new
        {
            Receipt = "Oz-Ware Purchase Invoice",
            Customer = new
            {
                FirstName = "John",
                LastName = "Doe",
            },
            Items = new[]
            {
                new { Part = "A4786", Name = "Water Bucket (Filled)" },
                new { Part = "E1628", Name = "High Heeled \"Ruby\" Slippers" },
            },
            Address = "123 Tornado Alley\r\nSuite 16"
        });
        
        Console.WriteLine(yaml);
    }
    
    public class FlowEverythingEmitter : ChainedEventEmitter
    {
        public FlowEverythingEmitter(IEventEmitter nextEmitter) : base(nextEmitter) { }

        public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
        {
            eventInfo.Style = MappingStyle.Flow;
            base.Emit(eventInfo, emitter);
        }

        public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
        {
            eventInfo.Style = SequenceStyle.Flow;
            nextEmitter.Emit(eventInfo, emitter);
        }
    }
}

Output:

{Receipt: Oz-Ware Purchase Invoice, Customer: {FirstName: John, LastName: Doe}, Items: [{Part: A4786, Name: Water Bucket (Filled)}, {Part: E1628, Name: High Heeled "Ruby" Slippers}], Address: "123 Tornado Alley\r\nSuite 16"}

pensono avatar Jul 09 '20 05:07 pensono