YamlStringEnumConverter Class
Definition
Produces a converter that serializes any enumeration as a YAML string holding its member name, applying an optional naming policy and honoring StringEnumMemberNameAttribute on individual members.
public sealed class YamlStringEnumConverter : YamlConverterFactory
- Inheritance
-
YamlStringEnumConverter
- Inherited Members
- Extension Methods
Examples
var options = new YamlSerializerOptions();
options.Converters.Add(new YamlStringEnumConverter(NamingPolicy.SnakeCaseLower, allowIntegerValues: false));
// Status.OnHold now serializes as the YAML scalar on_hold.
Remarks
Register the factory on Converters to apply it to every enumeration, or use the generic YamlStringEnumConverter<TEnum> with ConverterAttribute to apply it to a single enumeration. Two constructors are provided: a parameterless form that applies no naming policy and accepts integers on read, and a form that takes an explicit naming policy and integer-handling flag. The produced converter writes the member name regardless of WriteEnumsAsStrings.
Constructors
YamlStringEnumConverter()
Initializes a new instance of the YamlStringEnumConverter class that applies no naming policy and accepts integers on read.
public YamlStringEnumConverter()
YamlStringEnumConverter(NamingPolicy?, bool)
Initializes a new instance of the YamlStringEnumConverter class with the specified naming policy and integer-handling behavior.
public YamlStringEnumConverter(NamingPolicy? namingPolicy, bool allowIntegerValues)
Parameters
namingPolicyNamingPolicyThe naming policy applied to member names, or null to use member names unchanged.
allowIntegerValuesboolWhether a YAML integer scalar is accepted as an enumeration value on read.
Methods
CanConvert(Type)
Determines whether the converter can convert the specified type.
public override bool CanConvert(Type typeToConvert)
Parameters
typeToConvertTypeThe type to test.
Returns
CreateConverter(Type, YamlSerializerOptions)
Creates a converter for the specified type.
public override YamlConverter CreateConverter(Type typeToConvert, YamlSerializerOptions options)
Parameters
typeToConvertTypeThe closed type to create a converter for.
optionsYamlSerializerOptionsThe serializer options in effect.
Returns
- YamlConverter
A converter that handles
typeToConvert.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |