Skip to content

Commit e78eee5

Browse files
committed
Minor refactor following merge of PR #19617.
1 parent ba2072c commit e78eee5

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
using Umbraco.Cms.Core.Models;
1+
using Umbraco.Cms.Core.Models;
22

33
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
44

55
/// <summary>
6-
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as a string
6+
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as a string.
77
/// </summary>
88
/// <remarks>
99
/// Resolved from the <see cref="IContentCacheDataSerializerFactory" />. This cannot be resolved from DI.
1010
/// </remarks>
1111
internal interface IContentCacheDataSerializer
1212
{
1313
/// <summary>
14-
/// Deserialize the data into a <see cref="ContentCacheDataModel" />
14+
/// Deserialize the data into a <see cref="ContentCacheDataModel" />.
1515
/// </summary>
1616
ContentCacheDataModel? Deserialize(IReadOnlyContentBase content, string? stringData, byte[]? byteData, bool published);
1717

1818
/// <summary>
19-
/// Serializes the <see cref="ContentCacheDataModel" />
19+
/// Serializes the <see cref="ContentCacheDataModel" />.
2020
/// </summary>
2121
ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model, bool published);
2222
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
66

7+
/// <summary>
8+
/// Serializes/deserializes <see cref="ContentCacheDataModel" /> documents to the SQL Database as JSON.
9+
/// </summary>
710
internal class JsonContentNestedDataSerializer : IContentCacheDataSerializer
811
{
912
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
@@ -40,4 +43,27 @@ public ContentCacheDataSerializationResult Serialize(
4043
var json = JsonSerializer.Serialize(model, _jsonSerializerOptions);
4144
return new ContentCacheDataSerializationResult(json, null);
4245
}
46+
47+
/// <summary>
48+
/// Provides a converter for handling JSON objects that can be of various types (string, number, boolean, null, or complex types).
49+
/// </summary>
50+
internal class JsonObjectConverter : JsonConverter<object>
51+
{
52+
/// <inheritdoc/>
53+
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
54+
=> reader.TokenType switch
55+
{
56+
JsonTokenType.String => reader.GetString(),
57+
JsonTokenType.Number => reader.TryGetInt64(out var value) ? value : reader.GetDouble(),
58+
JsonTokenType.True => true,
59+
JsonTokenType.False => false,
60+
JsonTokenType.Null => null,
61+
_ => JsonDocument.ParseValue(ref reader).RootElement.Clone(), // fallback for complex types
62+
};
63+
64+
/// <inheritdoc/>
65+
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
66+
=> JsonSerializer.Serialize(writer, value, value.GetType(), options);
67+
}
68+
4369
}

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Text;
1+
using System.Text;
22
using K4os.Compression.LZ4;
33
using MessagePack;
44
using MessagePack.Resolvers;
@@ -8,14 +8,17 @@
88
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
99

1010
/// <summary>
11-
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as bytes using
12-
/// MessagePack
11+
/// Serializes/deserializes <see cref="ContentCacheDataModel" /> documents to the SQL Database as bytes using
12+
/// MessagePack.
1313
/// </summary>
1414
internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSerializer
1515
{
1616
private readonly MessagePackSerializerOptions _options;
1717
private readonly IPropertyCacheCompression _propertyOptions;
1818

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="MsgPackContentNestedDataSerializer"/> class.
21+
/// </summary>
1922
public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOptions)
2023
{
2124
_propertyOptions = propertyOptions ?? throw new ArgumentNullException(nameof(propertyOptions));
@@ -40,6 +43,7 @@ public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOpti
4043
.WithSecurity(MessagePackSecurity.UntrustedData);
4144
}
4245

46+
/// <inheritdoc/>
4347
public ContentCacheDataModel? Deserialize(IReadOnlyContentBase content, string? stringData, byte[]? byteData, bool published)
4448
{
4549
if (byteData != null)
@@ -62,18 +66,18 @@ public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOpti
6266
return null;
6367
}
6468

69+
/// <inheritdoc/>
6570
public ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model, bool published)
6671
{
6772
Compress(content, model, published);
6873
var bytes = MessagePackSerializer.Serialize(model, _options);
6974
return new ContentCacheDataSerializationResult(null, bytes);
7075
}
7176

72-
public string ToJson(byte[] bin)
73-
{
74-
var json = MessagePackSerializer.ConvertToJson(bin, _options);
75-
return json;
76-
}
77+
/// <summary>
78+
/// Converts the binary MessagePack data to a JSON string representation.
79+
/// </summary>
80+
public string ToJson(byte[] bin) => MessagePackSerializer.ConvertToJson(bin, _options);
7781

7882
/// <summary>
7983
/// Used during serialization to compress properties

0 commit comments

Comments
 (0)