ASP.NETCore中Serializer的不同实现”
创始人
2024-09-18 16:00:21
0

ASP.NET Core默认提供了三种Serializer的实现:JsonSerializer、XmlSerializer和Formatters。它们都是从抽象类InputFormatter和OutputFormatter派生而来。我们可以根据需要自定义Serializer。

下面是一个使用JsonSerializer自定义Serializer的示例:

首先,创建一个实现InputFormatter和OutputFormatter的类CustomJsonSerializer。

using Microsoft.AspNetCore.Mvc.Formatters;
using Newtonsoft.Json;

public class CustomJsonSerializer : InputFormatter, IOutputFormatter
{
    private readonly JsonSerializerSettings _settings;

    public CustomJsonSerializer(JsonSerializerSettings settings)
    {
        _settings = settings;
        SupportedMediaTypes.Add("application/json");
    }

    protected override bool CanReadType(Type type)
    {
        return true; // 可以处理任何类型
    }

    protected override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
    {
        var streamReader = new StreamReader(context.HttpContext.Request.Body);
        var requestBody = streamReader.ReadToEndAsync().Result;
        var model = JsonConvert.DeserializeObject(requestBody, context.ModelType, _settings);
        return Task.FromResult(model);
    }

    public override void WriteResponseHeaders(OutputFormatterWriteContext context)
    {
        context.ContentType = "application/json";
    }

    public async Task WriteAsync(OutputFormatterWriteContext context)
    {
        var response = context.Object;
        if (response == null)
        {
            return;
        }
        var jsonString = JsonConvert.SerializeObject(response, _settings);
        await context.HttpContext.Response.WriteAsync(jsonString);
    }
}

然后,在Startup类中注册CustomJsonSerializer。

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.InputFormatters.Insert(0, new CustomJsonSerializer(new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
            options.OutputFormatters.Insert(0, new CustomJsonSerializer(new JsonSerializerSettings
            {

相关内容

热门资讯

9分钟实锤!德州之星有外挂(德... 9分钟实锤!德州之星有外挂(德州nzt)其实是有挂(详细辅助可靠技巧)1、德州之星有外挂系统规律教程...
第八分钟实锤!德扑ai智能机器... 第八分钟实锤!德扑ai智能机器人(德州nzt)总是存在有挂(详细辅助力荐教程)1、让任何用户在无需德...
第9分钟实锤!德扑之星辅助器购... 第9分钟实锤!德扑之星辅助器购买(德扑之星)好像是真的有挂(详细辅助2025新版总结)进入游戏-大厅...
第十分钟实锤!德州ai机器人(... 第十分钟实锤!德州ai机器人(德州ai)确实是真的有挂(详细辅助实用技巧);1、首先打开德州ai机器...
第一分钟实锤!德州之星有外挂(... 第一分钟实锤!德州之星有外挂(德州wpk)总是是真的有挂(详细辅助微扑克教程)运德州之星有外挂辅助工...
第八分钟实锤!德州之星辅助挂(... 第八分钟实锤!德州之星辅助挂(德州俱乐部)一直有挂(详细辅助线上教程)1、这是跨平台的德州之星辅助挂...
七分钟实锤!德扑ai助手(线上... 七分钟实锤!德扑ai助手(线上wpk德州)果然真的有挂(详细辅助必胜教程)1、完成德扑ai助手的残局...
9分钟实锤!德扑之星有猫腻(云... 9分钟实锤!德扑之星有猫腻(云扑克德州)果然真的有挂(详细辅助实用技巧);1、任何德扑之星有猫腻ai...
第八分钟实锤!德州ai辅助(德... 第八分钟实锤!德州ai辅助(德州ai)都是是有挂(详细辅助AI教程);德州ai辅助是一种具有地方特色...
五分钟实锤!德扑ai助手(德州... 五分钟实锤!德扑ai助手(德州app)果然真的是有挂(详细辅助技巧教程);1、五分钟实锤!德扑ai助...