<PackageReference Include="NJsonSchema" Version="11.0.0-preview007" />

IgnoreEmptyCollectionsContractResolver

using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections; using System.Reflection; using System.Runtime.CompilerServices; namespace NJsonSchema.Infrastructure { internal sealed class IgnoreEmptyCollectionsContractResolver : PropertyRenameAndIgnoreSerializerContractResolver { [System.Runtime.CompilerServices.NullableContext(1)] protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization); if ((property.Required == Required.Default || property.Required == Required.DisallowNull) && property.PropertyType != typeof(string)) { TypeInfo typeInfo = typeof(IEnumerable).GetTypeInfo(); Type propertyType = property.PropertyType; if (typeInfo.IsAssignableFrom(((object)propertyType != null) ? propertyType.GetTypeInfo() : null)) property.ShouldSerialize = ((object instance) => ((instance != null) ? (property.ValueProvider?.GetValue(instance) as IEnumerable) : null)?.GetEnumerator().MoveNext() ?? true); } return property; } } }