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.
 
 

46 lines
1.3 KiB

using Newtonsoft.Json;
using System.Text;
namespace Znyc.CloudCar.Utility.Extensions
{
/// <summary>
/// 字符串序列化类(用于redis数据传递保持编码格式统一)
/// </summary>
public static class SerializeExtensions
{
/// <summary>
/// 序列化
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public static byte[] Serialize(object item)
{
var jsonString = JsonConvert.SerializeObject(item);
return Encoding.UTF8.GetBytes(jsonString);
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static TEntity Deserialize<TEntity>(byte[] value)
{
if (value == null)
{
#pragma warning disable CS8603 // 可能返回 null 引用。
return default;
#pragma warning restore CS8603 // 可能返回 null 引用。
}
var jsonString = Encoding.UTF8.GetString(value);
#pragma warning disable CS8603 // 可能返回 null 引用。
return JsonConvert.DeserializeObject<TEntity>(jsonString);
#pragma warning restore CS8603 // 可能返回 null 引用。
}
}
}