2022-12-21 14:29:59 +01:00
|
|
|
|
using System.Text.Json;
|
2023-02-18 13:41:54 +01:00
|
|
|
|
using System.Text.Json.Serialization;
|
2022-12-21 14:29:59 +01:00
|
|
|
|
using Umbraco.Cms.Core.Serialization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Infrastructure.Serialization;
|
|
|
|
|
|
|
2023-02-18 13:41:54 +01:00
|
|
|
|
// FIXME: clean up all config editor serializers when we can migrate fully to System.Text.Json
|
2022-12-21 14:29:59 +01:00
|
|
|
|
// - move this implementation to ConfigurationEditorJsonSerializer (delete the old implementation)
|
|
|
|
|
|
// - use this implementation as the registered singleton (delete ContextualConfigurationEditorJsonSerializer)
|
|
|
|
|
|
// - reuse the JsonObjectConverter implementation from management API (delete the local implementation - pending V12 branch update)
|
|
|
|
|
|
|
|
|
|
|
|
public class SystemTextConfigurationEditorJsonSerializer : IConfigurationEditorJsonSerializer
|
|
|
|
|
|
{
|
|
|
|
|
|
private JsonSerializerOptions _jsonSerializerOptions;
|
|
|
|
|
|
|
|
|
|
|
|
public SystemTextConfigurationEditorJsonSerializer()
|
|
|
|
|
|
{
|
|
|
|
|
|
_jsonSerializerOptions = new JsonSerializerOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
|
// in some cases, configs aren't camel cased in the DB, so we have to resort to case insensitive
|
|
|
|
|
|
// property name resolving when creating configuration objects (deserializing DB configs)
|
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
2023-02-18 13:41:54 +01:00
|
|
|
|
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
2022-12-21 14:29:59 +01:00
|
|
|
|
};
|
2023-02-18 13:41:54 +01:00
|
|
|
|
_jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
2022-12-21 14:29:59 +01:00
|
|
|
|
_jsonSerializerOptions.Converters.Add(new JsonObjectConverter());
|
2023-02-22 11:28:21 +01:00
|
|
|
|
_jsonSerializerOptions.Converters.Add(new JsonUdiConverter());
|
|
|
|
|
|
_jsonSerializerOptions.Converters.Add(new JsonGuidUdiConverter());
|
2023-09-04 14:24:37 +02:00
|
|
|
|
_jsonSerializerOptions.Converters.Add(new JsonBoolConverter());
|
2022-12-21 14:29:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Serialize(object? input) => JsonSerializer.Serialize(input, _jsonSerializerOptions);
|
|
|
|
|
|
|
|
|
|
|
|
public T? Deserialize<T>(string input) => JsonSerializer.Deserialize<T>(input, _jsonSerializerOptions);
|
|
|
|
|
|
|
|
|
|
|
|
public T? DeserializeSubset<T>(string input, string key) => throw new NotSupportedException();
|
|
|
|
|
|
}
|