61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace HSchool.People;
|
|
|
|
/// <summary>On-disk shape of <c>saves/{id}.people.json</c>. Composition only — needs are not live values.</summary>
|
|
public sealed class RosterDocument
|
|
{
|
|
public const int CurrentFormat = 1;
|
|
|
|
public int Format { get; init; } = CurrentFormat;
|
|
|
|
public int Seed { get; init; }
|
|
|
|
public required IReadOnlyList<Person> People { get; init; }
|
|
|
|
public required IReadOnlyList<Family> Families { get; init; }
|
|
|
|
public required IReadOnlyList<SchoolClass> 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<RosterDocument>(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;
|
|
}
|
|
}
|