2

For better or worse our codebase relies heavily on Newtonsoft.Json. There are various type converters etc. based on this framework and it is simply not worth the effort to rewrite them using some other JSON framework (if even possible).

We use appsettings.json (+ other custom files) to load settings into our applications.

Typically we used to configure this like:

configurationBuilder.AddJsonFile(pathToFile, ...);

Now, we need to use some of the converters we usually rely on when parsing regular JSON for the settings JSON as well. These are usually being automatically picked up by Newtonsoft.Json so we thought the solution would simply be to reference Microsoft.Extensions.Configuration.NewtonsoftJson and change to:

configurationBuilder.AddNewtonwoftJsonFile(pathToFile, ...);

However this does not appear to be the case. The converters are not being invoked when we call.

var someSettings = configurationSection.Get<SomeSettings>();

If we paste the same settings into a string and manually parse it the usual Newtonsoft.Json way. This works just fine.

Our conclusion is that either we are doing something wrong (hopefully) or the "binding" part where a configuration section is transferred into actual properties on the object is not part of the Newtonsoft.Json configuration extension.

Any suggestions?

jool
  • 1,491
  • 12
  • 14
  • Which file you have tested this on? Which ASP.NET Core/ .NET version are you using? – Guru Stron Jan 27 '22 at 13:45
  • .NET 5. Currently only tried it appsettings.json but I have no reason to suspect that any other file would work better. – jool Jan 27 '22 at 13:47
  • Also you can always implement [custom configuration provider](https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-configuration-provider) if you other attempts will not work. – Guru Stron Jan 27 '22 at 13:47
  • Right, well this only seems to be dealing with the actual fetching part of the configuration. I guess I need to look into the possibility to provide my own "binding". – jool Jan 27 '22 at 14:06
  • You can try this [link](https://stackoverflow.com/questions/49046847/how-can-i-add-a-custom-json-file-into-iconfiguration) – Xinran Shen Jan 28 '22 at 03:02

0 Answers0