2016-01-06 14:45:08 +08:00
|
|
|
|
using System.IO;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serialize object using json format.
|
|
|
|
|
/// </summary>
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public class JsonStrorage<T> where T : new()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private T _json;
|
|
|
|
|
private readonly JsonSerializerSettings _serializerSettings;
|
|
|
|
|
|
|
|
|
|
protected string FileName { get; set; }
|
|
|
|
|
protected string FilePath { get; set; }
|
|
|
|
|
protected const string FileSuffix = ".json";
|
|
|
|
|
protected string DirectoryPath { get; set; }
|
|
|
|
|
protected const string DirectoryName = "Config";
|
|
|
|
|
|
|
|
|
|
internal JsonStrorage()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
FileName = typeof(T).Name;
|
|
|
|
|
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
|
|
|
|
|
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
|
|
|
|
|
|
|
|
|
// use property initialization instead of DefaultValueAttribute
|
|
|
|
|
// easier and flexible for default value of object
|
2016-04-26 07:54:09 +08:00
|
|
|
|
_serializerSettings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
};
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public T Load()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
if (!Directory.Exists(DirectoryPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(DirectoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(FilePath))
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
var searlized = File.ReadAllText(FilePath);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(searlized))
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
Deserialize(searlized);
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
else
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
LoadDefault();
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
LoadDefault();
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
|
|
|
|
return _json;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private void Deserialize(string searlized)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
try
|
2015-01-23 21:52:46 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
_json = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
|
|
|
|
|
}
|
|
|
|
|
catch (JsonSerializationException e)
|
|
|
|
|
{
|
|
|
|
|
LoadDefault();
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadDefault()
|
|
|
|
|
{
|
|
|
|
|
_json = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
string serialized = JsonConvert.SerializeObject(_json, Formatting.Indented);
|
|
|
|
|
File.WriteAllText(FilePath, serialized);
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|