When i do a GET request to retrieve my list of Persons the enums(Title) get converted into integers:
class Person {
public Title PersonTitle{ get; set;}
public string Name { get; set;}
}
enum Title {
STUDENT,
TEACHER,
DIRECTOR
}
Let's Say we have the following situation:
Person first = new Person(){
PersonTitle = Title.STUDENT,
Name = "Dave"
}
this wil result in the following JSON:
[
{
"Name" : "Dave",
"PersonTitle" : 1,
}
]
How do I get the real value of the enum (STUDENT in this case) ?
CodePudding user response:
you can set the JsonConverterAttribute
class Person {
[JsonConverter(typeof(StringEnumConverter))]
public Title PersonTitle{ get; set;}
public string Name { get; set;}
}
CodePudding user response:
You need to add the adequate converter to the JSON serializer.
System.Text.Json
:
- Use JsonStringEnumConverter class.
Json.NET
:
- Use StringEnumConverter class.