mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Support System.Text.Json
This commit is contained in:
parent
4e4d4b7837
commit
b861689c1d
@ -1,10 +1,11 @@
|
|||||||
using Newtonsoft.Json;
|
using NetTaste;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Converters;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
@ -12,6 +13,19 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public string SerializeObject(object value)
|
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);
|
return JsonConvert.SerializeObject(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +39,20 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public T DeserializeObject<T>(string 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 };
|
var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
|
||||||
return JsonConvert.DeserializeObject<T>(value, jSetting);
|
return JsonConvert.DeserializeObject<T>(value, jSetting);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user