Update DataRequest Setter (#28)

* Update DataRequest Setter

* Fixed some json bug (#29)

* JsonManager应是internal的

---------

Co-authored-by: milimoe <110188673+milimoe@users.noreply.github.com>
This commit is contained in:
Yezi 2023-06-05 00:31:45 +08:00 committed by GitHub
parent 23c9ade79e
commit 133fef14d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -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()

View File

@ -47,10 +47,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
public T? GetParam<T>(int index)
{
if (index >= Parameters.Length) throw new IndexOutOfArrayLengthException();
object obj = Parameters[index];
T? result = JsonManager.GetObject<T>(obj.ToString() ?? "");
return result;
return JsonManager.GetObject<T>(this, index);
}
}
}

View File

@ -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
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetString<T>(T obj)
internal static string GetString<T>(T obj)
{
return JsonSerializer.Serialize(obj, GeneralOptions);
}
@ -57,7 +58,7 @@ namespace Milimoe.FunGame.Core.Service
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T? GetObject<T>(string json)
internal static T? GetObject<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, GeneralOptions);
}
@ -67,7 +68,7 @@ namespace Milimoe.FunGame.Core.Service
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static object? GetObject(string json)
internal static object? GetObject(string json)
{
return JsonSerializer.Deserialize<object>(json, GeneralOptions);
}
@ -79,10 +80,26 @@ namespace Milimoe.FunGame.Core.Service
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static List<T> GetObjects<T>(string json)
internal static List<T> GetObjects<T>(string json)
{
json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组
return JsonSerializer.Deserialize<List<T>>(json, GeneralOptions) ?? new List<T>();
}
/// <summary>
/// 反序列化SocketObject中索引为index的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfArrayLengthException"></exception>
internal static T? GetObject<T>(SocketObject obj, int index)
{
if (index >= obj.Parameters.Length) throw new IndexOutOfArrayLengthException();
JsonElement element = (JsonElement)obj.Parameters[index];
T? result = element.Deserialize<T>(GeneralOptions);
return result;
}
}
}