< 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 />
 1686515    public PolymorphicDictionaryConverter(JsonSerializerOptions options, IWellKnownTypeRegistry wellKnownTypeRegistry)
 16    {
 14888917        var factory = (JsonConverterFactory)(options.Converters.FirstOrDefault(x => x is PolymorphicObjectConverterFacto
 1686518        _objectConverter = (JsonConverter<object>)factory.CreateConverter(typeof(object), options)!;
 1686519    }
 20
 21    /// <inheritdoc />
 22    public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOption
 23    {
 944724        if (reader.TokenType != JsonTokenType.StartObject)
 025            throw new JsonException("Expected start of object.");
 26
 944727        var dictionary = new Dictionary<string, object>();
 28
 1933729        while (reader.Read())
 30        {
 1933731            if (reader.TokenType == JsonTokenType.EndObject)
 944732                return dictionary;
 33
 989034            var key = reader.GetString()!;
 989035            reader.Read();
 989036            var value = _objectConverter.Read(ref reader, typeof(object), options)!;
 989037            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    {
 1135946        writer.WriteStartObject();
 47
 2715848        foreach (var kvp in value)
 49        {
 222050            writer.WritePropertyName(kvp.Key);
 222051            _objectConverter.Write(writer, kvp.Value, options);
 52        }
 53
 1135954        writer.WriteEndObject();
 1135955    }
 56}