Files
ThingsGateway/src/tools/ThingsGateway.ASPNetCore/JsonSerialization/JSON.cs
Diego 1fd6cc2b5e release:6.0.5.9
refactor: 封装furion swagger文档
refactor:简化代码
refactor:更新依赖包
2024-08-18 04:03:01 +08:00

87 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人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);
}
}