ASP.Net Core 2.2

编程入门 行业动态 更新时间:2024-10-27 14:33:16
本文介绍了ASP.Net Core 2.2-用于输入和输出的单独的序列化程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

ASP.Net Core 2.2允许使用MvcJsonOptions.SerializerSettings属性设置序列化程序设置.问题在于它同时影响输入和输出.有没有办法为输入(反序列化)和输出(序列化)提供单独的选项?特别是,我需要为NullValueHandling设置设置不同的行为:反序列化客户端json时,对不可为空的字段忽略null错误,而对结果进行序列化时,为已定义的模型字段保留null.

ASP.Net Core 2.2 allows to set serializer settings using MvcJsonOptions.SerializerSettings property. The problem is that it affects both input and output. Is there a way to have separate options for input (deserialization) and output (serialization)? In particular, I need to set a different behavior for NullValueHandling settings: ignore null errors for non-nullable fields when deserializing client json but keep nulls for defined model fields when serializing the result.

例如,我有一个用于请求的C#模型:

For example, I have a C# model for request:

public class SomeEntity { public int Id { get; set; } public int? ParentId { get; set; } public string Name { get; set; } }

,然后输入JSON:{ id: null, parentId: null, name: "test" }.反序列化对于NullValueHandling.Include失败,但对于NullValueHandling.Ignore有效.

And input JSON: { id: null, parentId: null, name: "test" }. The deserialization fails for NullValueHandling.Include but works for NullValueHandling.Ignore.

但是当我序列化这样的实体

But when I serialize an entity like this one

new SomeEntity { Id = 1, ParentId = null, Name = "test" }

使用NullValueHandling.Include:{ id: 1, parentId: null, name: "test" }保留为空,但是使用NullValueHandling.Ignore:{ id: 1, name: "test" }删除.

It keeps null with NullValueHandling.Include: { id: 1, parentId: null, name: "test" } but erases it with NullValueHandling.Ignore: { id: 1, name: "test" }.

我需要实现输入为忽略"场景和输出为包含"场景.

I need to achieve the "Ignore" scenario for input and "Include" for output.

推荐答案

最后找到此解决方法: github/aspnet/Mvc/issues/4562#issuecomment-226100352

Finally found this workaround: github/aspnet/Mvc/issues/4562#issuecomment-226100352

public class CustomSerializerSettingsSetup : IConfigureOptions<MvcOptions> { private readonly ILoggerFactory _loggerFactory; private readonly ArrayPool<char> _charPool; private readonly ObjectPoolProvider _objectPoolProvider; public CustomSerializerSettingsSetup( ILoggerFactory loggerFactory, ArrayPool<char> charPool, ObjectPoolProvider objectPoolProvider) { _loggerFactory = loggerFactory; _charPool = charPool; _objectPoolProvider = objectPoolProvider; } public void Configure(MvcOptions options) { options.OutputFormatters.RemoveType<JsonOutputFormatter>(); options.InputFormatters.RemoveType<JsonInputFormatter>(); options.InputFormatters.RemoveType<JsonPatchInputFormatter>(); var outputSettings = new JsonSerializerSettings(); options.OutputFormatters.Add(new JsonOutputFormatter(outputSettings, _charPool)); var inputSettings = new JsonSerializerSettings(); var jsonInputLogger = _loggerFactory.CreateLogger<JsonInputFormatter>(); options.InputFormatters.Add(new JsonInputFormatter( jsonInputLogger, inputSettings, _charPool, _objectPoolProvider)); var jsonInputPatchLogger = _loggerFactory.CreateLogger<JsonPatchInputFormatter>(); options.InputFormatters.Add(new JsonPatchInputFormatter( jsonInputPatchLogger, inputSettings, _charPool, _objectPoolProvider)); } }

services.TryAddEnumerable( ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, CustomSerializerSettingsSetup>());

在服务提供商配置中

更多推荐

ASP.Net Core 2.2

本文发布于:2023-11-13 23:28:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!