wanshenmean
2026-03-11 a8f45091019012eeafec563913dee71cda3d9790
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
        }
    }
}