using System.Text.Json; using System.Text.Json.Serialization; namespace HSchool.People; /// On-disk shape of saves/{id}.people.json. Composition only — needs are not live values. public sealed class RosterDocument { public const int CurrentFormat = 1; public int Format { get; init; } = CurrentFormat; public int Seed { get; init; } public required IReadOnlyList People { get; init; } public required IReadOnlyList Families { get; init; } public required IReadOnlyList Classes { get; init; } public ApplicantPool? Applicants { get; init; } public Roster ToRoster() => new(People, Families, Classes); public static RosterDocument From(int seed, Roster roster, ApplicantPool? applicants = null) => new() { Format = CurrentFormat, Seed = seed, People = roster.People, Families = roster.Families, Classes = roster.Classes, Applicants = applicants, }; } public static class RosterJson { public static JsonSerializerOptions Options { get; } = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; public static string Serialize(RosterDocument document) => JsonSerializer.Serialize(document, Options); public static RosterDocument Parse(string json) { var document = JsonSerializer.Deserialize(json, Options); if (document is null || document.People is null || document.Families is null || document.Classes is null) { throw new InvalidOperationException("People save deserialized to nothing."); } return document; } }