diff --git a/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs b/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs index 93eb13c1a7..99ecca25d0 100644 --- a/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs +++ b/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using System.Text; namespace SqlSugar { @@ -12,6 +12,19 @@ public class SerializeService : ISerializeService { public string SerializeObject(object value) { + if (value != null && value.GetType().FullName.StartsWith("System.Text.Json.")) + { + // 动态创建一个 JsonSerializer 实例 + Type serializerType = value.GetType().Assembly.GetType("System.Text.Json.JsonSerializer"); + + var methods = serializerType + .GetMyMethod("Serialize", 2); + + // 调用 SerializeObject 方法序列化对象 + string json = (string)methods.MakeGenericMethod(value.GetType()) + .Invoke(null, new object[] { value,null }); + return json; + } return JsonConvert.SerializeObject(value); } @@ -25,6 +38,20 @@ public string SugarSerializeObject(object value) public T DeserializeObject(string value) { + if (typeof(T).FullName.StartsWith("System.Text.Json.")) + { + // 动态创建一个 JsonSerializer 实例 + Type serializerType =typeof(T).Assembly.GetType("System.Text.Json.JsonSerializer"); + + var methods = serializerType + .GetMethods().Where(it=>it.Name== "Deserialize") + .Where(it=>it.GetParameters().Any(z=>z.ParameterType==typeof(string))).First(); + + // 调用 SerializeObject 方法序列化对象 + T json = (T)methods.MakeGenericMethod(typeof(T)) + .Invoke(null, new object[] { value, null }); + return json; + } var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; return JsonConvert.DeserializeObject(value, jSetting); }