Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Newtonsoft.Json as default in Asp.net Core Web Api?

I am new to ASP.Net Web Api Core. I have been using ASP.Net MVC for past few years and I always have written an ActionFilter and used JSON.Net for Serializing data into JSON. So, in that way I replaced Microsoft's JavaScript Serializer (which is slower than JSON.Net) with JSON.Net (it is claimed to be 400% faster).

How to do all this in ASP.Net Web Api Core? Where to change the default formattor?

Note: Please feel free to ask if you have any questions.

Thanks

like image 999
Super Coder Avatar asked Feb 17 '17 06:02

Super Coder


People also ask

Is Newtonsoft JSON compatible with .NET Core?

Text. Json library is included in the runtime for . NET Core 3.1 and later versions. For other target frameworks, install the System.

Is JSON net the same as Newtonsoft?

Thanks. Json.NET vs Newtonsoft. Json are the same thing. You must be trying to use the same code with different versions of Json.NET.

How do I use NewtonSoft JSON serializer with ASP NET Core?

By default, ASP.NET Core uses System.Text.Json for JSON serialization. If you want to use Newtonsoft instead, you can add the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, then call AddNewtonsoftJson () in Startup.ConfigureServices () like this: In this article, I’ll show how to configure the Newtonsoft serializer options.

Does ASP NET Core use JSON?

Turns out .Net Core uses it out of the box. Refer to this answer for how to use it natively. UPDATE: From the answer linked above: This is only true for ASP.NET Core 1.0 to 2.2. ASP.NET Core 3.0 removes the dependency on JSON.NET and uses it's own JSON serializer.

What is the default JSON formatter in ASP NET Core?

In ASP.NET Core 3.0 or later, the default JSON formatters are based on System.Text.Json. Support for Newtonsoft.Json -based formatters and features is available by installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and configuring it in Startup.ConfigureServices.

How to get NewtonSoft JSON to work?

You don't need to do anything special to get Newtonsoft.JSON to work. Simply install it through NuGet (or manually) and you're good to go. You will however have to go through your code and replace any code you wrote to work with JSON before installing it. EDIT: You dont even have to do that! Turns out .Net Core uses it out of the box.


2 Answers

In .NET Core 3.0+ include the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson and then replace

services.AddControllers();  

in ConfigureServices with

services.AddControllers().AddNewtonsoftJson();  

This is a pre-release NuGet package in .NET Core 3.0 but a full release package in .NET Core 3.1.

I came across this myself, but I've found that the same answer with some additional info is in this SO question and answer.

Edit: As a useful update: code with the call to AddNewtonsoftJson() will compile and run even without installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. If you do that, it runs with both converters installed, but defaulting to the System.Text.Json converter which you presumably don't want since you're reading this answer. So you must remember to install the NuGet package for this to work properly (and remember to re-install it if you ever clear down and redo your NuGet dependencies).

like image 85
MikeBeaton Avatar answered Sep 23 '22 21:09

MikeBeaton


ASP.NET Core already uses JSON.NET as JavaScriptSerializer isn't implemented/ported to .NET Core.

Microsoft.AspNetCore.Mvc depends on Microsoft.AspNetCore.Formatter.Json which depends on Microsoft.AspNetCore.JsonPatch, which depends on Newtonsoft.Json (see source).

Update

This is only true for ASP.NET Core 1.0 to 2.2. ASP.NET Core 3.0 removes the dependency on JSON.NET and uses it's own JSON serializer.

like image 30
Tseng Avatar answered Sep 20 '22 21:09

Tseng