Odata Deserialize in json with Enum DisplayName or EnumMember Value

馋奶兔 提交于 2021-01-28 09:07:44

问题


I have an enum declared in c#. Having 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 getting enum identifier as a string. which isn't expected.

{
   "Id" : 10001,
   "Book_Name":"JohnWick" // as desclared in enum  against value 
},
{
   "Id" : 10001,
   "Book_Name": "JohnSnow" // as desclared in enum  against value 
},
{
   "Id" : 10001,
   "Book_Name": "JackSparrow" // as desclared in enum  against value 
}

来源:https://stackoverflow.com/questions/62150650/odata-deserialize-in-json-with-enum-displayname-or-enummember-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!