ASP.Net Core 2.2-输入和输出的单独的序列化程序设置

时间:2019-11-29 15:19:26

标签: c# .net asp.net-core json.net asp.net-core-2.2

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

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

NullValueHandling

然后输入JSON:public class SomeEntity { public int Id { get; set; } public int? ParentId { get; set; } public string Name { get; set; } } 。对于{ id: null, parentId: null, name: "test" },反序列化失败,但对于NullValueHandling.Include有效。

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

NullValueHandling.Ignore

它与new SomeEntity { Id = 1, ParentId = null, Name = "test" } NullValueHandling.Include保持为空,但通过{ id: 1, parentId: null, name: "test" }NullValueHandling.Ignore擦除。

我需要实现输入为“忽略”场景和输出为“包含”场景。

2 个答案:

答案 0 :(得分:1)

最后找到了解决方法:https://github.com/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>());

在服务提供商配置中

答案 1 :(得分:0)

我将使用Newtonsoft.Json的JsonSerializerSettings类来定义两个实例mySerializeSettingmyDeserializeSettings

using Newtonsoft.Json;
using Microsoft.Rest.Serialization;

string requestContent = SafeJsonConvert.SerializeObject(value, mySerializationSettings);

要反序列化:

var result = SafeJsonConvert.DeserializeObject<myclass>(input, myDeserializeSettings);

要了解如何设置序列化设置,请参见https://www.newtonsoft.com/json/help/html/SerializationSettings.htm