ASP.NET Core 3.1 How can I get the controller to use System.Text.Json to serialize and deserialize

喜你入骨 提交于 2020-05-28 08:05:45

问题


I am converting from .NET CORE 2.2 to 3.1. Is it possible to force the controller to use System.Text.Json to serialize and deserialize any incoming request and response? Basically, when the request comes in I want to use System.Text.Json and when a response goes out I want to use System.Text.Json. If yes, how?

The reason for doing this is that Microsoft is really pushing this library as the replacement for Newtonsoft.Json as being so much safer and faster. But, I cannot seem to find any documentation on Microsoft's pages that reflects this. I find it hard that Microsoft would not update their code to utilize this new library.

UPDATE

I am unable to get System.Text.Json to bind the model by parsing with application/vnd.api+json - the model is NULL. It will only bind if I parse using application/json. This is problematic because the JSON:API specification requires application/vnd.api+json (see here: https://stackoverflow.com/a/61452011/4630376). I tried decorating the controller with [Consumes("application/vnd.api+json")], but that does not work.

How do I get System.Text.Json to bind models using application/vnd.api+json? My initial assumption in asking this question is that .NET Core 3.1 was not using System.Text.Json. Since no one has provided an answer, other than a few comments, I have opted to expand this question.

ChangePassword Model:

using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace ABC.Model.Clients
{
    public class ChangePassword
    {   
        public ChangePassword() { }

        [Required]
        [JsonPropertyName("old-password")]
        public string OldPassword { get; set; }

        [Required]
        [JsonPropertyName("new-password")]
        public string NewPassword { get; set; }

        [Required]
        [JsonPropertyName("confirm-password")]
        public string ConfirmPassword { get; set; }
    }
}

Postman request:

{ 
    "data": {
        "attributes": {
            "old-password" : "**********",
            "new-password" : "**********",
            "confirm-password" : "**********"
        },
        "type": "change-passwords"
    }
}

回答1:


.NET Core 3.0 by default uses System.Text.Json. Check also the official documentation article where this is described.

Regarding the application/vnd.api+json and the JSON:API support in .NET Core 3.0. I am not aware of any support for this in ASP.NET Core. This is a separate standard from plain web API. You cannot just add it as another type you accept and expect it to work.

I found a project on GitHub JSON API .Net Core that provides a framework for building JSON:API compliant web API services. The version for .NET Core 3.0 is at the moment still in Alpha and currently uses Newtonsoft.Json which is not what you want. You can look at the source to see how to handle such requests and maybe build your own solution based on it. Or join the project and help them to make also a System.Text.Json version.




回答2:


You need to add application/vnd.api+json media type for the SupportedMediaTypes of SystemTextJsonInputFormatter.

services.AddMvc(options =>
{
    var formatter = options.InputFormatters.OfType<SystemTextJsonInputFormatter>().FirstOrDefault();
    formatter.SupportedMediaTypes.Add("application/vnd.api+json");
});


来源:https://stackoverflow.com/questions/61452081/asp-net-core-3-1-how-can-i-get-the-controller-to-use-system-text-json-to-seriali

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!