using System.Text.Json; using System.Text.Json.Serialization; namespace AIStudio.TrimSmokeTest; /// /// The types which AI Studio reads or writes as JSON, each with the options it uses for them. /// /// /// The options of AI Studio each name the context which knows their types, so the types registered in that /// context are exactly those which System.Text.Json gets to read or write with these options. Whatever these /// types hold, i.e., the types of their properties, the elements of their collections, and their derived /// types, gets checked along with them. A new type, or new options, need nothing here: they are found /// through their context. /// public static class JsonRoots { /// /// Returns every type registered in a context of the options of AI Studio, together with these options. /// /// /// Options which several members share are checked once. Like AppJsonOptions.FindAll, this needs the /// static state of the app to be filled before. /// public static IReadOnlyList FindAll() => AppJsonOptions.FindAll().Values .Distinct(ReferenceEqualityComparer.Instance) .Cast() .SelectMany(options => options.TypeInfoResolverChain .OfType() .SelectMany(RegisteredTypes) .Select(type => new JsonRoot(type, options))) .ToList(); private static IEnumerable RegisteredTypes(JsonSerializerContext context) => context.GetType() .GetCustomAttributesData() .Where(attribute => attribute.AttributeType == typeof(JsonSerializableAttribute)) .Select(attribute => (Type)attribute.ConstructorArguments[0].Value!); }