using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class NullFilteringListConverter<T> : JsonConverter<List<T>>
public override List<T> ReadJson(JsonReader reader, Type objectType, List<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null)
var list = existingValue as List<T> ?? (List<T>)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, list);
list.RemoveAll(i => i == null);
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, List<T> value, JsonSerializer serializer) => throw new NotImplementedException();
public static partial class JsonExtensions
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None)
while (reader.TokenType == JsonToken.Comment)
public static JsonReader ReadAndAssert(this JsonReader reader)
throw new ArgumentNullException();
throw new JsonReaderException("Unexpected end of JSON stream.");
public string Variable1 { get; set; }
public string Variable2 { get; set; }
[JsonConverter(typeof(NullFilteringListConverter<ArrayObject>))]
public List<ArrayObject> ArrayObject { get; set; }
public string VariableArray1 { get; set; }
public string VariableArray2 { get; set; }
public static void Test()
var settings = new JsonSerializerSettings()
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Class1 class1 = JsonConvert.DeserializeObject<Class1>(json, settings);
Console.WriteLine(class1.ArrayObject == null);
Console.WriteLine(class1.ArrayObject.Count());
foreach (var item in class1.ArrayObject)
Console.WriteLine(item.VariableArray1);
Console.WriteLine(item.VariableArray2);
Console.WriteLine("#######################");
Assert.IsTrue(class1.ArrayObject.All(i => i != null));
""Variable2"": ""50000"",
""ArrayObject"": /*Some Comment */ [null /*Some Comment */, {}, null, {}]
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];