< 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>
 621    protected ConfigurableSerializer(IServiceProvider serviceProvider)
 22    {
 623        ServiceProvider = serviceProvider;
 624    }
 25
 26    /// <summary>
 27    /// Gets the service provider.
 28    /// </summary>
 350129    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    {
 313036        if (_options != null)
 312437            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    {
 58850        Configure(options);
 58851        AddConverters(options);
 58852        RunConfigurators(options);
 58853    }
 54
 55    /// <summary>
 56    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 57    /// </summary>
 58    protected JsonSerializerOptions GetOptionsInternal()
 59    {
 58260        var options = CreateOptionsInternal();
 58261        ApplyOptions(options);
 58262        _options = options;
 58263        return options;
 64    }
 65
 66    /// <summary>
 67    /// Creates a new instance of <see cref="JsonSerializerOptions"/>.
 68    /// </summary>
 69    private static JsonSerializerOptions CreateOptionsInternal()
 70    {
 58871        var options = new JsonSerializerOptions
 58872        {
 58873            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 58874            PropertyNameCaseInsensitive = true,
 58875            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
 58876            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 58877        };
 78
 58879        options.Converters.Add(new JsonStringEnumConverter());
 58880        options.Converters.Add(JsonMetadataServices.TimeSpanConverter);
 58881        options.Converters.Add(new IntegerJsonConverter());
 58882        options.Converters.Add(new BigIntegerJsonConverter());
 58883        options.Converters.Add(new DecimalJsonConverter());
 84
 58885        return options;
 86    }
 87
 88    /// <summary>
 89    /// Configures the <see cref="JsonSerializerOptions"/> object.
 90    /// </summary>
 91    protected virtual void Configure(JsonSerializerOptions options)
 92    {
 58793    }
 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    {
 588107        var configurators = ServiceProvider.GetServices<ISerializationOptionsConfigurator>();
 588108        var modifiers = new List<Action<JsonTypeInfo>>();
 109
 8232110        foreach (var configurator in configurators)
 111        {
 3528112            configurator.Configure(options);
 3528113            var modifiersToAdd = configurator.GetModifiers();
 3528114            modifiers.AddRange(modifiersToAdd);
 115        }
 116
 588117        options.TypeInfoResolver = new ModifiableJsonTypeInfoResolver(modifiers);
 588118    }
 119
 120    /// <summary>
 121    /// Creates an instance of the specified type using the service provider.
 122    /// </summary>
 2912123    protected T CreateInstance<T>() => ActivatorUtilities.CreateInstance<T>(ServiceProvider);
 124}