mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-03 05:53:12 +00:00
58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using Newtonsoft.Json;
|
|
|
|
namespace AutoClient.Modes.FolderStore
|
|
{
|
|
public class JsonFile<T> where T : new()
|
|
{
|
|
private readonly App app;
|
|
private readonly string filePath;
|
|
private readonly object fileLock = new object();
|
|
|
|
public JsonFile(App app, string filePath)
|
|
{
|
|
this.app = app;
|
|
this.filePath = filePath;
|
|
}
|
|
|
|
public T Load()
|
|
{
|
|
lock (fileLock)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
var state = new T();
|
|
Save(state);
|
|
return state;
|
|
}
|
|
var text = File.ReadAllText(filePath);
|
|
return JsonConvert.DeserializeObject<T>(text)!;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
app.Log.Error("Failed to load state: " + exc);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Save(T state)
|
|
{
|
|
lock (fileLock)
|
|
{
|
|
try
|
|
{
|
|
var json = JsonConvert.SerializeObject(state, Formatting.Indented);
|
|
File.WriteAllText(filePath, json);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
app.Log.Error("Failed to save state: " + exc);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|