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.
51 lines
1.7 KiB
51 lines
1.7 KiB
5 days ago
|
namespace Admin.NET.Core;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Newtonsoft.Json 实现
|
||
|
/// </summary>
|
||
|
public class JsonSerializerProvider : IJsonSerializerProvider, ISingleton
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 序列化对象
|
||
|
/// </summary>
|
||
|
/// <param name="value"></param>
|
||
|
/// <param name="jsonSerializerOptions"></param>
|
||
|
/// <returns></returns>
|
||
|
public string Serialize(object value, object jsonSerializerOptions = null)
|
||
|
{
|
||
|
return JsonConvert.SerializeObject(value, (jsonSerializerOptions ?? GetSerializerOptions()) as JsonSerializerSettings);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 反序列化字符串
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
/// <param name="json"></param>
|
||
|
/// <param name="jsonSerializerOptions"></param>
|
||
|
/// <returns></returns>
|
||
|
public T Deserialize<T>(string json, object jsonSerializerOptions = null)
|
||
|
{
|
||
|
return JsonConvert.DeserializeObject<T>(json, (jsonSerializerOptions ?? GetSerializerOptions()) as JsonSerializerSettings);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 反序列化字符串
|
||
|
/// </summary>
|
||
|
/// <param name="json"></param>
|
||
|
/// <param name="returnType"></param>
|
||
|
/// <param name="jsonSerializerOptions"></param>
|
||
|
/// <returns></returns>
|
||
|
public object Deserialize(string json, Type returnType, object jsonSerializerOptions = null)
|
||
|
{
|
||
|
return JsonConvert.DeserializeObject(json, returnType, (jsonSerializerOptions ?? GetSerializerOptions()) as JsonSerializerSettings);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 返回读取全局配置的 JSON 选项
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public object GetSerializerOptions()
|
||
|
{
|
||
|
return App.GetOptions<MvcNewtonsoftJsonOptions>()?.SerializerSettings;
|
||
|
}
|
||
|
}
|