龙腾猫跃 4 miesięcy temu
rodzic
commit
d05df2df9b
2 zmienionych plików z 14 dodań i 3 usunięć
  1. 1 1
      Shared/Extensions/StringExtensions.cs
  2. 13 2
      Shared/Utils/FileUtils.cs

+ 1 - 1
Shared/Extensions/StringExtensions.cs

@@ -399,7 +399,7 @@ public static class StringExtensions {
     }
 
     /// <summary>
-    /// 将字符串转化为 JSON 对象。
+    /// 将字符串转化为 JSON 对象,若失败则抛出异常。
     /// </summary>
     public static JToken? DeserializeJson(this string data) {
         try {

+ 13 - 2
Shared/Utils/FileUtils.cs

@@ -1,4 +1,5 @@
-using System.IO.Compression;
+using Newtonsoft.Json.Linq;
+using System.IO.Compression;
 
 namespace MeloongCore;
 public static class FileUtils {
@@ -62,6 +63,13 @@ public static class FileUtils {
     public static string[] TryReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null, Type? type = null)
         => FileUtils.TryReadAsString(filePath, encoding, type)?.SplitLines(skipEmptyLines) ?? [];
 
+    /// <summary>
+    /// 读取 JSON 文件中的所有内容。
+    /// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
+    /// </summary>
+    public static JToken? ReadAsJson(string filePath, Encoding? encoding = null, Type? type = null)
+        => FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson();
+
     #endregion
 
     #region 创建 / 写入
@@ -407,6 +415,9 @@ public static class FileUtils {
 
 }
 
+/// <summary>
+/// 检查文件是否符合校验规则,包括大小、Hash 值、是否为 JSON 文件。
+/// </summary>
 public class FileChecker {
 
     /// <summary>
@@ -480,7 +491,7 @@ public class FileChecker {
             if (IsJson) {
                 string content = FileUtils.ReadAsString(localPath);
                 if (content == "") return "读取到的文件为空";
-                //GetJson(content); // 失败会抛出异常
+                content.DeserializeJson();
             }
 
             return null;