< Summary

Information
Class: Elsa.Common.Serialization.ConfigurableSerializer
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/ConfigurableSerializer.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 124
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ServiceProvider()100%11100%
GetOptions()100%22100%
ApplyOptions(...)100%11100%
GetOptionsInternal()100%11100%
CreateOptionsInternal()100%11100%
Configure(...)100%11100%
AddConverters(...)100%11100%
RunConfigurators(...)100%22100%
CreateInstance()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/ConfigurableSerializer.cs

#LineLine coverage
 1using System.Text.Encodings.Web;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using System.Text.Json.Serialization.Metadata;
 5using System.Text.Unicode;
 6using Elsa.Common.Converters;
 7using Microsoft.Extensions.DependencyInjection;
 8
 9namespace Elsa.Common.Serialization;
 10
 11/// <summary>
 12/// A base class for configurable JSON serializers.
 13/// </summary>
 14public abstract class ConfigurableSerializer
 15{
 16    private JsonSerializerOptions? _options;
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="ConfigurableSerializer"/> class.
 20    /// </summary>
 1821    protected ConfigurableSerializer(IServiceProvider serviceProvider)
 22    {
 1823        ServiceProvider = serviceProvider;
 1824    }
 25
 26    /// <summary>
 27    /// Gets the service provider.
 28    /// </summary>
 1621329    protected IServiceProvider ServiceProvider { get; }
 30
 31    /// <summary>
 32    /// Creates a new instance of <see cref="JsonSerializerOptions"/> with the configured options.
 33    /// </summary>
 34    public virtual JsonSerializerOptions GetOptions()
 35    {
 443736        if (_options != null)
 443337            return _options;
 38
 439        var options = CreateOptionsInternal();
 440        ApplyOptions(options);
 441        _options = options;
 442        return options;
 43    }
 44
 45    /// <summary>
 46    /// Creates a new instance of <see cref="JsonSerializerOptions"/> with the configured options.
 47    /// </summary>
 48    public virtual void ApplyOptions(JsonSerializerOptions options)
 49    {
 270550        Configure(options);
 270551        AddConverters(options);
 270552        RunConfigurators(options);
 270553    }
 54
 55    /// <summary>
 56    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 57    /// </summary>
 58    protected JsonSerializerOptions GetOptionsInternal()
 59    {
 270160        var options = CreateOptionsInternal();
 270161        ApplyOptions(options);
 270162        _options = options;
 270163        return options;
 64    }
 65
 66    /// <summary>
 67    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 68    /// </summary>
 69    private static JsonSerializerOptions CreateOptionsInternal()
 70    {
 270571        var options = new JsonSerializerOptions
 270572        {
 270573            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 270574            PropertyNameCaseInsensitive = true,
 270575            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
 270576            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 270577        };
 78
 270579        options.Converters.Add(new JsonStringEnumConverter());
 270580        options.Converters.Add(JsonMetadataServices.TimeSpanConverter);
 270581        options.Converters.Add(new IntegerJsonConverter());
 270582        options.Converters.Add(new BigIntegerJsonConverter());
 270583        options.Converters.Add(new DecimalJsonConverter());
 84
 270585        return options;
 86    }
 87
 88    /// <summary>
 89    /// Configures the <see cref="JsonSerializerOptions"/> object.
 90    /// </summary>
 91    protected virtual void Configure(JsonSerializerOptions options)
 92    {
 270493    }
 94
 95    /// <summary>
 96    /// Adds additional <see cref="JsonConverter"/> objects.
 97    /// </summary>
 98    protected virtual void AddConverters(JsonSerializerOptions options)
 99    {
 1100    }
 101
 102    /// <summary>
 103    /// Give external packages a chance to further configure the serializer options.
 104    /// </summary>
 105    protected virtual void RunConfigurators(JsonSerializerOptions options)
 106    {
 2705107        var configurators = ServiceProvider.GetServices<ISerializationOptionsConfigurator>();
 2705108        var modifiers = new List<Action<JsonTypeInfo>>();
 109
 37870110        foreach (var configurator in configurators)
 111        {
 16230112            configurator.Configure(options);
 16230113            var modifiersToAdd = configurator.GetModifiers();
 16230114            modifiers.AddRange(modifiersToAdd);
 115        }
 116
 2705117        options.TypeInfoResolver = new ModifiableJsonTypeInfoResolver(modifiers);
 2705118    }
 119
 120    /// <summary>
 121    /// Creates an instance of the specified type using the service provider.
 122    /// </summary>
 13507123    protected T CreateInstance<T>() => ActivatorUtilities.CreateInstance<T>(ServiceProvider);
 124}