Minor refactor following merge of PR #19617.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
|
||||
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as a string
|
||||
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as a string.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Resolved from the <see cref="IContentCacheDataSerializerFactory" />. This cannot be resolved from DI.
|
||||
@@ -11,12 +11,12 @@ namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
|
||||
internal interface IContentCacheDataSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Deserialize the data into a <see cref="ContentCacheDataModel" />
|
||||
/// Deserialize the data into a <see cref="ContentCacheDataModel" />.
|
||||
/// </summary>
|
||||
ContentCacheDataModel? Deserialize(IReadOnlyContentBase content, string? stringData, byte[]? byteData, bool published);
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the <see cref="ContentCacheDataModel" />
|
||||
/// Serializes the <see cref="ContentCacheDataModel" />.
|
||||
/// </summary>
|
||||
ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model, bool published);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ using Umbraco.Cms.Core.Models;
|
||||
|
||||
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Serializes/deserializes <see cref="ContentCacheDataModel" /> documents to the SQL Database as JSON.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a converter for handling JSON objects that can be of various types (string, number, boolean, null, or complex types).
|
||||
/// </summary>
|
||||
internal class JsonObjectConverter : JsonConverter<object>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
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
|
||||
};
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||
=> JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Serializes/Deserializes <see cref="ContentCacheDataModel" /> document to the SQL Database as bytes using
|
||||
/// MessagePack
|
||||
/// Serializes/deserializes <see cref="ContentCacheDataModel" /> documents to the SQL Database as bytes using
|
||||
/// MessagePack.
|
||||
/// </summary>
|
||||
internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSerializer
|
||||
{
|
||||
private readonly MessagePackSerializerOptions _options;
|
||||
private readonly IPropertyCacheCompression _propertyOptions;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MsgPackContentNestedDataSerializer"/> class.
|
||||
/// </summary>
|
||||
public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOptions)
|
||||
{
|
||||
_propertyOptions = propertyOptions ?? throw new ArgumentNullException(nameof(propertyOptions));
|
||||
@@ -40,6 +43,7 @@ internal sealed class MsgPackContentNestedDataSerializer : IContentCacheDataSeri
|
||||
.WithSecurity(MessagePackSecurity.UntrustedData);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts the binary MessagePack data to a JSON string representation.
|
||||
/// </summary>
|
||||
public string ToJson(byte[] bin) => MessagePackSerializer.ConvertToJson(bin, _options);
|
||||
|
||||
/// <summary>
|
||||
/// Used during serialization to compress properties
|
||||
|
||||
Reference in New Issue
Block a user