3

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.

vivek86
  • 707
  • 9
  • 18
  • do you need specific functionality from newtonsoft.json? otherwise i'd suggest just migrating to system.text.json anyway: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-7-0 – Zimano Dec 06 '22 at 12:25
  • Its not about functionality, its about the effort to migrate everything from `Newtonsoft.Json` to `System.Text.Json`. – vivek86 Dec 09 '22 at 21:28
  • @vivek86 Did you find a solution for using Newtonsoft.Json in an isolated process? – Chai Aug 02 '23 at 09:41
  • @Chai unfortunately i couldnt find a way. So i decided to brute force it i.e. read the json string as a plain string and then use the conversion manually via newtonsoft by calling JsonConvert.Serialize<>(). i could not find a way to force Azure Function Apps to use Newtonsoft instead of System.Text.Json via DI so that serialization and deserialization could happen automatically. – vivek86 Aug 02 '23 at 15:22

1 Answers1

0
  • AFAIK, Services.AddMvcCore().AddJsonOptions(...) is for System.Text.Json whereas builder.services.AddMvcCore().AddNewtonsoftJson(options => can be used for Newtonsoft.json.

  • If you want to use Newtonsoft, add Microsoft.AspNetCore.Mvc.NewtonsoftJson as a dependency from NuGet which contains AddNewtonsoftJson() extension.

dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 7.0.0

enter image description here

  • Thanks to @Nate1zn, I found how to use Newtonsoft.Json in Azure Function apps, you can add a startup to your function project as below:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection

[assembly: FunctionsStartup(typeof(NewtonsoftjsonfunctionApp.functionsample))]
namespace NewtonsoftjsonfunctionApp
public class functionsample : FunctionsStartup
{
  public override void Configure(IFunctionsHostBuilder builder)
  {
    builder.services.AddMvcCore().AddNewtonsoftJson(options => 
    {
      options.SerializerSettings.//json settings
    }
  }
}
Pravallika KV
  • 2,415
  • 2
  • 2
  • 7
  • 1
    I tried that already and unfortunately that did not work. Moreover I am running Azure Functions in an Isolated-Process and the startup is different to that of an In-Process Azure Function. – vivek86 Dec 09 '22 at 21:26