2
0
Files
controlmymonitormanagement/Library/Helpers/JsonHelper.cs

127 lines
3.6 KiB
C#
Raw Normal View History

2023-07-03 01:51:09 +08:00
using System.IO;
2022-05-23 00:58:58 +08:00
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
2023-07-03 01:51:09 +08:00
namespace CMM.Library.Helpers;
static class JsonSerializerExtensions
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
public static JsonSerializerOptions defaultSettings = new JsonSerializerOptions()
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
WriteIndented = true,
IgnoreNullValues = true,
PropertyNamingPolicy = null,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static class JsonHelper
{
/// <summary>
/// 複製整個obj全部結構
/// </summary>
public static T DeepCopy<T>(T RealObject) =>
JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(RealObject, JsonSerializerExtensions.defaultSettings));
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string JsonFormResource(this string fileName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName =
assembly.GetManifestResourceNames().
Where(str => str.Contains(fileName)).FirstOrDefault();
if (resourceName == null) return "";
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream, Encoding.UTF8))
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
return reader.ReadToEnd();
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static T JsonFormResource<T>(this string fileName) =>
JsonFormString<T>(JsonFormResource(fileName));
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static T JsonFormFile<T>(this string fileName) =>
JsonFormString<T>(Load(fileName));
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static T JsonFormString<T>(this string json) =>
JsonSerializer.Deserialize<T>(json);
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static void FileToJson<T>(this T payload, string savePath) =>
Save(savePath, payload.ToJson());
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string ToJson<T>(this T payload) =>
JsonSerializer.Serialize(payload, JsonSerializerExtensions.defaultSettings);
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// 從Embedded resource讀string
/// </summary>
/// <param name="aFileName">resource位置不含副檔名</param>
public static string GetResource(this Assembly assembly, string aFileName)
{
var resourceName = assembly
.GetManifestResourceNames()
.Where(str => str.Contains(aFileName))
.FirstOrDefault();
if (resourceName == null) return "";
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var sr = new StreamReader(stream, Encoding.UTF8))
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
return sr.ReadToEnd();
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string Load(string aFileName) =>
Load(new FileInfo(aFileName));
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string Load(FileInfo aFi)
{
if (aFi.Exists)
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
string _Json = string.Empty;
try
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
var sr = new StreamReader(aFi.FullName);
_Json = sr.ReadToEnd();
sr.Close();
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
catch (IOException) { throw; }
catch (Exception) { throw; }
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
return _Json;
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
throw new Exception("開檔失敗。");
}
public static void Save(string filePath, string content) =>
Save(new FileInfo(filePath), content);
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static void Save(FileInfo aFi, string aContent)
{
if (!aFi.Directory.Exists)
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
aFi.Directory.Create();
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
if (aFi.Exists)
{
aFi.Delete();
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
aFi.Refresh();
if (aFi.Exists) throw new Exception("寫檔失敗,檔案已存在或已開啟。");
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
try
{
File.WriteAllText(aFi.FullName, aContent);
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
catch (IOException) { throw; }
catch (Exception) { throw; }
2022-05-23 00:58:58 +08:00
}
}