using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Text.Json.Serialization;
public string Code { get; set; }
public int Id { get; set; }
public string Type { get; set; }
class EntityDtoIEnumerableConverter : JsonConverter<IEnumerable<EntityDto>>
public override IEnumerable<EntityDto> Read(
ref Utf8JsonReader reader,
JsonSerializerOptions options)
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException("JSON payload expected to start with StartObject token.");
List<EntityDto> list = null;
var startDepth = reader.CurrentDepth;
if (reader.TokenType == JsonTokenType.EndObject && reader.CurrentDepth == startDepth)
if (reader.TokenType == JsonTokenType.StartArray)
throw new JsonException("Multiple lists encountered.");
var eodPostions = JsonSerializer.Deserialize<EntityDto[]>(ref reader, options);
(list = new List<EntityDto>(eodPostions.Length)).AddRange(eodPostions);
throw new JsonException();
public override void Write(Utf8JsonWriter writer, IEnumerable<EntityDto> value, JsonSerializerOptions options)
throw new NotImplementedException();
public class WrapperObject
public IEnumerable<EntityDto> Entities { get; set; }
public string SomeAddedData { get; set; }
public static async Task Test()
var serializerOptions = new JsonSerializerOptions
PropertyNameCaseInsensitive = true,
Converters = { new EntityDtoIEnumerableConverter() },
var result = JsonSerializer.Deserialize<IEnumerable<EntityDto>>(json, serializerOptions);
var wrappedJson = GetWrappedJson();
var wrapper = JsonSerializer.Deserialize<WrapperObject>(wrappedJson, serializerOptions);
Assert.IsTrue(wrapper.SomeAddedData == "zzz");
Assert.IsTrue(wrapper.Entities.Count() == result.Count());
var resultJson = JsonSerializer.Serialize(result);
Console.WriteLine("\nRe-serialized {0}", result);
Console.WriteLine(resultJson);
Assert.IsTrue(Newtonsoft.Json.Linq.JToken.DeepEquals(Newtonsoft.Json.Linq.JToken.Parse(resultJson), Newtonsoft.Json.Linq.JToken.Parse(json).SelectToken("Response.Result")));
static string GetWrappedJson()
return @"{ ""Entities"" : " + GetJson() + @", ""SomeAddedData"" : ""zzz"" }";
public static async Task Main(string[] args)
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];