26 lines
734 B
C#
26 lines
734 B
C#
using System.Text.Json.Serialization;
|
|
using System.Text.Json;
|
|
|
|
namespace Harmonia.Core.Data;
|
|
|
|
public abstract class JsonFileRepository<TObject> : FileRepository<TObject> where TObject : notnull, new()
|
|
{
|
|
private readonly JsonSerializerOptions _options = new()
|
|
{
|
|
WriteIndented = true,
|
|
IgnoreReadOnlyProperties = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
};
|
|
|
|
protected override string Extension => "json";
|
|
|
|
protected override TObject Deserialize(Stream stream)
|
|
{
|
|
return JsonSerializer.Deserialize<TObject>(stream) ?? new();
|
|
}
|
|
|
|
protected override string Serialize(TObject obj)
|
|
{
|
|
return JsonSerializer.Serialize(obj, _options);
|
|
}
|
|
} |