Skip to content

Commit ba2072c

Browse files
authored
fix: json serialization and deserialization for NuCacheSerializerType (#19617)
fix: json serialization and deserialization
1 parent 49b95c1 commit ba2072c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Umbraco.PublishedCache.HybridCache/Serialization/JsonContentNestedDataSerializer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Text.Json;
1+
using System.Text.Json;
22
using System.Text.Json.Serialization;
33
using Umbraco.Cms.Core.Models;
44

@@ -8,7 +8,11 @@ internal class JsonContentNestedDataSerializer : IContentCacheDataSerializer
88
{
99
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
1010
{
11-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
11+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
12+
Converters =
13+
{
14+
new JsonObjectConverter(),
15+
},
1216
};
1317

1418
/// <inheritdoc />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
using Umbraco.Cms.Core.Models;
4+
5+
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
6+
7+
internal class JsonObjectConverter : JsonConverter<object>
8+
{
9+
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
=> reader.TokenType switch
11+
{
12+
JsonTokenType.String => reader.GetString(),
13+
JsonTokenType.Number => reader.TryGetInt64(out var l) ? l : reader.GetDouble(),
14+
JsonTokenType.True => true,
15+
JsonTokenType.False => false,
16+
JsonTokenType.Null => null,
17+
_ => JsonDocument.ParseValue(ref reader).RootElement.Clone(), // fallback for complex types
18+
};
19+
20+
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
21+
=> JsonSerializer.Serialize(writer, value, value.GetType(), options);
22+
}

0 commit comments

Comments
 (0)