From 9fb45dc502d98bf72e886f876ac600faa9dcf7c8 Mon Sep 17 00:00:00 2001
From: milimoe <110188673+milimoe@users.noreply.github.com>
Date: Thu, 27 Jul 2023 09:10:29 +0800
Subject: [PATCH] =?UTF-8?q?=E5=90=91Utility=E6=B7=BB=E5=8A=A0=E6=9B=B4?=
=?UTF-8?q?=E5=A4=9AJson=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95=20(#45)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Api/Utility/General.cs | 40 ++++++++++++++++++++++-
Service/JsonManager.cs | 72 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 111 insertions(+), 1 deletion(-)
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