SharpYaml allows you to define custom serialization and deserialization logic for specific CLR types using converters. You can register these converters at three different levels of granularity, which determine their scope and priority:
- Member-level attribute (Highest Priority): Apply
[YamlConverter(typeof(YourConverter))] directly to a specific property or field. This overrides all other settings for that specific member. - Options-level (High Priority): Add converters to the
Converters list in YamlSerializerOptions. This applies the converter globally to all instances of the supported type. - Type-level attribute (Medium Priority): Apply
[YamlConverter(typeof(YourConverter))] to the class or struct definition. This applies to all instances of that type throughout the serialization process.
If no custom converter is matched, SharpYaml falls back to built-in converters, then to .NET 7+ IParsable<T> implementations, and finally to the default reflection-based object converter.
// 1. Member-level
public class Config {
[YamlConverter(typeof(HexIntConverter))]
public int Color { get; set; }
}
// 2. Options-level
var options = new YamlSerializerOptions {
Converters = [ new IPAddressConverter() ]
};
// 3. Type-level
[YamlConverter(typeof(TemperatureConverter))]
public struct Temperature { ... }