hadashiA / VYaml

The extra fast, low memory footprint YAML library for C#, focued on .NET and Unity.
MIT License
305 stars 18 forks source link

Configure enum deserialization? #39

Closed frnsys closed 10 months ago

frnsys commented 1 year ago

Hi, thanks for this library, it's great.

Is it possible to configure enum deserialization? My input yaml has enum values serialized like Foo, Bar, but it looks like this library requires them to be serialized as camel case (foo, bar). Is it possible to have an attribute or something that specifies what casing scheme it should be deserialized with (similar to serde's rename_all)?

Thanks

hadashiA commented 1 year ago

That's a fair point.

Unfortunately, currently it can only be set per enum member.

I have come up with a plan to implement the following features

Provide multiple EnumFormatters. For example,

The behavior can be changed globally by specifying them in YamlSerializerOptions.

In addition, a feature to specify a formatter for each type, as shown below, would satisfy the request.

[YamlFormatter(typeof(UpperCamelCaseEnumFormatter))]
enum Foo 
{
    A, 
    B, 
    C
}
frnsys commented 1 year ago

That sounds great to me. For now I have a workaround that seems ok. Thanks again.

hadashiA commented 10 months ago

In #60, serialization format of enum can now be specified.

[YamlObject(NamingConvention.UpperCamelCase)]
enum Foo 
{
    A, 
    B, 
    C
}

Not that it can only be changed for each enum type.