You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
1 day ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace CoreAgent.Domain.Helpers
|
||
|
{
|
||
|
public static class JsonHelper
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 对象序列化
|
||
|
/// </summary>
|
||
|
/// <param name="obj">对象</param>
|
||
|
/// <param name="isUseTextJson">是否使用textjson</param>
|
||
|
/// <returns>返回json字符串</returns>
|
||
|
public static string ObjToJson(this object obj, bool isUseTextJson = false)
|
||
|
{
|
||
|
if (isUseTextJson)
|
||
|
{
|
||
|
return System.Text.Json.JsonSerializer.Serialize(obj);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// json反序列化obj
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T">反序列类型</typeparam>
|
||
|
/// <param name="strJson">json</param>
|
||
|
/// <param name="isUseTextJson">是否使用textjson</param>
|
||
|
/// <returns>返回对象</returns>
|
||
|
public static T JsonToObj<T>(this string strJson, bool isUseTextJson = false)
|
||
|
{
|
||
|
if (isUseTextJson)
|
||
|
{
|
||
|
return System.Text.Json.JsonSerializer.Deserialize<T>(strJson);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(strJson);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|