using Newtonsoft.Json;
|
|
namespace WIDESEAWCS_RedisService.Serialization
|
{
|
public class NewtonsoftRedisSerializer : IRedisSerializer
|
{
|
private static readonly JsonSerializerSettings _settings = new()
|
{
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
NullValueHandling = NullValueHandling.Ignore,
|
DateFormatString = "yyyy-MM-dd HH:mm:ss"
|
};
|
|
public string Serialize<T>(T value)
|
{
|
return JsonConvert.SerializeObject(value, _settings);
|
}
|
|
public T? Deserialize<T>(string value)
|
{
|
if (string.IsNullOrEmpty(value)) return default;
|
return JsonConvert.DeserializeObject<T>(value, _settings);
|
}
|
|
public object? Deserialize(string value, Type type)
|
{
|
if (string.IsNullOrEmpty(value)) return null;
|
return JsonConvert.DeserializeObject(value, type, _settings);
|
}
|
}
|
}
|