rm Newtonsoft json

This commit is contained in:
Collin M. Barrett 2020-02-09 16:01:42 -06:00
parent a153c8564f
commit 291461f01f
3 changed files with 1 additions and 43 deletions

View file

@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace FilterLists.Api.DependencyInjection.Extensions
{
@ -27,12 +26,7 @@ private static void ConfigureCookiePolicy(this IServiceCollection services) =>
private static void AddControllersCustom(this IServiceCollection services) =>
services.AddControllers()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(opts =>
{
opts.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
opts.SerializerSettings.ContractResolver = new SkipEmptyContractResolver();
});
.SetCompatibilityVersion(CompatibilityVersion.Latest);
private static void AddRoutingCustom(this IServiceCollection services) =>
services.AddRouting(opts => opts.LowercaseUrls = true);

View file

@ -39,7 +39,6 @@
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.7.9" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>

View file

@ -1,35 +0,0 @@
using System;
using System.Collections;
using System.Reflection;
using Humanizer;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace FilterLists.Api
{
// REF: https://stackoverflow.com/a/18486790/2343739
public class SkipEmptyContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
property.PropertyName = property.PropertyName.Camelize();
var isDefaultValueIgnored =
((property.DefaultValueHandling ?? DefaultValueHandling.Ignore) & DefaultValueHandling.Ignore) != 0;
if (!isDefaultValueIgnored || typeof(string).IsAssignableFrom(property.PropertyType) ||
!typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
return property;
bool NewShouldSerialize(object obj)
{
return !(property.ValueProvider.GetValue(obj) is ICollection collection) || collection.Count != 0;
}
var oldShouldSerialize = property.ShouldSerialize;
property.ShouldSerialize = oldShouldSerialize != null
? o => oldShouldSerialize(o) && NewShouldSerialize(o)
: (Predicate<object>)NewShouldSerialize;
return property;
}
}
}