I am developing a .Net Core 2.2 Web API. I am attempting to take advantage of ControllerBase methods such as AcceptedAtAction, Ok, etc. I have configured my JsonSerializerSettings via .AddJsonOptions in my Startup.cs. However, these action methods appear to ignore the serialization settings. Is there something else I need to do in order to have these methods honor these settings?
Startup.cs
.AddJsonOptions(opts =>
{
opts.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
opts.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
opts.SerializerSettings.ContractResolver = new EmptyCollectionContractResolver();
})
Controller
return AcceptedAtAction(
nameof(GetPayEntryImport),
new
{
companyId = companyId,
id = timeImportResponseModel.TimeImportFileTrackingId,
version = apiVersion.ToString()
},
timeImportResponseModel);
Response with empty collections serialized as empty arrays which shouldn't be the case given the ContractResolver I am utilizing.
{
"timeImportFileTrackingId": "bd3cd9fc-09c7-4da0-b1d9-eb8863841ed8",
"status": "The file was accepted and is currently being processed.",
"postProcessingResults": [],
"processedData": []
}
EmptyCollectionContractResolver
public class EmptyCollectionContractResolver : CamelCasePropertyNamesContractResolver
{
protected virtual JsonProperty CreateProperty(
MemberInfo member,
MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
Predicate<object> shouldSerialize = property.get_ShouldSerialize();
property.set_ShouldSerialize((Predicate<object>) (obj =>
{
if (shouldSerialize == null || shouldSerialize(obj))
return !this.IsEmptyCollection(property, obj);
return false;
}));
return property;
}
private bool IsEmptyCollection(JsonProperty property, object target)
{
object obj = property.ValueProvider.GetValue(target);
switch (obj)
{
case ICollection collection:
if (collection.Count == 0)
return true;
break;
case null:
return false;
}
if (!typeof (IEnumerable).IsAssignableFrom(property.get_PropertyType()))
return false;
PropertyInfo property1 = property.get_PropertyType().GetProperty("Count");
if (property1 == (PropertyInfo) null)
return false;
return (int) property1.GetValue(obj, (object[]) null) == 0;
}
}
EmptyCollectionContractResolver
? That might be at fault here, because the configuration you've applied should apply toAcceptedAtAction
et al. – Kirk LarkinEmptyCollectionContractResolver
come from? I can only find one reference to it on google on some random guys GitHub. – DetectivePikachu