Microsoft.Extensions.Configuration
will deserialse enums stored in strings in JSON, which I find a great way to solve this problem.
As an example:
public enum Format
{
UNKNOWN = 0,
PNG,
JPEG
}
public class ImageOptions
{
/* List of supported file types */
public List<Format> SupportedFileTypes { get; set; }
}
/* Options config dict */
public static Dictionary<string, string> DefaultImageServiceConfigDict = new Dictionary<string, string>
{
/* Image Options NOTE: Enums stored as strings!! */
{"ImageServiceOptions:SupportedFileTypes:0", "png"},
{"ImageServiceOptions:SupportedFileTypes:1", "jpeg"},
};
/* Read options */
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(DefaultImageServiceConfigDict);
configuration = builder.Build();
/* Parses config, including enum deserialisation by name */
imageConfig = config.GetSection(nameof(ImageOptions)).TryGet<ImageServiceOptions();