I have a list of countries, and need to convert "DE" to CountryCode.DE. But the following code does not work. Is there a simple way to go from string to enum in this case?
using System.Text.Json;
using System.Text.Json.Serialization;
class Program
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum CountryCodes
{
[Display(Name = "Japan", Description = "392")]
JP,
[Display(Name = "Germany", Description = "276")]
DE,
}
static void Main()
{
var code = JsonSerializer.Deserialize<CountryCodes>("DE");
}
}