gpt4 book ai didi

c# - 序列化和反序列化的 NullValueHandling 的不同值

转载 作者:行者123 更新时间:2023-12-04 04:01:05 24 4
gpt4 key购买 nike

我有一个 .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());
});
我已经把这一行忽略反序列化的空 json 值:
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
但我注意到它也忽略了序列化的空值(当使用 Json 类的方法 Microsoft.AspNetCore.Mvc.Controller 时),但我不想要这种行为。
有没有办法指定 NullValueHandling 的不同值用于序列化和反序列化?

最佳答案

最后我选择了这个解决方案:
我做了一个 BaseController继承自 Microsoft.AspNetCore.Mvc.Controller 的类.我从这个 BaseController 继承了我的每个 Controller 类(class)。
在这个类中,我覆盖了 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<MvcNewtonsoftJsonOptions>我能够恢复在启动时初始化的序列化程序设置。

编辑
我注意到值的变化 _jsonSerializerSettings.NullValueHandling = NullValueHandling.Include;还要更改 init 序列化程序设置。
所以我做了一个扩展方法来复制序列化器设置的所有数据,目的是更新新设置:
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;
}

关于c# - 序列化和反序列化的 NullValueHandling 的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63054916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com