CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Net.Http;
using System.Threading;
using System.Buffers;
using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
#nullable enable
//https://stackoverflow.com/questions/69572189/jsonelement-getrawtext-method-throws-operation-is-not-valid-exception-on-empty
public static partial class JsonExtensions
{
public static async Task<T?> DeserializeResponse<T>(Stream str, string? property, CancellationToken ct = default)
{
if (property is null) // some calls to the API do return data at root-level
{
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) =>
type != typeof(string)
&& typeof(IEnumerable).IsAssignableFrom(type)
&& type != typeof(byte []) // byte arrays are serialized as Base64 strings.
&& !type.IsDictionary();
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())
: type.GetInterfaces();
}
public class JobFunctionCombination
{
/// <summary>
/// Gets or sets the combined identifier of the main function group, the function group and the sub function group.
/// </summary>
/// <example>01_0101_010101</example>
[JsonPropertyName("job_function_combination_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Id { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the combined names of the function groups.
/// </summary>
/// <example>Management | Human Resources | Finance controller</example>
[JsonPropertyName("description")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Description { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the identifier of the main function group.
/// </summary>
[JsonPropertyName("job_main_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string MainFunctionGroupId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the identifier of the function group.
/// </summary>
[JsonPropertyName("job_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string FunctionGroupId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the identifier of the sub function group.
/// </summary>
[JsonPropertyName("job_sub_function_group_id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SubFunctionGroupId { get; set; } = string.Empty;
}
public class Program
{
public static async Task Test()
{
Console.WriteLine(JsonSerializer.Serialize(new byte [] { 1, 2, 3 }));
await Test<JobFunctionCombination>();
await Test<int>();
await Test<string>();
await Test<Dictionary<string, string>>();
await Test<byte []>();
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"",
""value"": []
}
";
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();
try
{
await Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
public static string GetNetCoreVersion()
{
//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
//
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];
return null;
}
}