using System.Text.Json.Serialization; using System.Text.Json; namespace Harmonia.Core.Data; public abstract class JsonFileRepository : FileRepository where TObject : notnull, new() { private readonly JsonSerializerOptions _options = new() { WriteIndented = true, IgnoreReadOnlyProperties = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; protected override string Extension => "json"; protected override async Task DeserializeAsync(Stream stream) { return await JsonSerializer.DeserializeAsync(stream) ?? new(); } protected override async Task SerializeAsync(TObject obj) { using MemoryStream memoryStream = new(); await JsonSerializer.SerializeAsync(memoryStream, obj, _options); memoryStream.Position = 0; using StreamReader reader = new(memoryStream); return await reader.ReadToEndAsync(); } }