问题
I normally use ShouldSerialize
to exclude properties that have no data such as array but now, it does not appear to be triggered when I'm only using JSON serializer in .NET Core 3
. It was being triggered when using NewtonSoft
but I've removed it from my project since it no longer appears to be required.
For example:
private ICollection<UserDto> _users;
public ICollection<UserDto> Users
{
get => this._users ?? (this._users = new HashSet<UserDto>());
set => this._users = value;
}
public bool ShouldSerializeUsers()
{
return this._users?.Count > 0;
}
Any ideas why ShouldSerializeUsers is not being triggered?
I've seen other answers where you can use:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling =
NullValueHandling.Ignore;
});
}
But I'd like to know if there is another way to handle this as I'm not using .AddMvc
Thanks.
回答1:
The reason that your ShouldSerialize
is not triggered in ASP.NET Core 3.0 is that, in this and subsequent versions of ASP.NET, a different JSON serializer is being used by default, namely System.Text.Json.JsonSerializer. See:
- Try the new System.Text.Json APIs.
- Breaking changes to Microsoft.AspNetCore.App in 3.0 #325.
- The future of JSON in .NET Core 3.0 #90.
Unfortunately as of .NET Core 3.1 this serializer does not support the ShouldSerializeXXX()
pattern; if it did it would be somewhere in JsonSerializer.Write.HandleObject.cs -- but it's not. The following issues track requests for conditional serialization:
.net core 3.0 system.text.json option for ignoring property at runtime like newstonsoft DefaultContractResolver #42043.
System.Text.Json option to ignore default values in serialization & deserialization #779.
To restore ShouldSerialize
functionality, you can revert back to using Newtonsoft as shown in this answer to Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0? by poke, and also Add Newtonsoft.Json-based JSON format support:
- Install Microsoft.AspNetCore.Mvc.NewtonsoftJson.
Then call
AddNewtonsoftJson()
inStartup.ConfigureServices
:public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddNewtonsoftJson(); }
来源:https://stackoverflow.com/questions/59507818/shouldserialize-method-is-not-triggered-in-net-core-3