This commit is contained in:
qianlifeng
2015-01-01 23:07:14 +08:00
parent 96d908094b
commit a038587224
5 changed files with 12 additions and 2 deletions

View 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);
}
}
}