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
发布评论