From e78eee50edce537f81534ff713ff3bd820409a6f Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 30 Jun 2025 13:56:51 +0200 Subject: [PATCH] Minor refactor following merge of PR #19617. --- .../IContentCacheDataSerializer.cs | 8 +++--- .../JsonContentNestedDataSerializer.cs | 26 +++++++++++++++++++ .../Serialization/JsonObjectConverter.cs | 22 ---------------- .../MsgPackContentNestedDataSerializer.cs | 20 ++++++++------ 4 files changed, 42 insertions(+), 34 deletions(-) delete mode 100644 src/Umbraco.PublishedCache.HybridCache/Serialization/JsonObjectConverter.cs diff --git a/src/Umbraco.PublishedCache.HybridCache/Serialization/IContentCacheDataSerializer.cs b/src/Umbraco.PublishedCache.HybridCache/Serialization/IContentCacheDataSerializer.cs index a46c667a4d..91c32d6161 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Serialization/IContentCacheDataSerializer.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Serialization/IContentCacheDataSerializer.cs @@ -1,9 +1,9 @@ -using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization; /// -/// Serializes/Deserializes document to the SQL Database as a string +/// Serializes/Deserializes document to the SQL Database as a string. /// /// /// Resolved from the . This cannot be resolved from DI. @@ -11,12 +11,12 @@ namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization; internal interface IContentCacheDataSerializer { /// - /// Deserialize the data into a + /// Deserialize the data into a . /// ContentCacheDataModel? Deserialize(IReadOnlyContentBase content, string? stringData, byte[]? byteData, bool published); /// - /// Serializes the + /// Serializes the . /// ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model, bool published); } diff --git a/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonContentNestedDataSerializer.cs b/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonContentNestedDataSerializer.cs index cfe1fb2425..1bb4822ced 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonContentNestedDataSerializer.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonContentNestedDataSerializer.cs @@ -4,6 +4,9 @@ using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization; +/// +/// Serializes/deserializes documents to the SQL Database as JSON. +/// internal class JsonContentNestedDataSerializer : IContentCacheDataSerializer { private static readonly JsonSerializerOptions _jsonSerializerOptions = new() @@ -40,4 +43,27 @@ internal class JsonContentNestedDataSerializer : IContentCacheDataSerializer var json = JsonSerializer.Serialize(model, _jsonSerializerOptions); return new ContentCacheDataSerializationResult(json, null); } + + /// + /// Provides a converter for handling JSON objects that can be of various types (string, number, boolean, null, or complex types). + /// + internal class JsonObjectConverter : JsonConverter + { + /// + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => reader.TokenType switch + { + JsonTokenType.String => reader.GetString(), + JsonTokenType.Number => reader.TryGetInt64(out var value) ? value : 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); + } + } diff --git a/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonObjectConverter.cs b/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonObjectConverter.cs deleted file mode 100644 index a28ccf0562..0000000000 --- a/src/Umbraco.PublishedCache.HybridCache/Serialization/JsonObjectConverter.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; -using Umbraco.Cms.Core.Models; - -namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization; - -internal class JsonObjectConverter : JsonConverter -{ - 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); -} diff --git a/src/Umbraco.PublishedCache.HybridCache/Serialization/MsgPackContentNestedDataSerializer.cs b/src/Umbraco.PublishedCache.HybridCache/Serialization/MsgPackContentNestedDataSerializer.cs index ec4e047d29..9962b07da9 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Serialization/MsgPackContentNestedDataSerializer.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Serialization/MsgPackContentNestedDataSerializer.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Text; using K4os.Compression.LZ4; using MessagePack; using MessagePack.Resolvers; @@ -8,14 +8,17 @@ using Umbraco.Cms.Core.PropertyEditors; namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization; /// -/// Serializes/Deserializes document to the SQL Database as bytes using -/// MessagePack +/// Serializes/deserializes documents to the SQL Database as bytes using +/// MessagePack. /// internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSerializer { private readonly MessagePackSerializerOptions _options; private readonly IPropertyCacheCompression _propertyOptions; + /// + /// Initializes a new instance of the class. + /// public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOptions) { _propertyOptions = propertyOptions ?? throw new ArgumentNullException(nameof(propertyOptions)); @@ -40,6 +43,7 @@ internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSeri .WithSecurity(MessagePackSecurity.UntrustedData); } + /// public ContentCacheDataModel? Deserialize(IReadOnlyContentBase content, string? stringData, byte[]? byteData, bool published) { if (byteData != null) @@ -62,6 +66,7 @@ internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSeri return null; } + /// public ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model, bool published) { Compress(content, model, published); @@ -69,11 +74,10 @@ internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSeri return new ContentCacheDataSerializationResult(null, bytes); } - public string ToJson(byte[] bin) - { - var json = MessagePackSerializer.ConvertToJson(bin, _options); - return json; - } + /// + /// Converts the binary MessagePack data to a JSON string representation. + /// + public string ToJson(byte[] bin) => MessagePackSerializer.ConvertToJson(bin, _options); /// /// Used during serialization to compress properties