// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Text.Json; public class Json13 { public class C { public int C1 { get; set; } public string C2 { get; set; } } public class P { public int P1 { get; set; } public string P2 { get; set; } public C P3 { get; set; } } public static void Main(string[] args) { P o = new P { P1 = 123, P2 = "ABC", P3 = new C { C1 = 456, C2 = "DEF" } }; Console.WriteLine(JsonSerializer.Serialize(o)); Console.WriteLine(JsonSerializer.Serialize(o, new JsonSerializerOptions { WriteIndented = true, IndentSize = 4 } )); } }