123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System.Text.Json;
- using CacheManager.Core.Internal;
- namespace Hotline.CacheManager
- {
- public class SystemTextJsonSerializer: CacheSerializer
- {
- private static readonly Type OpenGenericType = typeof(JsonCacheItem<>);
- private readonly JsonSerializerOptions _jsonSerializerOptions;
- public SystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)
- {
- _jsonSerializerOptions = jsonSerializerOptions;
- }
- public SystemTextJsonSerializer(): this(new JsonSerializerOptions(JsonSerializerDefaults.Web))
- {
-
- }
- protected override Type GetOpenGeneric()
- {
- return OpenGenericType;
- }
- protected override object CreateNewItem<TCacheValue>(ICacheItemProperties properties, object value)
- {
- return new JsonCacheItem<TCacheValue>(properties, value);
- }
- public override byte[] Serialize<T>(T value)
- {
- return JsonSerializer.SerializeToUtf8Bytes(value, _jsonSerializerOptions);
- }
- public override object Deserialize(byte[] data, Type target)
- {
- return JsonSerializer.Deserialize(data, target, _jsonSerializerOptions);
- }
- }
- }
|