为Object新增toJson扩展方法

This commit is contained in:
falcon 2023-05-05 13:54:27 +08:00
parent e442a099e6
commit 4db845412d

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text.Json;
namespace Falcon.SugarApi
{
@ -126,6 +127,20 @@ namespace Falcon.SugarApi
}
return obj;
}
/// <summary>
/// 将对象转换为Json字符串格式
/// </summary>
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="OptionBuilder">转换参数</param>
/// <returns>转换后字符串</returns>
public static string ToJson<T>(this T obj,Action<JsonSerializerOptions>? OptionBuilder = null) {
var option = new JsonSerializerOptions();
OptionBuilder?.Invoke(option);
var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option);
return ser.Serialize(obj);
}
}
/// <summary>