aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.58k stars 486 forks source link

Option to serialize members of base types first #984

Closed n9 closed 2 months ago

n9 commented 2 months ago

Is your feature request related to a problem? Please describe. Members are serialized in the order returned by reflection, with members of base types coming last, which is sometimes not desirable.

Describe the solution you'd like Remove sealed from ReadablePropertiesTypeInspector and and from ReflectionPropertyDescriptor. https://github.com/aaubry/YamlDotNet/blob/485daaa7fa2a9fdc5863294ef580b880aac6a4df/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs#L33 https://github.com/aaubry/YamlDotNet/blob/485daaa7fa2a9fdc5863294ef580b880aac6a4df/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs#L63

And extract ReflectionPropertyDescriptor to a top-level class (see Additional context), or make protected.

Describe alternatives you've considered Copy/paste ReadablePropertiesTypeInspector and tweak the order.

Additional context WritablePropertiesTypeInspector can be also later refactored to share code with ReadablePropertiesTypeInspector. https://github.com/aaubry/YamlDotNet/blob/485daaa7fa2a9fdc5863294ef580b880aac6a4df/YamlDotNet/Serialization/TypeInspectors/WritablePropertiesTypeInspector.cs#L33

EdwardCooke commented 2 months ago

To keep this change simple I'll just remove sealed and make the type protected for now.

n9 commented 2 months ago

@EdwardCooke Thank you!

I realized that I still need to copy part of code to achieve ordering (as some members are private). I try to address this by PR: https://github.com/aaubry/YamlDotNet/pull/987