I am trying to put a list of enums into appsettings.json that will be iterated through at one point in the code. The problem is that if I put a null value into the list in the json file, it does not populate a null value into the list when the Settings are loaded from the file.
minimum code example:
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var builder = new ConfigurationBuilder().
AddJsonFile($"appsettings.json");
var config = builder.Build();
var test = config.GetRequiredSection("Settings").Get<Settings>();
test.EnumList.Add(null);
}
}
class Settings
{
public enum EnumValues
{
Value1,
Value2
}
public List<EnumValues?> EnumList { get; set; }
}
json file:
{
"Settings": {
"EnumList" : [null, "Value1"]
}
}
The test.EnumList.Add(null) will predictably and correctly add a null to the list, however, there will be no null in the list before that line, despite being in the json file. I would like to actually be able to put a null in the json file and have it show up in list.
How can I do this? I've hunted for a bit and I've come up with nothing.


JsonOptionsin yourstartupclass? Are you using Newtonsoft?