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 T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
var bufferWriter = new ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
public static T ToObject<T>(this JsonDocument document, JsonSerializerOptions options = null)
throw new ArgumentNullException(nameof(document));
return document.RootElement.ToObject<T>(options);
public long Version { get; set; }
public Section[] Sections { get; set; }
public string Label { get; set; }
public Field[] Fields { get; set; }
public long Order { get; set; }
public string InternalName { get; set; }
public static async Task Test()
using var doc = JsonDocument.Parse(GetJson());
var options = new System.Text.Json.JsonSerializerOptions {PropertyNameCaseInsensitive = true};
var serializedLayout = doc.RootElement.ToObject<UILayout>(options);
Assert.AreEqual(1, serializedLayout.Version);
Assert.AreEqual(1, serializedLayout.Sections.Length);
Assert.AreEqual(3, serializedLayout.Sections[0].Fields.Length);
""label"": ""General Information"",
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];