< Summary

Information
Class: Elsa.Resilience.Serialization.ResilienceStrategySerializer
Assembly: Elsa.Resilience.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Resilience.Core/Serialization/ResilienceStrategySerializer.cs
Line coverage
46%
Covered lines: 15
Uncovered lines: 17
Coverable lines: 32
Total lines: 49
Line coverage: 46.8%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%9655.55%
get_SerializerOptions()100%210%
Serialize(...)100%210%
SerializeMany(...)100%210%
Deserialize(...)100%210%
DeserializeMany(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Resilience.Core/Serialization/ResilienceStrategySerializer.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using System.Text.Json.Serialization.Metadata;
 4using Elsa.Extensions;
 5using Elsa.Resilience.Options;
 6using Microsoft.Extensions.Options;
 7
 8namespace Elsa.Resilience.Serialization;
 9
 10public class ResilienceStrategySerializer
 11{
 12    private readonly JsonSerializerOptions _serializerOptions;
 13
 114    public ResilienceStrategySerializer(IOptions<ResilienceOptions> options)
 15    {
 116        var serializerOptions = new JsonSerializerOptions
 117        {
 118            TypeInfoResolver = new DefaultJsonTypeInfoResolver().WithAddedModifier(typeInfo =>
 119            {
 020                if (typeInfo.Type != typeof(IResilienceStrategy))
 021                    return;
 122
 023                if (typeInfo.Kind != JsonTypeInfoKind.Object)
 024                    return;
 125
 026                var polymorphismOptions = new JsonPolymorphismOptions
 027                {
 028                    TypeDiscriminatorPropertyName = "$type"
 029                };
 130
 031                foreach (var type in options.Value.StrategyTypes.ToList())
 032                    polymorphismOptions.DerivedTypes.Add(new(type, type.Name));
 133
 034                typeInfo.PolymorphismOptions = polymorphismOptions;
 035            }),
 136            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 137            PropertyNameCaseInsensitive = true,
 138            NumberHandling = JsonNumberHandling.AllowReadingFromString
 139        }.WithConverters(new JsonStringEnumConverter());
 40
 141        _serializerOptions = serializerOptions;
 142    }
 43
 044    public JsonSerializerOptions SerializerOptions => _serializerOptions;
 045    public string Serialize(IResilienceStrategy strategy) => JsonSerializer.Serialize(strategy, _serializerOptions);
 046    public string SerializeMany(IEnumerable<IResilienceStrategy> strategies) => JsonSerializer.Serialize(strategies, _se
 047    public IResilienceStrategy Deserialize(string json) => JsonSerializer.Deserialize<IResilienceStrategy>(json, _serial
 048    public IEnumerable<IResilienceStrategy> DeserializeMany(string json) => JsonSerializer.Deserialize<IEnumerable<IResi
 49}