diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs index 0de6b12..0646f38 100644 --- a/Api/Utility/General.cs +++ b/Api/Utility/General.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Net.NetworkInformation; using System.Security.Cryptography; +using System.Text.Json; using System.Text.RegularExpressions; using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Constant; @@ -128,6 +129,18 @@ namespace Milimoe.FunGame.Core.Api.Utility return Service.JsonManager.GetString(obj); } + /// + /// 返回目标对象的Json字符串 可指定反序列化选项 + /// + /// + /// + /// + /// + public static string JsonSerialize(T obj, JsonSerializerOptions options) + { + return Service.JsonManager.GetString(obj, options); + } + /// /// 反序列化Json对象 /// @@ -139,6 +152,18 @@ namespace Milimoe.FunGame.Core.Api.Utility return Service.JsonManager.GetObject(json); } + /// + /// 反序列化Json对象 可指定反序列化选项 + /// + /// + /// + /// + /// + public static T? JsonDeserialize(string json, JsonSerializerOptions options) + { + return Service.JsonManager.GetObject(json, options); + } + /// /// 反序列化Hashtable中的Json对象 /// @@ -148,7 +173,20 @@ namespace Milimoe.FunGame.Core.Api.Utility /// public static T? JsonDeserializeFromHashtable(Hashtable hashtable, string key) { - return Transmittal.DataRequest.GetHashtableJsonObject(hashtable, key); + return Service.JsonManager.GetObject(hashtable, key); + } + + /// + /// 反序列化Hashtable中的Json对象 可指定反序列化选项 + /// + /// + /// + /// + /// + /// + public static T? JsonDeserializeFromHashtable(Hashtable hashtable, string key, JsonSerializerOptions options) + { + return Service.JsonManager.GetObject(hashtable, key, options); } } diff --git a/Service/JsonManager.cs b/Service/JsonManager.cs index 83237d6..712621b 100644 --- a/Service/JsonManager.cs +++ b/Service/JsonManager.cs @@ -38,6 +38,18 @@ namespace Milimoe.FunGame.Core.Service return JsonSerializer.Serialize(obj, GeneralOptions); } + /// + /// 获取Json字符串 + /// + /// + /// + /// + /// + internal static string GetString(T obj, JsonSerializerOptions options) + { + return JsonSerializer.Serialize(obj, options); + } + /// /// 反序列化Json对象 /// @@ -48,6 +60,18 @@ namespace Milimoe.FunGame.Core.Service { return JsonSerializer.Deserialize(json, GeneralOptions); } + + /// + /// 反序列化Json对象 + /// + /// + /// + /// + /// + internal static T? GetObject(string json, JsonSerializerOptions options) + { + return JsonSerializer.Deserialize(json, options); + } /// /// 反序列化Json对象,此方法可能无法返回正确的类型,请注意辨别 @@ -59,6 +83,18 @@ namespace Milimoe.FunGame.Core.Service return JsonSerializer.Deserialize(json, GeneralOptions); } + /// + /// 反序列化Json对象,此方法可能无法返回正确的类型,请注意辨别 + /// + /// + /// + /// + /// + internal static object? GetObject(string json, JsonSerializerOptions options) + { + return JsonSerializer.Deserialize(json, options); + } + /// /// 反序列化SocketObject中索引为index的Json对象 /// @@ -96,6 +132,28 @@ namespace Milimoe.FunGame.Core.Service return default; } + /// + /// 反序列化Hashtable中Key对应的Json对象 + /// + /// + /// + /// + /// + /// + internal static T? GetObject(Hashtable table, string key, JsonSerializerOptions options) + { + if (table.ContainsKey(key)) + { + JsonElement? element = (JsonElement?)table[key]; + if (element != null) + { + T? result = ((JsonElement)element).Deserialize(options); + return result; + } + } + return default; + } + /// /// 反序列化多个Json对象 /// 注意必须是相同的Json对象才可以使用此方法解析 @@ -108,5 +166,19 @@ namespace Milimoe.FunGame.Core.Service json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 return JsonSerializer.Deserialize>(json, GeneralOptions) ?? new List(); } + + /// + /// 反序列化多个Json对象 + /// 注意必须是相同的Json对象才可以使用此方法解析 + /// + /// + /// + /// + /// + internal static List GetObjects(string json, JsonSerializerOptions options) + { + json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 + return JsonSerializer.Deserialize>(json, options) ?? new List(); + } } }