stil
stil

Reputation: 5556

How to apply custom converter to properties in YamlDotNet

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?

Upvotes: 3

Views: 2192

Answers (1)

Antoine Aubry
Antoine Aubry

Reputation: 12469

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();

Upvotes: 5

Related Questions