< 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>
 4221    protected ConfigurableSerializer(IServiceProvider serviceProvider)
 22    {
 4223        ServiceProvider = serviceProvider;
 4224    }
 25
 26    /// <summary>
 27    /// Gets the service provider.
 28    /// </summary>
 1025129    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    {
 7653636        if (_options != null)
 7653037            return _options;
 38
 639        var options = CreateOptionsInternal();
 640        ApplyOptions(options);
 641        _options = options;
 642        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    {
 171350        Configure(options);
 171351        AddConverters(options);
 171352        RunConfigurators(options);
 171353    }
 54
 55    /// <summary>
 56    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 57    /// </summary>
 58    protected JsonSerializerOptions GetOptionsInternal()
 59    {
 170760        var options = CreateOptionsInternal();
 170761        ApplyOptions(options);
 170762        _options = options;
 170763        return options;
 64    }
 65
 66    /// <summary>
 67    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 68    /// </summary>
 69    private static JsonSerializerOptions CreateOptionsInternal()
 70    {
 171371        var options = new JsonSerializerOptions
 171372        {
 171373            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 171374            PropertyNameCaseInsensitive = true,
 171375            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
 171376            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 171377        };
 78
 171379        options.Converters.Add(new JsonStringEnumConverter());
 171380        options.Converters.Add(JsonMetadataServices.TimeSpanConverter);
 171381        options.Converters.Add(new IntegerJsonConverter());
 171382        options.Converters.Add(new BigIntegerJsonConverter());
 171383        options.Converters.Add(new DecimalJsonConverter());
 84
 171385        return options;
 86    }
 87
 88    /// <summary>
 89    /// Configures the <see cref="JsonSerializerOptions"/> object.
 90    /// </summary>
 91    protected virtual void Configure(JsonSerializerOptions options)
 92    {
 171293    }
 94
 95    /// <summary>
 96    /// Adds additional <see cref="JsonConverter"/> objects.
 97    /// </summary>
 98    protected virtual void AddConverters(JsonSerializerOptions options)
 99    {
 3100    }
 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    {
 1713107        var configurators = ServiceProvider.GetServices<ISerializationOptionsConfigurator>();
 1713108        var modifiers = new List<Action<JsonTypeInfo>>();
 109
 23982110        foreach (var configurator in configurators)
 111        {
 10278112            configurator.Configure(options);
 10278113            var modifiersToAdd = configurator.GetModifiers();
 10278114            modifiers.AddRange(modifiersToAdd);
 115        }
 116
 1713117        options.TypeInfoResolver = new ModifiableJsonTypeInfoResolver(modifiers);
 1713118    }
 119
 120    /// <summary>
 121    /// Creates an instance of the specified type using the service provider.
 122    /// </summary>
 8537123    protected T CreateInstance<T>() => ActivatorUtilities.CreateInstance<T>(ServiceProvider);
 124}