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 static partial class JsonExtensions
public static async Task<T?> DeserializeResponse<T>(Stream str, string? property, CancellationToken ct = default)
return await JsonSerializer.DeserializeAsync<T>(str, cancellationToken: ct).ConfigureAwait(false);
using var jsonDocument = await JsonDocument.ParseAsync(str, default, ct).ConfigureAwait(false);
if (!jsonDocument.RootElement.TryGetProperty(property, out JsonElement parsed))
throw new InvalidDataException($"The specified lookup property \"{property}\" could not be found.");
return typeof(T).IsSerializedAsArray()
? parsed.Deserialize<T>()
: parsed.EnumerateArray().Select(i => i.Deserialize<T>()).FirstOrDefault();
static bool IsSerializedAsArray(this Type type) =>
&& typeof(IEnumerable).IsAssignableFrom(type)
&& type != typeof(byte [])
static bool IsDictionary(this Type type) =>
typeof(IDictionary).IsAssignableFrom(type)
|| type.GetInterfacesAndSelf().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>));
static IEnumerable<Type> GetInterfacesAndSelf(this Type type) =>
(type ?? throw new ArgumentNullException()).IsInterface
? new[] { type }.Concat(type.GetInterfaces())
public class JobFunctionCombination
[JsonPropertyName("job_function_combination_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Id { get; set; } = string.Empty;
[JsonPropertyName("description")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Description { get; set; } = string.Empty;
[JsonPropertyName("job_main_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string MainFunctionGroupId { get; set; } = string.Empty;
[JsonPropertyName("job_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string FunctionGroupId { get; set; } = string.Empty;
[JsonPropertyName("job_sub_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SubFunctionGroupId { get; set; } = string.Empty;
public static async Task Test()
Console.WriteLine(JsonSerializer.Serialize(new byte [] { 1, 2, 3 }));
await Test<JobFunctionCombination>();
await Test<Dictionary<string, string>>();
await TestArray<JobFunctionCombination []>();
await TestArray<List<int>>();
await TestArray<HashSet<string>>();
await TestArray<List<Dictionary<string, string>>>();
public static async Task Test<T>()
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(GetJson()));
var item = await JsonExtensions.DeserializeResponse<T>(stream, "value");
Assert.AreEqual(item, default(T));
public static async Task TestArray<T>() where T : IEnumerable
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(GetJson()));
var item = await JsonExtensions.DeserializeResponse<T>(stream, "value");
Assert.IsNotNull(item, $"item of type {typeof(T)} is null");
Assert.AreEqual(0, ((IEnumerable)item!).Cast<object>().Count(), $"item of type {typeof(T)} has nonzero count");
static string GetJson() => @"{
""context"": ""SomeUnusedContextValue"",
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.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];