Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Add source-generated YAML conversions

Open 0x326 opened this issue 4 months ago • 1 comments

Describe the feature

Just like how there are conversions for Newtonsoft.Json, System.Text.Json, and Bson, it would be nice to have a built-in conversion for YamlDotNet.

If it helps, here is an example YamlDotNet converter suggested by AI:

using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using Vogen;

public class VogenYamlConverter<TValueObject, TPrimitive> : IYamlTypeConverter
    where TValueObject : struct, IValueObject<TPrimitive>
{
    public bool Accepts(Type type) => type == typeof(TValueObject);

    public object ReadYaml(IParser parser, Type type)
    {
        var scalar = parser.Consume<Scalar>();
        var primitive = (TPrimitive)Convert.ChangeType(scalar.Value, typeof(TPrimitive));
        return ValueObjectFactory.Create<TValueObject, TPrimitive>(primitive);
    }

    public void WriteYaml(IEmitter emitter, object value, Type type)
    {
        var primitive = ((TValueObject)value).Value;
        emitter.Emit(new Scalar(primitive?.ToString()));
    }
}

0x326 avatar Aug 21 '25 13:08 0x326

Thanks for the feedback. I've never heard of YamlDotNet, but I'll certainly add this to the list of conversions!

SteveDunn avatar Aug 27 '25 11:08 SteveDunn