【发布时间】: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 的不同值?
【问题讨论】:
-
谢谢,但 .NetCore 3.1 似乎有所不同
标签: c# json .net-core json.net .net-core-3.1