ReadAsJson
This commit is contained in:
1 parent
9470a6160a
commit
d05df2df9b
2 files changed
+14
-3
No files matched your search
@@ -399,7 +399,7 @@ public static class StringExtensions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将字符串转化为 JSON 对象。
|
/// 将字符串转化为 JSON 对象,若失败则抛出异常。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static JToken? DeserializeJson(this string data) {
|
public static JToken? DeserializeJson(this string data) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.IO.Compression;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.IO.Compression;
|
||||||
|
|
||||||
namespace MeloongCore;
|
namespace MeloongCore;
|
||||||
public static class FileUtils {
|
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)
|
public static string[] TryReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null, Type? type = null)
|
||||||
=> FileUtils.TryReadAsString(filePath, encoding, type)?.SplitLines(skipEmptyLines) ?? [];
|
=> 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
|
#endregion
|
||||||
|
|
||||||
#region 创建 / 写入
|
#region 创建 / 写入
|
||||||
@@ -407,6 +415,9 @@ public static class FileUtils {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查文件是否符合校验规则,包括大小、Hash 值、是否为 JSON 文件。
|
||||||
|
/// </summary>
|
||||||
public class FileChecker {
|
public class FileChecker {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -480,7 +491,7 @@ public class FileChecker {
|
|||||||
if (IsJson) {
|
if (IsJson) {
|
||||||
string content = FileUtils.ReadAsString(localPath);
|
string content = FileUtils.ReadAsString(localPath);
|
||||||
if (content == "") return "读取到的文件为空";
|
if (content == "") return "读取到的文件为空";
|
||||||
//GetJson(content); // 失败会抛出异常
|
content.DeserializeJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in new issue
Block a user