fix: json serialization and deserialization for NuCacheSerializerType (#19617)

fix: json serialization and deserialization
This commit is contained in:
Davor Zlotrg
2025-06-30 14:07:00 +02:00
committed by GitHub
parent 49b95c1225
commit ba2072c979
2 changed files with 28 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Umbraco.Cms.Core.Models;
@@ -8,7 +8,11 @@ internal class JsonContentNestedDataSerializer : IContentCacheDataSerializer
{
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new JsonObjectConverter(),
},
};
/// <inheritdoc />

View File

@@ -0,0 +1,22 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
internal class JsonObjectConverter : JsonConverter<object>
{
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType switch
{
JsonTokenType.String => reader.GetString(),
JsonTokenType.Number => reader.TryGetInt64(out var l) ? l : reader.GetDouble(),
JsonTokenType.True => true,
JsonTokenType.False => false,
JsonTokenType.Null => null,
_ => JsonDocument.ParseValue(ref reader).RootElement.Clone(), // fallback for complex types
};
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
=> JsonSerializer.Serialize(writer, value, value.GetType(), options);
}