using System.Text.Json.Serialization;
using System.Collections.Generic;
public static void Main()
var outerThing = new OuterObject(new Thing(Guid.NewGuid(), "outer object thing", null));
var serializedOuterThing = JsonSerializer.Serialize(outerThing);
Console.WriteLine($"Serialized outer thing: {serializedOuterThing}");
var deserializedOuterThing = JsonSerializer.Deserialize<OuterObject>(serializedOuterThing);
Console.WriteLine($"Outer thing Id: {deserializedOuterThing.Thing.Id} Name: {deserializedOuterThing.Thing.Name}");
var containerThing = new ContainerObject(new[] { new Thing(Guid.NewGuid(), "container thing", null) });
var serializedContainerThing = JsonSerializer.Serialize(containerThing);
Console.WriteLine($"Serialized container thing: {serializedContainerThing}");
var deserializedContainerThing = JsonSerializer.Deserialize<ContainerObject>(serializedContainerThing);
Console.WriteLine($"Container thing Id: {deserializedContainerThing.Things.First().Id} Name: {deserializedContainerThing.Things.First().Name}");
var thingByItself = new Thing(Guid.NewGuid(), "thing by itself", null);
var serializedThingByItself = JsonSerializer.Serialize(thingByItself);
Console.WriteLine($"Serialized thing by itself: {serializedThingByItself}");
var deserializedThingByItself = JsonSerializer.Deserialize<IThing>(serializedThingByItself);
Console.WriteLine($"Container thing Id: {deserializedThingByItself.Id} Name: {deserializedThingByItself.Name}");
[JsonDerivedType(typeof(Thing), typeDiscriminator: 1)]
public string Name { get; }
public record Thing(Guid Id, string Name, string? Description) : IThing;
public record OuterObject(IThing Thing);
public record ContainerObject(IReadOnlyCollection<IThing> Things);