FileChecker、DeserializeJson

This commit is contained in:
龙腾猫跃 committed 2026-06-11 00:36:04 +08:00
1 parent ec1ad309d8
commit 9470a6160a
4 files changed
+113 -9

No files matched your search

+19 -5
View File
@@ -1,4 +1,6 @@
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
namespace MeloongCore.Extensions;
public static class StringExtensions {
@@ -331,8 +333,6 @@ public static class StringExtensions {
#endregion
#region 读取为字符串
/// <summary>
/// 读取 <see cref="Stream"/> 中的内容,并解码为字符串。
/// 会将流的位置主动重置到开头。
@@ -366,8 +366,6 @@ public static class StringExtensions {
}
}
#endregion
/// <summary>
/// 将第一个字符转换为大写,其余字符转换为小写。
/// </summary>
@@ -400,4 +398,20 @@ public static class StringExtensions {
return result ^ 0xA98F501BC684032FUL;
}
/// <summary>
/// 将字符串转化为 JSON 对象。
/// </summary>
public static JToken? DeserializeJson(this string data) {
try {
return JsonConvert.DeserializeObject<JToken>(data, new JsonSerializerSettings {DateTimeZoneHandling = DateTimeZoneHandling.Local});
} catch (Exception ex) {
int length = (data ?? "").Length;
throw new FormatException("JSON deserialize failed: " +
(length > 2000
? $"{data![..500]}...(total {length} characters)...{data[^500..]}"
: (data ?? "")),
ex);
}
}
}