READ BEFORE MARK AS DUPLICATED
The "JavaScriptSerializer - JSON serialization of enum as string" question don't answer my question!
I'm trying to serialize an enum but I want the value of the enum present in its description attribute to be serialized and not its value
Important to note that I don't want to use Newtonsoft
Example
using System.Text.Json;
using System.Text.Json.Serialization;
public class MyClassExample
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public YesOrNo IsShipped { get;set; }
}
public enum YesOrNo {
[Description("Y")]
Yes,
[Description("N")]
No
}
And I use like that:
var myJson = JsonSerializer.Serialize(new MyClassExample () { IsShipped = YesOrNo.Y });
The return is:
{ "IsShipped": "Yes" } but I expected { "IsShipped": "Y" }
And the same to
var myObject = JsonSerializer.Deserialize<MyClassExample>("{ ""IsShipped"": ""Y"" }");
How can I use the value o enums attribute to serialize/deserialize?