diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index 0a57d38..c4bbf10 100644 --- a/Api/Transmittal/DataRequest.cs +++ b/Api/Transmittal/DataRequest.cs @@ -10,7 +10,17 @@ namespace Milimoe.FunGame.Core.Api.Transmittal { public RequestResult Result => Worker.Result; public string Error => Worker.Error; - public object? this[string key] => Worker.ResultData[key]; + public object? this[string key] + { + get + { + return Worker.ResultData[key]; + } + set + { + AddRequestData(key, value); + } + } private readonly Request Worker; @@ -19,9 +29,10 @@ namespace Milimoe.FunGame.Core.Api.Transmittal Worker = new(Socket, RequestType); } - public void AddRequestData(string key, object value) + public void AddRequestData(string key, object? value) { - Worker.RequestData.Add(key, value); + if (Worker.RequestData.ContainsKey(key)) Worker.RequestData[key] = value; + else Worker.RequestData.Add(key, value); } public RequestResult SendRequest() diff --git a/Library/Common/Network/SocketObject.cs b/Library/Common/Network/SocketObject.cs index bcb5fe9..c7fcfc7 100644 --- a/Library/Common/Network/SocketObject.cs +++ b/Library/Common/Network/SocketObject.cs @@ -47,10 +47,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network /// 索引超过数组上限 public T? GetParam(int index) { - if (index >= Parameters.Length) throw new IndexOutOfArrayLengthException(); - object obj = Parameters[index]; - T? result = JsonManager.GetObject(obj.ToString() ?? ""); - return result; + return JsonManager.GetObject(this, index); } } } diff --git a/Service/JsonManager.cs b/Service/JsonManager.cs index 619dfb9..225b9e7 100644 --- a/Service/JsonManager.cs +++ b/Service/JsonManager.cs @@ -1,10 +1,11 @@ using System.Text.Json; using System.Text.Json.Serialization; using Milimoe.FunGame.Core.Library.Common.JsonConverter; +using Milimoe.FunGame.Core.Library.Common.Network; namespace Milimoe.FunGame.Core.Service { - public class JsonManager + internal class JsonManager { private static bool _IsFirst = true; private readonly static JsonSerializerOptions _GeneralOptions = new() @@ -46,7 +47,7 @@ namespace Milimoe.FunGame.Core.Service /// /// /// - public static string GetString(T obj) + internal static string GetString(T obj) { return JsonSerializer.Serialize(obj, GeneralOptions); } @@ -57,7 +58,7 @@ namespace Milimoe.FunGame.Core.Service /// /// /// - public static T? GetObject(string json) + internal static T? GetObject(string json) { return JsonSerializer.Deserialize(json, GeneralOptions); } @@ -67,7 +68,7 @@ namespace Milimoe.FunGame.Core.Service /// /// /// - public static object? GetObject(string json) + internal static object? GetObject(string json) { return JsonSerializer.Deserialize(json, GeneralOptions); } @@ -79,10 +80,26 @@ namespace Milimoe.FunGame.Core.Service /// /// /// - public static List GetObjects(string json) + internal static List GetObjects(string json) { json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 return JsonSerializer.Deserialize>(json, GeneralOptions) ?? new List(); } + + /// + /// 反序列化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; + } } }