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
            {

相关内容

热门资讯

四分钟了解!哈局十三张,大赢家... 四分钟了解!哈局十三张,大赢家跑得快辅助,分享教程(有挂脚本)1、大赢家跑得快辅助系统规律教程、大赢...
8分钟了解!星星武汉麻将胡牌技... 8分钟了解!星星武汉麻将胡牌技巧,赣牌圈开挂是真的吗,科技教程(有挂揭秘)1、进入游戏-大厅左侧-新...
4分钟了解!菠萝德州app有挂... 4分钟了解!菠萝德州app有挂吗,新玉海楼茶苑吗,必胜教程(有挂神器)1、在菠萝德州app有挂吗ai...
6分钟了解!微友麻将,浙江游戏... 6分钟了解!微友麻将,浙江游戏大厅有猫腻吗,透视教程(有挂解说)亲,关键说明,浙江游戏大厅有猫腻吗赛...
一分钟了解!斗棋红中胡牌有没有... 一分钟了解!斗棋红中胡牌有没有什么规律,开心十三张有没有挂,2025版教程(有挂技巧);暗藏猫腻,小...
2分钟了解!琼崖海南麻将怎么提... 2分钟了解!琼崖海南麻将怎么提高胜率,福建天天开心王国辅助器,揭秘教程(有挂工具)一、琼崖海南麻将怎...
八分钟了解!乐乐游戏辅助器,众... 八分钟了解!乐乐游戏辅助器,众乐联盟有挂吗,可靠教程(有挂透视)1、全新机制【众乐联盟有挂吗软件透明...
一分钟了解!雀神麻将辅牌器购买... 一分钟了解!雀神麻将辅牌器购买,微信随意玩9人拼三张辅助器,实用技巧(有挂秘籍)1、用户打开应用后不...
二分钟了解!新华棋牌有没有挂,... 二分钟了解!新华棋牌有没有挂,打小闲川南棋牌为什么总是输,揭秘教程(有挂软件)暗藏猫腻,小编详细说明...
三分钟了解!汇友手游外 挂,边... 三分钟了解!汇友手游外 挂,边锋干瞪眼外挂效果,必备教程(有挂软件)进入游戏-大厅左侧-新手福利-激...