Using Newtonsoft.Json In .NET Core 3+ Projects

Recently I’ve been working a lot in .NET Core 3.0 and 3.1 projects. Both upgrading existing 2.2 projects and a couple of new greenfields projects. The thing that I’ve had to do in each and every one is switch from using the new System.Text.Json package back to using Newtonsoft.Json.

In almost all of them I’ve actually tried to keep going with System.Text.Json, but in the existing projects I haven’t had time to switch out things like custom JsonConverters or Newtonsoft.Json specific attributes on my models.

In new projects, I always get to the point where I just know how to do it in Newtonsoft. And as much as I want to try this shiny new thing, I have my own deadlines which don’t quite allow me to fiddle about with new toys.

So if you’re in the same boat as me and just need to get something out the door. The first thing you need is to install the following Nuget package :

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Then you need to add a specific call to your IMVCBuilder. This will differ depending on how you have set up your project. If you are migrating from an existing project you’ll have a call to “AddMvc()” which you can then tack onto it like so :

services.AddMvc().AddNewtonsoftJson();

However in new .NET Core 3+ projects, you have a different set of calls replace MVC. So you’ll probably have one of the following :

services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();

If this is an API you will likely have AddControllers, but depending on your project setup you could have the others also. Tacking on AddNewtonsoftJson()  to the end means it will “revert” back to using Newtonsoft over System.Text.Json

 

ENJOY THIS POST?
Join over 3.000 subscribers who are receiving our weekly post digest, a roundup of this weeks blog posts.
We hate spam. Your email address will not be sold or shared with anyone else.

10 comments

  1. In .NET Core 3+ Projects: Newtonsoft.Json is better than built-in JsonConverter (De/Serialize). I get an error when use built-in Json Serialize with null string (no error with json.net). But I am still looking for a faster Json De/Serialize

    1. I’m about to do a post with a few gotchas that come with switching to the built in JSONConverter. Even things like by default, if you use the JSON Serializer outside an ASP.NET Core environment (For example in a console app to read a json file), it’s case sensitive. Or it doesn’t handle attributes to say “ignore if null” etc. There are quite a few things that are really really annoying.

    2. I’m not worried over speed. they all always fret over speed, give us their undone baby products.
      Yes it is faster, and it should be way faster than what you see. the fact is, when you write 10 line of code to do something obvious it is way faster than when you write 100 for handling many more issues. and those old good codes and projects already handled almost everything, but for sure they become slow. and then someone else with name and power give us another template, say it’s better, it’s a lot faster, but it do nothing 😀

      I don’t want to say Core its bad, it implemented with more of new day things in mind, but It’s not about speed, and I sometime miss a lot of things which worked but now doesn’t, and I have no extra time to get them work again.

  2. We are migrating API from Core 2.2 to Core 3.1 and we do have only services.mvc() method.
    After that we used AddNewtonsoftJson() but it didn’t take changes and we are not able to use newtonsoft.json functionality like ignorenull value .
    Please assist us with that. Thanks.

  3. Thank you, adding
    services.AddMvc().AddNewtonsoftJson();
    fixed a problem where the ShouldSerializeX functions were not being called.

  4. After upgrading from 2.2 to 3.1 JSON strigify could not handle cases with “new” property declaration. It was complaining: “The JSON property name for collides with another property”.
    After adding services.AddControllers().AddNewtonsoftJson() – resolved the problem. Thanks a lot!

  5. Thank you for posting the three closely related topics together: the asp.net core serializer override, the swagger serializer override, and the ReferenceLoop handling override. I love .net core and the new asp.net core web api, but sheesh, why is it every five years we need to re-learn how to peddle the bike!? The documentation is only five times more volumnous now…

  6. Hello,

    I have this “services.AddControllers().AddNewtonsoftJson()” but still getting “The JSON property name for collides with another property”. this error

Leave a Reply

Your email address will not be published. Required fields are marked *