Home > database >  Use Json.Net instead of Syste.Text:Json in .NET 6
Use Json.Net instead of Syste.Text:Json in .NET 6

Time:01-14

We want to use Json.Net instead System.Text.Json in our project, mainly because we must use the TypeNameHandling function.

The app has the following:

  • It's a console app.
  • It uses Dependency Injection.

Question: how can we tell the runtime to use Json.Net in serialization/deserialization instead of System.Text.Json?

Thank you.

CodePudding user response:

You have to insert it in your project from Nuget Package manager. There is no obstacle to install in a Console application. For dependency injection you can follow directions in this link.

CodePudding user response:

you have to add to your config something like this

using Newtonsoft.Json.Serialization;

builder.Services.AddControllersWithViews(options => 
             options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true)
            .AddNewtonsoftJson(options =>
            options.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver());

or if it is API

builder.Services.AddControllers(options => 
             options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true)
            .AddNewtonsoftJson(options =>
            options.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver());
  •  Tags:  
  • Related