< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.TypeJsonConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/TypeJsonConverter.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 81
Line coverage: 100%
Branch coverage
90%
Covered branches: 29
Total branches: 32
Branch coverage: 90.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CanConvert(...)100%22100%
Read(...)83.33%1212100%
Write(...)94.44%1818100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Contracts;
 4using Elsa.Extensions;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Workflows.Serialization.Converters;
 8
 9/// <summary>
 10/// Serializes <see cref="Type"/> objects to a simple alias representing the type.
 11/// </summary>
 12[UsedImplicitly]
 13public class TypeJsonConverter : JsonConverter<Type>
 14{
 15    private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
 16
 17    /// <inheritdoc />
 1286818    public TypeJsonConverter(IWellKnownTypeRegistry wellKnownTypeRegistry)
 19    {
 1286820        _wellKnownTypeRegistry = wellKnownTypeRegistry;
 1286821    }
 22
 23    /// <inheritdoc />
 24    public override bool CanConvert(Type typeToConvert)
 25    {
 20490726        return typeToConvert == typeof(Type) || typeToConvert.FullName == "System.RuntimeType";
 27    }
 28
 29    /// <inheritdoc />
 30    public override Type? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 31    {
 157132        var typeAlias = reader.GetString()!;
 33
 34        // Handle array types.
 157135        if (typeAlias.EndsWith("[]"))
 36        {
 1237            var elementTypeAlias = typeAlias[..^2];
 1238            var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementT
 1239            return elementType.MakeArrayType();
 40        }
 41
 42        // Handle collection types.
 155943        if (typeAlias.StartsWith("List<") && typeAlias.EndsWith(">"))
 44        {
 945            var elementTypeAlias = typeAlias[5..^1];
 946            var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementT
 947            return typeof(List<>).MakeGenericType(elementType);
 48        }
 49
 155050        return _wellKnownTypeRegistry.TryGetType(typeAlias, out var type) ? type : Type.GetType(typeAlias);
 51    }
 52
 53    /// <inheritdoc />
 54    public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)
 55    {
 56        // Handle array types.
 1085457        if (value.IsArray)
 58        {
 7259            var elementType = value.GetElementType()!;
 7260            var elementTypeAlias = _wellKnownTypeRegistry.TryGetAlias(elementType, out var elementTypeAliasValue) ? elem
 7261            writer.WriteStringValue($"{elementTypeAlias}[]");
 7262            return;
 63        }
 64
 65        // Handle collection types.
 1078266        if (value is { IsGenericType: true, GenericTypeArguments.Length: 1 })
 67        {
 78068            var elementType = value.GenericTypeArguments.First();
 78069            var typedEnumerable = typeof(IEnumerable<>).MakeGenericType(elementType);
 70
 78071            if (typedEnumerable.IsAssignableFrom(value) && _wellKnownTypeRegistry.TryGetAlias(elementType, out var eleme
 72            {
 9273                writer.WriteStringValue($"List<{elementTypeAlias}>");
 9274                return;
 75            }
 76        }
 77
 1069078        var typeAlias = _wellKnownTypeRegistry.TryGetAlias(value, out var alias) ? alias : value.GetSimpleAssemblyQualif
 1069079        writer.WriteStringValue(typeAlias);
 1069080    }
 81}