.NET Core 3 is here and that means upgrade time!
It’s great news that Microsoft have added the System.Text.Json namespace so that we no longer have to reach for Newtonsoft.Json
as the first thing we do after File > New Project.
However, if you don’t feel ready, or there are few quirks and edge cases that you find when upgrading, you can configure your application to use Newtonsoft.Json
instead!
Using Newtonsoft.Json
It’s really easy to set up. Simply, adding a package reference and then adding it to the service collection in your Startup.cs
file…
Packagage Reference
1 | <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" /> |
Startup.cs
1 | public void ConfigureServices(IServiceCollection services) |
You can also configure the serialization options…
1 | public void ConfigureServices(IServiceCollection services) |
Conclusion
That’s it! It’s really that simple.
If you’re wanting to upgrade your existing applications, but are unsure of the side affects or edge cases of using the new System.Text.Json
namespace, you can really easily configure your application to use the perhaps more familiar Newtonsoft.Json
library.