mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-15 03:59:15 +08:00
47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|