< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.PolymorphicDictionaryConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicDictionaryConverter.cs
Line coverage
90%
Covered lines: 19
Uncovered lines: 2
Coverable lines: 21
Total lines: 56
Line coverage: 90.4%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
Read(...)66.66%6681.81%
Write(...)100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicDictionaryConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Contracts;
 4
 5namespace Elsa.Workflows.Serialization.Converters;
 6
 7/// <summary>
 8/// A converter that can convert a dictionary of string keys and object values while preserving the object type.
 9/// </summary>
 10public class PolymorphicDictionaryConverter : JsonConverter<IDictionary<string, object>>
 11{
 12    private readonly JsonConverter<object> _objectConverter;
 13
 14    /// <inheritdoc />
 1605415    public PolymorphicDictionaryConverter(JsonSerializerOptions options, IWellKnownTypeRegistry wellKnownTypeRegistry)
 16    {
 14554517        var factory = (JsonConverterFactory)(options.Converters.FirstOrDefault(x => x is PolymorphicObjectConverterFacto
 1605418        _objectConverter = (JsonConverter<object>)factory.CreateConverter(typeof(object), options)!;
 1605419    }
 20
 21    /// <inheritdoc />
 22    public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOption
 23    {
 931024        if (reader.TokenType != JsonTokenType.StartObject)
 025            throw new JsonException("Expected start of object.");
 26
 931027        var dictionary = new Dictionary<string, object>();
 28
 1937529        while (reader.Read())
 30        {
 1937531            if (reader.TokenType == JsonTokenType.EndObject)
 931032                return dictionary;
 33
 1006534            var key = reader.GetString()!;
 1006535            reader.Read();
 1006536            var value = _objectConverter.Read(ref reader, typeof(object), options)!;
 1006537            dictionary.Add(key, value);
 38        }
 39
 040        throw new JsonException("Expected end of object.");
 41    }
 42
 43    /// <inheritdoc />
 44    public override void Write(Utf8JsonWriter writer, IDictionary<string, object> value, JsonSerializerOptions options)
 45    {
 1044846        writer.WriteStartObject();
 47
 2500048        foreach (var kvp in value)
 49        {
 205250            writer.WritePropertyName(kvp.Key);
 205251            _objectConverter.Write(writer, kvp.Value, options);
 52        }
 53
 1044854        writer.WriteEndObject();
 1044855    }
 56}