using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_SmartVisionCommon
{
///
/// T类型-JSON--Serialize和Deserialize
///
/// T类型
public static class ConfigManager
{
///
/// 保存配置
///
/// T类型
/// T类型的值
/// 文件存储路径以及名称
public static void SaveConfig(T config, string filePath)
{
if (config == null)
{
AsyncLogHelper.Error(nameof(config));
throw new ArgumentNullException(nameof(config));
}
if (filePath == null)
{
AsyncLogHelper.Error(nameof(filePath));
AsyncLogHelper.Error(nameof(filePath));
}
var json = JsonConvert.SerializeObject(config, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, json);
}
///
/// 加载配置文件
///
/// T类型
/// 文件存储路径以及名称
/// T类型的值
public static T LoadConfig(string filePath) where T : new()
{
if (!File.Exists(filePath))
{
return new T();
}
var json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject(json);
}
}
}