V13 / 7879e8b5740b
JsonSettings.cs
Lilith/Core/Extensions/JsonSettings.cs
Unchanged from the preceding versionCS
using System.Text.Json;
namespace Lilith.Core.Extensions;
public static class JsonSettings
{
public static T Load<T>(string path, Func<T, bool> valid) where T : class
{
var value = JsonSerializer.Deserialize<T>(File.ReadAllText(path));
if (value is null || !valid(value)) throw new JsonException("Saved preferences contain invalid values.");
return value;
}
public static void Save<T>(string path, T value) => AtomicFile.Write(path, temporary =>
File.WriteAllText(temporary, JsonSerializer.Serialize(value, new JsonSerializerOptions { WriteIndented = true })));
}