using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
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 string para { get; set; }
public List<string> GlossSeeAlso { get; set; }
public string ID { get; set; }
public string SortAs { get; set; }
public string GlossTerm { get; set; }
public string Acronym { get; set; }
public string Abbrev { get; set; }
public GlossDef GlossDef { get; set; }
public string GlossSee { get; set; }
public GlossEntry GlossEntry { get; set; }
public string title { get; set; }
public GlossList GlossList { get; set; }
public string title { get; set; }
public GlossDiv GlossDiv { get; set; }
public static void Test()
using var jDoc = JsonDocument.Parse(str);
var myClass = jDoc.RootElement.GetProperty("SomeProperty").ToObject<SomeClass>();
var newJson = JsonSerializer.Serialize(myClass, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(newJson);
""title"":""example glossary"",
""GlossTerm"":""Standard Generalized Markup Language"",
""Abbrev"":""ISO 8879:1986"",
""para"":""A meta-markup language, used to create markup languages such as DocBook."",
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("System.Text.Json version: " + 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];