Skip to content

Commit

Permalink
Synchronization code
Browse files Browse the repository at this point in the history
  • Loading branch information
DotNetNext committed Apr 25, 2023
1 parent a9c47a8 commit 6b23a51
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;

namespace SqlSugar
{
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);
}

Expand All @@ -25,6 +38,20 @@ public string SugarSerializeObject(object value)

public T DeserializeObject<T>(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<T>(value, jSetting);
}
Expand Down

0 comments on commit 6b23a51

Please sign in to comment.