You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
1 year ago
|
using System;
|
||
|
using System.Xml;
|
||
|
using Newtonsoft.Json;
|
||
|
using Formatting = Newtonsoft.Json.Formatting;
|
||
|
|
||
|
namespace CK.SCP.Utils
|
||
|
{
|
||
|
public class JsonHelper
|
||
|
{
|
||
|
public static T ReadConfigFromFile<T>(string fileName) where T : new()
|
||
|
{
|
||
|
var t = new T();
|
||
|
var strData = FileHelper.ReadFile(fileName);
|
||
|
try
|
||
|
{
|
||
|
t = JsonConvert.DeserializeObject<T>(strData);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
MessageHelper.ShowError($"配置文件{fileName}错误,请重新配置!");
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
public static void WriteConfigToFile<T>(string fileName, T t) where T : new()
|
||
|
{
|
||
|
var strData = JsonConvert.SerializeObject(t, Formatting.Indented);
|
||
|
// MessageHelper.ShowInfo(strConfig);
|
||
|
FileHelper.WriteFile(fileName, strData);
|
||
|
|
||
|
}
|
||
|
public static string GetJson<T>(T t)
|
||
|
{
|
||
|
return JsonConvert.SerializeObject(t, Formatting.Indented);
|
||
|
}
|
||
|
}
|
||
|
}
|