using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public string AProperty { get; set; } = "A";
public class A<T> : A where T : class, new()
[System.Text.Json.Serialization.JsonPropertyName("TObject")]
[Newtonsoft.Json.JsonIgnore]
public object SerializedTObject { get { return TObject; } }
[System.Text.Json.Serialization.JsonIgnore]
public T TObject { get; set; } = new T();
public string BProperty { get; set; } = "B";
public class B<T> : B where T : class, new()
public T TObject { get; set; } = new T();
public string CProperty { get; set; } = "C";
public static void Test()
var obj = new A<B> { TObject = new B<C>() };
var systemTextSerialized = System.Text.Json.JsonSerializer.Serialize(obj);
var newtonsoftSerialized = JsonConvert.SerializeObject(obj);
Console.WriteLine(systemTextSerialized);
Console.WriteLine(newtonsoftSerialized);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(Newtonsoft.Json.JsonSerializer).Assembly.GetName().Name, typeof(Newtonsoft.Json.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];