3

I'm porting my configuration file from .json to .yaml format. In Newtonsoft.Json I was able to apply attribute to a property which needed custom converter, for example

[JsonConverter(typeof(CustomIdConverter))]
public IList<CustomID> Users { get; set; }

How would I do the same using YamlDotNet?

I know converters should implement IYamlTypeConverter interface, but how would I apply this converter to exact property?

stil
  • 5,306
  • 3
  • 38
  • 44

1 Answers1

5

There is no support for that, although that would be a useful feature. What is supported is to associate a converter to a type. As a workaround, you can create a custom type for your property and associate the converter to it:

public interface ICustomIDList : IList<CustomID> {}

public class CustomIDListConverter : IYamlTypeConverter { /* ... */ }

var deserializer = new DeserializerBuilder()
   .WithTypeConverter(new CustomIDListConverter())
   .Build();
Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
  • I've opened an issue about this - https://github.com/aaubry/YamlDotNet/issues/233 – Antoine Aubry Jan 11 '17 at 00:02
  • This workaround definitely works, but I'd love to see that implemented with property attribute. It adds very granular control over serialization and will certainly solve many popular use cases (including mine). – stil Jan 11 '17 at 00:11