Click here to Skip to main content
14,366,827 members

JsonConverter Attribute in ASP.NET Core 3.0 Web API

Rate this:
5.00 (5 votes)
Please Sign up or sign in to vote.
5.00 (5 votes)
22 Oct 2019CPOL
ASP.NET Core 3.0 uses a built-in JSON converter from System.Text.Json so that JsonConverter attribute from Newtonsoft.Json does not work by default.

Introduction

In .NET Core 3.0, a built-in JSON converter is available in namespace System.Text.Json, and ASP.NET Core 3.0 uses this new JSON converter by default. Therefore, when you migrate your Web API from ASP.NET Core 2.x to 3.0, you need to replace JsonConverter attribute with System.Text.Json.Serialization.JsonConverter if you use it.

Migration to System.Text.Json

In ASP.NET Core 2.2 Web API, I had used Newtonsoft.Json.Converters.StringEnumConverter to convert enum values to the enumerator name at API response as shown below. In this case, the serialized JSON in the response looked like { "status": "Success", "message": "..." }.

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public enum ApiResponseStatus
{
    Success,
    Failure
}

public abstract class ApiResponse

{
    public ApiResponseStatus Status { get; set; }
    public string Message { get; set; }
}

[Route("api/[Controller]")]
[ApiController]
public class MessagingController : ControllerBase
{
    [HttpPost("[action]")]
    public async Task<ApiResponse> Messages([FromBody] Message message)
    {
        return new ApiResponse
        {
            Status = ApiResponseStatus.Success,
            Message = "The POST request successfully completed."
        }
    }
}

When this code migrates to ASP.NET Core 3.0, it does not work. The serialized JSON turns to be { "status": 0, "message": "..." }. Let's replace the attribute with the attribute from System.Text.Json.

using System.Text.Json.Serialization;

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ApiResponseStatus
{
    Success,
    Failure
}

The attribute name is the same as Newtonsoft.Json so that you just need to replace the namespace to System.Text.Json.Serialization. For some reason, the converter equivalent to StringEnumConverter is named JsonStringEnumConverter in System.Text.Json namespace. With this modification, the API will work in ASP.NET Core 3.0 as it does in 2.2.

Another Option: Keep using Newtonsoft.Json

In my case, there was an equivalent converter available, and the code migration was easy. However, you may want to keep using Nowtonsoft.Json, for example, if no equivalent converters are available or you have your own custom converter. In such a case, another option is to keep using Newtonsoft.Json.

Configuration to do this is quite simple. Newtonsoft.Json is now available from Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and it also has an extension method to configure it in a single method call. Add the NuGet package to your project, and call the extension method, AddNewtonsoftJson, after an MVC service registration method, which is AddControllers, AddControllersWithViews or AddRazorPages.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews()
            .AddNewtonsoftJson();
    }
}

Having your Startup class like this, JsonConvert attribute from Newtonsoft.Json can still work at API response in ASP.NET Core 3.0 Web API.

History

  • 2019-10-23: Published the first edit

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

YasIkeda
New Zealand New Zealand
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Satukan23-Oct-19 15:25
memberSatukan23-Oct-19 15:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 22 Oct 2019

Stats

4K views
2 bookmarked