'Serialize Enum as DisplayName or EnumMember Value
I have an enum declared in c#. I have a Display Name/ EnumMember value annotation and Student Class. I'm using asp.net core 2.2+ version with Odata.
[DataContract]
Public Class Library
{
public int Id {get;set;}
[DataMember(Name="Book_Name")]
public BookNameEnum BookName {get; set;}
}
[DataContract]
JsonConverter[typeof(StringEnumConverter)]
public enum BookNameEnum
{
[Display(Name ="John Wick"), EnumMember(value="John Wick")]
JohnWick = 1,
[Display(Name ="John Snow"), EnumMember(value="John Snow")]
JohnSnow = 2,
[Display(Name ="Jack Sparrow"), EnumMember(value="Jack Sparrow")]
JackSparrow = 3,
[Display(Name ="Jorder M"), EnumMember(value="Jorder M")]
JorderM = 4
}
I want to achieve the desired result as below in my postman API call.
{
"Id" : 10001,
"Book_Name": "John Wick" /*Display Name or EnumMember Value*/
},
{
"Id" : 10001,
"Book_Name": "John Snow" /*Display Name or EnumMember Value*/
},
{
"Id" : 10001,
"Book_Name": "Jack Sparrow" /*Display Name or EnumMember Value*/
}
But currently, I'm getting the below result which is just the enum identifier as a string, which isn't expected.
{
"Id" : 10001,
"Book_Name": "JohnWick" // as declared in enum against value
},
{
"Id" : 10001,
"Book_Name": "JohnSnow" // as declared in enum against value
},
{
"Id" : 10001,
"Book_Name": "JackSparrow" // as declared in enum against value
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|