mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-14 19:49:15 +08:00
51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using System.IO;
|
|
using System.Threading;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
{
|
|
/// <summary>
|
|
/// Serialize object using json format.
|
|
/// </summary>
|
|
public abstract class JsonStrorage<T> : BaseStorage<T> where T : class, IStorage, new()
|
|
{
|
|
private static object syncObject = new object();
|
|
protected override string FileSuffix
|
|
{
|
|
get { return ".json"; }
|
|
}
|
|
|
|
protected override void LoadInternal()
|
|
{
|
|
string json = File.ReadAllText(FilePath);
|
|
if (!string.IsNullOrEmpty(json))
|
|
{
|
|
try
|
|
{
|
|
serializedObject = JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
serializedObject = LoadDefault();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
serializedObject = LoadDefault();
|
|
}
|
|
}
|
|
|
|
protected override void SaveInternal()
|
|
{
|
|
ThreadPool.QueueUserWorkItem(o =>
|
|
{
|
|
lock (syncObject)
|
|
{
|
|
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
|
|
File.WriteAllText(FilePath, json);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|