You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an appsettings.json file where a property is defined as "AccountType": "Default".
There's a related SmartEnum defined as public static readonly AccountType Default = new(nameof(Default), 1);.
Deserialization is done via the ConfigurationBuilder:
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
.Build();
var options = config.Get<ConfigOptions>()
?? throw new ApplicationException("Configuration appsettings files not found");
The property is in a nested object and defined as:
[JsonConverter(typeof(SmartEnumNameConverter<AccountType, int>))]
public AccountType AccountType { get; set; }
I have tried deserializing with both name and value, but the property is always null.
I confirm that it works if it's manually deserialized
var configFile = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/appsettings.qa.json");
var jsonSerializerSettings = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
};
jsonSerializerSettings.Converters.Add(new SmartEnumNameConverter<AccountType, int>());
jsonSerializerSettings.Converters.Add(new SmartEnumNameConverter<TenantType, int>());
jsonSerializerSettings.Converters.Add(new SmartEnumNameConverter<UserRoles, int>());
jsonSerializerSettings.Converters.Add(new JsonStringEnumConverter());
var options = JsonSerializer.Deserialize<ConfigOptions>(configFile, jsonSerializerSettings);
The text was updated successfully, but these errors were encountered:
I have an
appsettings.json
file where a property is defined as"AccountType": "Default"
.There's a related SmartEnum defined as
public static readonly AccountType Default = new(nameof(Default), 1);
.Deserialization is done via the
ConfigurationBuilder
:The property is in a nested object and defined as:
I have tried deserializing with both name and value, but the property is always
null
.I confirm that it works if it's manually deserialized
The text was updated successfully, but these errors were encountered: