using System.Collections; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Unicode; using Milimoe.FunGame.Core.Library.Common.JsonConverter; using Milimoe.FunGame.Core.Library.Common.Network; namespace Milimoe.FunGame.Core.Service { internal class JsonManager { /// /// 默认的序列化选项 /// internal static JsonSerializerOptions GeneralOptions { get; } = new() { WriteIndented = true, PropertyNameCaseInsensitive = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), ReferenceHandler = ReferenceHandler.IgnoreCycles, Converters = { new DateTimeConverter(), new DataTableConverter(), new DataSetConverter(), new UserConverter(), new RoomConverter(), new CharacterConverter(), new MagicResistanceConverter(), new EquipSlotConverter(), new SkillConverter(), new EffectConverter(), new ItemConverter(), new InventoryConverter(), new NormalAttackConverter(), new ClubConverter(), new GoodsConverter(), new StoreConverter(), new NovelOptionConverter(), new NovelNodeConverter() } }; /// /// 获取Json字符串 /// /// /// /// internal static string GetString(T obj) { return JsonSerializer.Serialize(obj, GeneralOptions); } /// /// 获取Json字符串 /// /// /// /// /// internal static string GetString(T obj, JsonSerializerOptions options) { return JsonSerializer.Serialize(obj, options); } /// /// 反序列化Json对象 /// /// /// /// internal static T? GetObject(string json) { return JsonSerializer.Deserialize(json, GeneralOptions); } /// /// 反序列化Json对象,使用 /// /// /// /// /// internal static T? GetObject(ref Utf8JsonReader reader, JsonSerializerOptions options) { return JsonSerializer.Deserialize(ref reader, options); } /// /// 反序列化Json对象 /// /// /// /// /// internal static T? GetObject(string json, JsonSerializerOptions options) { return JsonSerializer.Deserialize(json, options); } /// /// 反序列化Json对象,此方法可能无法返回正确的类型,请注意辨别 /// /// /// internal static object? GetObject(string json) { return JsonSerializer.Deserialize(json, GeneralOptions); } /// /// 反序列化Json对象,此方法可能无法返回正确的类型,请注意辨别 /// /// /// /// internal static object? GetObject(string json, JsonSerializerOptions options) { return JsonSerializer.Deserialize(json, options); } /// /// 反序列化SocketObject中索引为index的Json对象 /// /// /// /// /// /// internal static T? GetObject(SocketObject obj, int index) { if (index >= obj.Parameters.Length) throw new IndexOutOfArrayLengthException(); JsonElement element = (JsonElement)obj.Parameters[index]; T? result = element.Deserialize(GeneralOptions); return result; } /// /// 反序列化Hashtable中Key对应的Json对象 /// /// /// /// /// internal static T? GetObject(Hashtable table, string key) { if (table.ContainsKey(key)) { JsonElement? element = (JsonElement?)table[key]; if (element != null) { T? result = ((JsonElement)element).Deserialize(GeneralOptions); return result; } } return default; } /// /// 反序列化Dictionary中Key对应的Json对象 /// /// /// /// /// internal static T? GetObject(Dictionary dict, string key) { if (dict.TryGetValue(key, out object? value)) { JsonElement? element = (JsonElement?)value; if (element != null) { T? result = ((JsonElement)element).Deserialize(GeneralOptions); return result; } } return default; } /// /// 反序列化IEnumerable中的Json对象 /// /// /// /// /// internal static T? GetObject(IEnumerable e, int index) { IEnumerable elements = e.Cast(); if (elements.Count() > index) { JsonElement? element = (JsonElement?)elements.ElementAt(index); if (element != null) { T? result = ((JsonElement)element).Deserialize(GeneralOptions); return result; } } return default; } /// /// 反序列化IEnumerable中的Json对象 /// /// /// /// /// /// internal static T? GetObject(IEnumerable e, int index, JsonSerializerOptions options) { IEnumerable elements = e.Cast(); if (elements.Count() > index) { JsonElement? element = (JsonElement?)elements.ElementAt(index); if (element != null) { T? result = ((JsonElement)element).Deserialize(options); return result; } } 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; } /// /// 反序列化Dictionary中Key对应的Json对象 /// /// /// /// /// /// internal static T? GetObject(Dictionary dict, string key, JsonSerializerOptions options) { if (dict.TryGetValue(key, out object? value)) { JsonElement? element = (JsonElement?)value; if (element != null) { T? result = ((JsonElement)element).Deserialize(options); return result; } } return default; } /// /// 反序列化多个Json对象 /// 注意必须是相同的Json对象才可以使用此方法解析 /// /// /// /// internal static List GetObjects(string json) { json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 return JsonSerializer.Deserialize>(json, GeneralOptions) ?? []; } /// /// 反序列化多个Json对象 /// 注意必须是相同的Json对象才可以使用此方法解析 /// /// /// /// /// internal static List GetObjects(string json, JsonSerializerOptions options) { json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 return JsonSerializer.Deserialize>(json, options) ?? []; } /// /// 检查字符串是否为完整的JSON对象 /// /// /// internal static bool IsCompleteJson(string json) { try { // 尝试解析JSON数据,如果成功则表示接收到完整的JSON GetObject(json); return true; } catch { // JSON解析失败,表示接收到的数据不完整 return false; } } } }