mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
remove storage dependency from jsonstorage
This commit is contained in:
@@ -11,21 +11,21 @@ namespace Wox.Infrastructure.Storage
|
||||
/// <summary>
|
||||
/// Stroage object using binary data
|
||||
/// Normally, it has better performance, but not readable
|
||||
/// You MUST mark implement class as Serializable
|
||||
/// </summary>
|
||||
public class BinaryStorage<T> : Storage<T>
|
||||
public class BinaryStorage<T>
|
||||
{
|
||||
public BinaryStorage(string filename)
|
||||
{
|
||||
FileSuffix = ".cache";
|
||||
DirectoryName = "Cache";
|
||||
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
|
||||
FileName = filename;
|
||||
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
||||
const string directoryName = "Cache";
|
||||
var directoryPath = Path.Combine(Constant.DataDirectory, directoryName);
|
||||
Helper.ValidateDirectory(directoryPath);
|
||||
|
||||
ValidateDirectory();
|
||||
const string fileSuffix = ".cache";
|
||||
FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}");
|
||||
}
|
||||
|
||||
public string FilePath { get; }
|
||||
|
||||
public T TryLoad(T defaultData)
|
||||
{
|
||||
if (File.Exists(FilePath))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
@@ -8,19 +8,19 @@ namespace Wox.Infrastructure.Storage
|
||||
/// <summary>
|
||||
/// Serialize object using json format.
|
||||
/// </summary>
|
||||
public class JsonStrorage<T> : Storage<T> where T : new()
|
||||
public class JsonStrorage<T>
|
||||
{
|
||||
private readonly JsonSerializerSettings _serializerSettings;
|
||||
private T _data;
|
||||
// need a new directory name
|
||||
public const string DirectoryName = "Settings";
|
||||
public const string FileSuffix = ".json";
|
||||
public string FilePath { get; set; }
|
||||
public string DirectoryPath { get; set; }
|
||||
|
||||
|
||||
internal JsonStrorage()
|
||||
{
|
||||
FileSuffix = ".json";
|
||||
DirectoryName = "Settings";
|
||||
DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);
|
||||
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
||||
|
||||
ValidateDirectory();
|
||||
|
||||
// use property initialization instead of DefaultValueAttribute
|
||||
// easier and flexible for default value of object
|
||||
_serializerSettings = new JsonSerializerSettings
|
||||
@@ -48,14 +48,14 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
LoadDefault();
|
||||
}
|
||||
return Data;
|
||||
return _data;
|
||||
}
|
||||
|
||||
private void Deserialize(string searlized)
|
||||
{
|
||||
try
|
||||
{
|
||||
Data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
|
||||
_data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
|
||||
}
|
||||
catch (JsonSerializationException e)
|
||||
{
|
||||
@@ -66,13 +66,13 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
public void LoadDefault()
|
||||
{
|
||||
Data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
|
||||
_data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
string serialized = JsonConvert.SerializeObject(Data, Formatting.Indented);
|
||||
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
|
||||
File.WriteAllText(FilePath, serialized);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,13 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public PluginJsonStorage()
|
||||
{
|
||||
DirectoryName = Constant.Plugins;
|
||||
|
||||
// C# releated, add python releated below
|
||||
var assemblyName = DataType.Assembly.GetName().Name;
|
||||
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName, assemblyName);
|
||||
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
||||
var dataType = typeof(T);
|
||||
var assemblyName = typeof(T).Assembly.GetName().Name;
|
||||
DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Plugins, assemblyName);
|
||||
Helper.ValidateDirectory(DirectoryPath);
|
||||
|
||||
ValidateDirectory();
|
||||
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public class Storage<T>
|
||||
{
|
||||
protected T Data;
|
||||
protected Type DataType { get; }
|
||||
public string FileName { get; set; }
|
||||
public string FilePath { get; set; }
|
||||
public string FileSuffix { get; set; }
|
||||
public string DirectoryPath { get; set; }
|
||||
public string DirectoryName { get; set; }
|
||||
|
||||
protected Storage()
|
||||
{
|
||||
DataType = typeof (T);
|
||||
FileName = DataType.Name;
|
||||
DirectoryPath = Constant.DataDirectory;
|
||||
}
|
||||
|
||||
protected void ValidateDirectory()
|
||||
{
|
||||
if (!Directory.Exists(DirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(DirectoryPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Wox.Infrastructure/Storage/WoxJsonStorage.cs
Normal file
21
Wox.Infrastructure/Storage/WoxJsonStorage.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
class WoxJsonStorage<T> : JsonStrorage<T> where T : new()
|
||||
{
|
||||
public WoxJsonStorage()
|
||||
{
|
||||
var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);
|
||||
Helper.ValidateDirectory(directoryPath);
|
||||
|
||||
var filename = typeof(T).Name;
|
||||
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user