I currently use Azure App Configuration to store all the config data. When i read the data, i would like to use Newtonsoft.Json instead of the default System.Text.Json to correctly transform my data to custom types.
Unfortunately, i cannot find a way to tell the HostBuilder in the Program.cs file to use Newtonsoft.Json instead of the System.Text.Json. All my converters are written using Newtonsoft.Json and i really would like to not migrate them all to System.Text.Json.
As an example:
public Enum SampleEnum
{
[Description("Sample")] SampleAbc
}
public class SampleConfig
{
public SampleEnum ConfigEnumA {get; set;}
}
// configuration is the IConfiguration object from Microsoft.Extensions.Configuration
var sampleConfig = new SampleConfig()
configuration.GetSection("section").bind(sampleConfig);
The Description value Sample is what comes from Azure App Configuration as a string and ideally my current Newtonsoft.Json converters should transform it to SampleAbc enum value when binding the object SampleConfig. This allows me to be type safe in code.
This means that ConfigEnumA should have a value of SampleEnum.SampleAbc instead of Sample
I have already tried using but it did not work although i am working with HTTP Triggers and not a web application
var hostBuilder = new HostBuilder().ConfigureServices(services =>
{
services.AddMvc().AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters = <my-custom-converters>
}
)
});
if there is no way to use Newtonsoft.Json in Azure Function apps that arent Mvc applications, then can someone help with achieving the same behaviour with System.Text.Json?
Thanks for the help. Cheers.
