Add Newtonsoft (JSON.NET) serialization and deserialization in ASP.NET Core

Add Newtonsoft Json support in ASPNET Core JSONNET in net core

In this article, we shall see how to Add Newtonsoft Json support in ASP.NET Core.

We will see how to use Newtonsoft JSON serializer as a default serializer in ASP.NET Core 3.1 or 6.0 and above version-based applications.

Today in this article, we will cover below aspects,

ASP.NET Core 3.0* onwards Microsoft has removed the dependency on JSON.NET.

So there is no default Newtonsoft JSON-based serialization and deserialization available.

ASP.NET Core uses its own JSON serializer i.e ‘System.Text.Json which is lightweight and very efficient.

The new JSON serializer is still evolving and new features are getting added.

However, the new JSON serializer is compact and not as mature as Newtonsoft.

Ex. ReferenceLoopHandling is currently not supported in the System.Text.Json serializer.

This feature will be supported in the next new version of .NET Core in the future.

It’s recommended to use a lightweight and highly efficient System.Text.Json as a serializer and overcome the challenges using some workaround or create a custom converter.

But however, if you still would like to continue to use JSON.NET as the default serializer, kindly follow the below steps to achieve that.

Add a reference to the NewtonsoftJson Nuget package

ASP.NET Core 3.0 and above Newtonsoft JSON is available as NuGet Package.

Please add a reference to the NewtonsoftJson Nuget package as below.

Using PMC

PM> Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.2

Or

Note: Please use the latest available version

Using Visual Studio

Add NewtonsoftJson in ConfigureServices .NET Core 3.1 and above

If using .NET Core 3.1 above .NET template, Please add NewtonsoftJson in the ConfigureServices method as below,

Add Newtonsoft Json ASPNET Core

AddNewtonsoftJson extension method helps to specify any additional Json options or settings including as input and output formats and returns IMvcBuilder configured with the settings.

Example: Below Code adds ReferenceLoopHandling as below,

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(options =>
            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
            services.AddScoped<IEmployeeRepository, EmployeeRepository>();
            services.AddDbContext<EmployeeContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("EmployeeDB"),
                 sqlServerOptionsAction: sqlOptions =>
                 {
                     sqlOptions.EnableRetryOnFailure();
                 });
            });
        }

Add NewtonsoftJson in ConfigureServices .NET Core 2.2 or 3.0 and below

Similarly, if you have IRouter-based routing enabled for API using AddMVC (While migrating .NET Core 2.2 to 3.* version) using EnableEndpointRouting set as false you will get the same error.

In a similar way, you can use MvcNewtonsoftJsonOptions to specify any custom option and return it.

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddNewtonsoftJson();
        }

Example:

Add Newtonsoft Json in ASPNET Core

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Happy Coding!

Summary

Today in this article we learned how to add and use Newtonsoft JSON as the default serializer in .NET Core applications. ASP.NET Core uses its own JSON serializer i.e ‘System.Text.Json‘ but this package is currently available with fewer features.

Hence for any limitations or advanced requirements please use the above techniques to add support for NewtonSoft.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.