Use Json Serializer in cache again

This commit is contained in:
2025-12-02 22:59:43 +08:00
parent 9addf38677
commit b364edc74b
3 changed files with 28 additions and 11 deletions

View File

@@ -1,16 +1,31 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using DysonNetwork.Shared.Data;
using NodaTime;
using NodaTime.Serialization.SystemTextJson;
namespace DysonNetwork.Shared.Cache;
public class JsonCacheSerializer(JsonSerializerOptions? options = null) : ICacheSerializer
public class JsonCacheSerializer : ICacheSerializer
{
private readonly JsonSerializerOptions _options = options ?? new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
// Customize as needed (NodaTime, camelCase, converters, etc.)
WriteIndented = false
};
private readonly JsonSerializerOptions _options;
// Customize as needed (NodaTime, camelCase, converters, etc.)
public JsonCacheSerializer()
{
_options = new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { JsonExtensions.UnignoreAllProperties() },
},
ReferenceHandler = ReferenceHandler.Preserve,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Converters = { new ByteStringConverter() }
};
_options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
_options.PropertyNameCaseInsensitive = true;
}
public string Serialize<T>(T value)
=> JsonSerializer.Serialize(value, _options);