Issue with Custom Serialization Using System.Text.Json in .NET 7 for Enums with Flags Attribute
I'm trying to implement I've been banging my head against this for hours... I'm currently facing an issue with serializing an enum that has the `Flags` attribute using `System.Text.Json` in .NET 7. My enum looks like this: ```csharp [Flags] enum MyFlags { None = 0, OptionA = 1, OptionB = 2, OptionC = 4 } ``` I have a class that uses this enum as a property: ```csharp public class MySettings { public MyFlags SelectedOptions { get; set; } } ``` When I serialize an instance of `MySettings`, I'm expecting a string representation of the selected flags. For example, if `SelectedOptions` is set to `OptionA | OptionB`, the expected output should be something like `"OptionA, OptionB"`. However, I am getting the output as a number instead, which is `3` (the sum of the flag values). Iβve tried using a custom converter to handle the serialization, but it doesnβt seem to work as expected: ```csharp public class MyFlagsConverter : JsonConverter<MyFlags> { public override MyFlags Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return (MyFlags)Enum.Parse(typeof(MyFlags), reader.GetString()); } public override void Write(Utf8JsonWriter writer, MyFlags value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString()); // This doesn't give the desired output } } ``` I registered the converter in my serialization options: ```csharp var options = new JsonSerializerOptions(); options.Converters.Add(new MyFlagsConverter()); ``` Despite this, the output remains a numeric value. I suspect I might be missing something in how Iβm implementing the converter or registering the serialization options. Can anyone provide guidance on how to ensure that the flags are serialized as a string representation instead of their underlying integer value? This is part of a larger API I'm building. Thanks in advance! I'm developing on Windows 11 with C#. Am I missing something obvious? Thanks, I really appreciate it!