Removed leftover System.Text.Json dependencies (#16040)

This commit is contained in:
Kenn Jacobsen
2024-04-15 08:51:00 +02:00
committed by GitHub
parent 2f0b3d055f
commit 576b360cce
8 changed files with 121 additions and 44 deletions

View File

@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;
namespace Umbraco.Cms.Core.Serialization;
/// <summary>
@@ -24,4 +26,14 @@ public interface IJsonSerializer
/// A <typeparamref name="T" /> representation of the JSON value.
/// </returns>
T? Deserialize<T>(string input);
/// <summary>
/// Attempts to parse an object that represents a JSON structure - i.e. a JSON object or a JSON array - to a strongly typed representation.
/// </summary>
/// <typeparam name="T">The target type of the JSON value.</typeparam>
/// <param name="input">The object input to parse.</param>
/// <param name="value">The parsed result, or null if the parsing fails.</param>
/// <returns>True if the parsing results in a non-null value, false otherwise.</returns>
bool TryDeserialize<T>(object input, [NotNullWhen(true)] out T? value)
where T : class;
}

View File

@@ -222,20 +222,21 @@ internal class ImageCropperPropertyValueEditor : DataValueEditor // TODO: core v
private ImageCropperValue? TryParseImageCropperValue(object? editorValue)
{
// FIXME: consider creating an object deserialization method on IJsonSerializer instead of relying on deserializing serialized JSON here (and likely other places as well)
if (editorValue is JsonObject jsonObject)
try
{
try
if (editorValue is null ||
_jsonSerializer.TryDeserialize(editorValue, out ImageCropperValue? imageCropperValue) is false)
{
ImageCropperValue? imageCropperValue = _jsonSerializer.Deserialize<ImageCropperValue>(jsonObject.ToJsonString());
imageCropperValue?.Prune();
return imageCropperValue;
}
catch (Exception ex)
{
// For some reason the value is invalid - log error and continue as if no value was saved
_logger.LogWarning(ex, "Could not parse editor value to an ImageCropperValue object.");
return null;
}
imageCropperValue.Prune();
return imageCropperValue;
}
catch (Exception ex)
{
// For some reason the value is invalid - log error and continue as if no value was saved
_logger.LogWarning(ex, "Could not parse editor value to an ImageCropperValue object.");
}
return null;

View File

@@ -1,4 +1,3 @@
using System.Text.Json.Nodes;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
@@ -112,14 +111,8 @@ public class MediaPicker3PropertyEditor : DataEditor
public override object? FromEditor(ContentPropertyData editorValue, object? currentValue)
{
// FIXME: consider creating an object deserialization method on IJsonSerializer instead of relying on deserializing serialized JSON here (and likely other places as well)
if (editorValue.Value is not JsonArray jsonArray)
{
return base.FromEditor(editorValue, currentValue);
}
List<MediaWithCropsDto>? mediaWithCropsDtos = _jsonSerializer.Deserialize<List<MediaWithCropsDto>>(jsonArray.ToJsonString());
if (mediaWithCropsDtos is null)
if (editorValue.Value is null ||
_jsonSerializer.TryDeserialize(editorValue.Value, out List<MediaWithCropsDto>? mediaWithCropsDtos) is false)
{
return base.FromEditor(editorValue, currentValue);
}

View File

@@ -1,7 +1,6 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Text.Json.Nodes;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
@@ -73,16 +72,9 @@ public class SliderPropertyEditor : DataEditor
}
public override object? FromEditor(ContentPropertyData editorValue, object? currentValue)
{
// FIXME: do not rely explicitly on concrete JSON implementation here - consider creating an object deserialization method on IJsonSerializer (see also MultiUrlPickerValueEditor)
if (editorValue.Value is not JsonNode jsonNode)
{
return null;
}
SliderRange? range = _jsonSerializer.Deserialize<SliderRange>(jsonNode.ToJsonString());
return range?.ToString();
}
=> editorValue.Value is not null && _jsonSerializer.TryDeserialize(editorValue.Value, out SliderRange? sliderRange)
? sliderRange.ToString()
: null;
internal class SliderRange
{

View File

@@ -5,7 +5,7 @@ using Umbraco.Cms.Core.Serialization;
namespace Umbraco.Cms.Infrastructure.Serialization;
/// <inheritdoc />
public sealed class SystemTextConfigurationEditorJsonSerializer : IConfigurationEditorJsonSerializer
public sealed class SystemTextConfigurationEditorJsonSerializer : SystemTextJsonSerializerBase, IConfigurationEditorJsonSerializer
{
private readonly JsonSerializerOptions _jsonSerializerOptions;
@@ -30,9 +30,5 @@ public sealed class SystemTextConfigurationEditorJsonSerializer : IConfiguration
}
};
/// <inheritdoc />
public string Serialize(object? input) => JsonSerializer.Serialize(input, _jsonSerializerOptions);
/// <inheritdoc />
public T? Deserialize<T>(string input) => JsonSerializer.Deserialize<T>(input, _jsonSerializerOptions);
protected override JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions;
}

View File

@@ -1,11 +1,10 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Umbraco.Cms.Core.Serialization;
namespace Umbraco.Cms.Infrastructure.Serialization;
/// <inheritdoc />
public sealed class SystemTextJsonSerializer : IJsonSerializer
public sealed class SystemTextJsonSerializer : SystemTextJsonSerializerBase
{
private readonly JsonSerializerOptions _jsonSerializerOptions;
@@ -26,9 +25,5 @@ public sealed class SystemTextJsonSerializer : IJsonSerializer
}
};
/// <inheritdoc />
public string Serialize(object? input) => JsonSerializer.Serialize(input, _jsonSerializerOptions);
/// <inheritdoc />
public T? Deserialize<T>(string input) => JsonSerializer.Deserialize<T>(input, _jsonSerializerOptions);
protected override JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions;
}

View File

@@ -0,0 +1,35 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
namespace Umbraco.Cms.Infrastructure.Serialization;
public abstract class SystemTextJsonSerializerBase : IJsonSerializer
{
protected abstract JsonSerializerOptions JsonSerializerOptions { get; }
/// <inheritdoc />
public string Serialize(object? input) => JsonSerializer.Serialize(input, JsonSerializerOptions);
/// <inheritdoc />
public T? Deserialize<T>(string input) => JsonSerializer.Deserialize<T>(input, JsonSerializerOptions);
/// <inheritdoc />
public bool TryDeserialize<T>(object input, [NotNullWhen(true)] out T? value)
where T : class
{
var jsonString = input switch
{
JsonNode jsonNodeValue => jsonNodeValue.ToJsonString(),
string stringValue when stringValue.DetectIsJson() => stringValue,
_ => null
};
value = jsonString.IsNullOrWhiteSpace()
? null
: Deserialize<T>(jsonString);
return value != null;
}
}