【问题标题】:Different value of NullValueHandling for serialize and deserialize用于序列化和反序列化的 NullValueHandling 的不同值
【发布时间】:2020-11-13 05:14:39
【问题描述】:

我有一个 .net core 3.1 应用程序。我使用库 json.net (newtonsoft) 来序列化或反序列化 json 。这是 newtonsoft 的应用设置:

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers(options =>
        {
            options.SuppressAsyncSuffixInActionNames = false;
        }).AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
            options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            options.SerializerSettings.Converters.Add(new GuidJsonConverter());
        });

我已经在反序列化时忽略了 null json 值:

options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

但我注意到它也忽略了序列化的 null 值(当使用 Microsoft.AspNetCore.Mvc.Controller 类的 Json 方法时),但我不想要这种行为。

有没有办法为序列化和反序列化指定 NullValueHandling 的不同值?

【问题讨论】:

标签: c# json .net-core json.net .net-core-3.1


【解决方案1】:

最后我选择了这个解决方案: 我创建了一个继承自Microsoft.AspNetCore.Mvc.ControllerBaseController 类。我从这个BaseController 类继承了我的每个控制器。 在这个类中,我重写了Microsoft.AspNetCore.Mvc.Controller.Json 方法:

    public class BaseController : Controller
    {
        private readonly JsonSerializerSettings _jsonSerializerSettings;

        public BaseController(IServiceProvider services)
        {
            IOptions<MvcNewtonsoftJsonOptions> newtonsoftOptions = services.GetService<IOptions<MvcNewtonsoftJsonOptions>>();
            _jsonSerializerSettings = newtonsoftOptions.Value.SerializerSettings;
            _jsonSerializerSettings.NullValueHandling = NullValueHandling.Include;
        }

        public override JsonResult Json(object data)
        {
            return Json(data, _jsonSerializerSettings);
        }

感谢IOptions&lt;MvcNewtonsoftJsonOptions&gt; 我能够恢复启动时初始化的序列化程序设置。


编辑

我注意到值_jsonSerializerSettings.NullValueHandling = NullValueHandling.Include; 的更改也会更改初始化序列化程序设置。 所以我做了一个扩展方法来复制序列化程序设置的所有数据,目的是更新新设置:

public CustomerAccountController(IServiceProvider services)
{
        IOptions<MvcNewtonsoftJsonOptions> newtonsoftOptions = services.GetService<IOptions<MvcNewtonsoftJsonOptions>>();
        _jsonSerializerSettings = newtonsoftOptions.Value.SerializerSettings.CloneJsonSerializerSettings();
        _jsonSerializerSettings.NullValueHandling = NullValueHandling.Include;
}
public static JsonSerializerSettings CloneJsonSerializerSettings(this JsonSerializerSettings settings)
{
    JsonSerializerSettings cloneSettings = new JsonSerializerSettings();
    cloneSettings.StringEscapeHandling = settings.StringEscapeHandling;
    cloneSettings.FloatParseHandling = settings.FloatParseHandling;
    cloneSettings.FloatFormatHandling = settings.FloatFormatHandling;
    cloneSettings.DateParseHandling = settings.DateParseHandling;
    cloneSettings.DateTimeZoneHandling = settings.DateTimeZoneHandling;
    cloneSettings.DateFormatHandling = settings.DateFormatHandling;
    cloneSettings.Formatting = settings.Formatting;
    cloneSettings.MaxDepth = settings.MaxDepth;
    cloneSettings.DateFormatString = settings.DateFormatString;
    cloneSettings.Context = settings.Context;
    cloneSettings.Error = settings.Error;
    cloneSettings.SerializationBinder = settings.SerializationBinder;
    cloneSettings.Binder = settings.Binder;
    cloneSettings.TraceWriter = settings.TraceWriter;
    cloneSettings.Culture = settings.Culture;
    cloneSettings.ReferenceResolverProvider = settings.ReferenceResolverProvider;
    cloneSettings.EqualityComparer = settings.EqualityComparer;
    cloneSettings.ContractResolver = settings.ContractResolver;
    cloneSettings.ConstructorHandling = settings.ConstructorHandling;
    cloneSettings.TypeNameAssemblyFormatHandling = settings.TypeNameAssemblyFormatHandling;
    cloneSettings.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
    cloneSettings.MetadataPropertyHandling = settings.MetadataPropertyHandling;
    cloneSettings.TypeNameHandling = settings.TypeNameHandling;
    cloneSettings.PreserveReferencesHandling = settings.PreserveReferencesHandling;
    cloneSettings.Converters = settings.Converters;
    cloneSettings.DefaultValueHandling = settings.DefaultValueHandling;
    cloneSettings.NullValueHandling = settings.NullValueHandling;
    cloneSettings.ObjectCreationHandling = settings.ObjectCreationHandling;
    cloneSettings.MissingMemberHandling = settings.MissingMemberHandling;
    cloneSettings.ReferenceLoopHandling = settings.ReferenceLoopHandling;
    cloneSettings.ReferenceResolver = settings.ReferenceResolver;
    cloneSettings.CheckAdditionalContent = settings.CheckAdditionalContent;

    return cloneSettings;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2014-07-02
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2019-12-24
    • 2021-05-25
    相关资源
    最近更新 更多