I have managed to ignore the null buts but I have not been able to do the same with the empty strings. My project is an Azure Function running on .NET Core 3.1.
The configuration I have is the following in the startup
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;
builder.Services.AddMvcCore()
.AddNewtonsoftJson(options =>
{
options.UseMemberCasing();
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
})
.AddJsonOptions(option =>
{
option.JsonSerializerOptions.IgnoreNullValues = true;
option.JsonSerializerOptions.WriteIndented = true;
option.JsonSerializerOptions.IgnoreReadOnlyProperties = true;
});
Any idea why I don't ignore empty strings?
CodePudding user response:
Your settings seems to be correct. However you might need to decorate your properties with the Default value attribute for
CodePudding user response:
Maybe override the tostring method and pass in your JSON options there or just do some isnullorempty checks before you jsonStringify
override public string ToString() {
return [Your format logic here];
}