mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
fix #185. Loading index cache on startup.
This commit is contained in:
46
Wox.Infrastructure/Storage/JsonStrorage.cs
Normal file
46
Wox.Infrastructure/Storage/JsonStrorage.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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()
|
||||
{
|
||||
protected override string FileSuffix
|
||||
{
|
||||
get { return ".json"; }
|
||||
}
|
||||
|
||||
protected override void LoadInternal()
|
||||
{
|
||||
string json = File.ReadAllText(ConfigPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
serializedObject = JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SaveInternal()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user