using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public MyClass Myself => this;
public class MyFieldClass
public MyFieldClass() => this.Myself = this;
public MyFieldClass Myself;
public class MyEnumerable : IEnumerable<MyEnumerable>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<MyEnumerable> GetEnumerator() { yield return this; }
[JsonConverter(typeof(MyConverter))]
public record MyConvertable;
public class MyConverter : JsonConverter<MyConvertable>
public override void WriteJson(JsonWriter writer, MyConvertable? value, JsonSerializer serializer) =>
serializer.Serialize(writer, value);
public override MyConvertable? ReadJson(JsonReader reader, Type objectType, MyConvertable? existingValue, bool hasExistingValue, JsonSerializer serializer) =>
throw new NotImplementedException();
public static void Test()
Test(new MyFieldClass());
var dictionary = new Dictionary<string, object>();
dictionary.Add("Myself", dictionary);
var list = new List<object>();
Test(new MyEnumerable());
Test(new MyConvertable());
var root = new { data = new List<object?>() { null } };
root.data.Add(new { Value = root });
static void Test<T>(T value)
Console.WriteLine(JsonConvert.SerializeObject(value));
Console.WriteLine("\nCaught exception {1} when serializing {0}:\n {2}", value, ex.GetType().Name, ex.Message);
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");