Vogen
Vogen copied to clipboard
Add source-generated YAML conversions
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()));
}
}
Thanks for the feedback. I've never heard of YamlDotNet, but I'll certainly add this to the list of conversions!