< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.InputJsonConverter<T>
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/InputJsonConverter.cs
Line coverage
76%
Covered lines: 30
Uncovered lines: 9
Coverable lines: 39
Total lines: 87
Line coverage: 76.9%
Branch coverage
53%
Covered branches: 17
Total branches: 32
Branch coverage: 53.1%
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%210%
Read(...)50%362472.22%
Write(...)62.5%8882.35%

File(s)

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

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Elsa.Expressions.Contracts;
 5using Elsa.Expressions.Helpers;
 6using Elsa.Expressions.Models;
 7using Elsa.Workflows.Models;
 8
 9namespace Elsa.Workflows.Serialization.Converters;
 10
 11/// <summary>
 12/// Serializes <see cref="Input"/> objects.
 13/// </summary>
 14public class InputJsonConverter<T> : JsonConverter<Input<T>>
 15{
 16    private readonly IExpressionDescriptorRegistry _expressionDescriptorRegistry;
 17
 18    /// <inheritdoc />
 387319    public InputJsonConverter(IExpressionDescriptorRegistry expressionDescriptorRegistry)
 20    {
 387321        _expressionDescriptorRegistry = expressionDescriptorRegistry;
 387322    }
 23
 24    /// <inheritdoc />
 025    public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert);
 26
 27    /// <inheritdoc />
 28    [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require
 29    public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 30    {
 199131        if (!JsonDocument.TryParseValue(ref reader, out var doc))
 032            return default!;
 33
 34        // If the value is an object, it represents an Input-wrapped property.
 199135        if (doc.RootElement.ValueKind == JsonValueKind.Object)
 36        {
 199137            if (!doc.RootElement.TryGetProperty("typeName", out _))
 038                return default!;
 39
 199140            var expressionElement = doc.RootElement.TryGetProperty("expression", out var expressionElementValue) ? expre
 199141            var expressionTypeNameElement = expressionElement.ValueKind != JsonValueKind.Undefined ? expressionElement.T
 199142            var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeName
 199143            var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeNam
 199144            var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory?.Invoke();
 45
 199146            if (memoryBlockReference == null)
 047                return default!;
 48
 199149            var memoryBlockType = memoryBlockReference.GetType();
 199150            var context = new ExpressionSerializationContext(expressionTypeName!, expressionElement, options, memoryBloc
 199151            var expression = expressionDescriptor!.Deserialize(context);
 199152            return (Input<T>)Activator.CreateInstance(typeof(Input<T>), expression, memoryBlockReference)!;
 53        }
 54
 055        var convertedValue = doc.RootElement.ToString().TryConvertTo<T>().Value;
 056        return (Input<T>)Activator.CreateInstance(typeof(Input<T>), new Literal(convertedValue))!;
 57    }
 58
 59    /// <inheritdoc />
 60    public override void Write(Utf8JsonWriter writer, Input<T> value, JsonSerializerOptions options)
 61    {
 184062        var expression = value.Expression;
 63
 184064        if (expression == null)
 65        {
 066            writer.WriteNullValue();
 067            return;
 68        }
 69
 184070        var expressionType = expression.Type;
 184071        var expressionDescriptor = expressionType != null ? _expressionDescriptorRegistry.Find(expressionType) : default
 72
 184073        if (expressionDescriptor == null)
 074            throw new JsonException($"Could not find an expression descriptor for expression type '{expressionType}'.");
 75
 184076        var targetType = value.Type;
 184077        var expressionValue = expressionDescriptor.IsSerializable ? expression : null;
 78
 184079        var model = new
 184080        {
 184081            TypeName = targetType,
 184082            Expression = expressionValue!
 184083        };
 84
 184085        JsonSerializer.Serialize(writer, model, options);
 184086    }
 87}