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-27 09:15:53 +08:00
|
|
|
|
public class JsonStrorage<T> : Storage<T> where T : new()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private readonly JsonSerializerSettings _serializerSettings;
|
|
|
|
|
|
|
|
|
|
internal JsonStrorage()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
FileSuffix = ".json";
|
|
|
|
|
DirectoryName = "Settings";
|
|
|
|
|
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
ValidateDirectory();
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
// 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-27 09:15:53 +08:00
|
|
|
|
public override T Load()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
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-27 09:15:53 +08:00
|
|
|
|
return Data;
|
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-27 09:15:53 +08:00
|
|
|
|
Data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
catch (JsonSerializationException e)
|
|
|
|
|
{
|
|
|
|
|
LoadDefault();
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public override void LoadDefault()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
Data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public override void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
string serialized = JsonConvert.SerializeObject(Data, Formatting.Indented);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
File.WriteAllText(FilePath, serialized);
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|