mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-10-23 03:50:30 +08:00
87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
//------------------------------------------------------------------------------
|
||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||
// 使用文档:https://kimdiego2098.github.io/
|
||
// QQ群:605534569
|
||
//------------------------------------------------------------------------------
|
||
|
||
// 版权归百小僧及百签科技(广东)有限公司所有。
|
||
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
|
||
using System.Text.Json;
|
||
|
||
namespace ThingsGateway.JsonSerialization;
|
||
|
||
/// <summary>
|
||
/// JSON 静态帮助类
|
||
/// </summary>
|
||
public static class JSON
|
||
{
|
||
/// <summary>
|
||
/// 反序列化字符串
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="json"></param>
|
||
/// <param name="jsonSerializerOptions"></param>
|
||
/// <returns></returns>
|
||
public static T Deserialize<T>(string json, object jsonSerializerOptions = default)
|
||
{
|
||
return GetJsonSerializer().Deserialize<T>(json, jsonSerializerOptions);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取 JSON 序列化提供器
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static IJsonSerializerProvider GetJsonSerializer()
|
||
{
|
||
return NetCoreApp.RootServices!.GetService<IJsonSerializerProvider>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取 JSON 配置选项
|
||
/// </summary>
|
||
/// <typeparam name="TOptions"></typeparam>
|
||
/// <returns></returns>
|
||
public static TOptions GetSerializerOptions<TOptions>()
|
||
where TOptions : class
|
||
{
|
||
return GetJsonSerializer().GetSerializerOptions() as TOptions;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查 JSON 字符串是否有效
|
||
/// </summary>
|
||
/// <param name="jsonString"></param>
|
||
/// <returns></returns>
|
||
public static bool IsValid(string jsonString)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(jsonString)) return false;
|
||
|
||
try
|
||
{
|
||
using var document = JsonDocument.Parse(jsonString);
|
||
return true;
|
||
}
|
||
catch (JsonException)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 序列化对象
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
/// <param name="jsonSerializerOptions"></param>
|
||
/// <returns></returns>
|
||
public static string Serialize(object value, object jsonSerializerOptions = default)
|
||
{
|
||
return GetJsonSerializer().Serialize(value, jsonSerializerOptions);
|
||
}
|
||
}
|