< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.ExpressionJsonConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/ExpressionJsonConverter.cs
Line coverage
48%
Covered lines: 16
Uncovered lines: 17
Coverable lines: 33
Total lines: 74
Line coverage: 48.4%
Branch coverage
16%
Covered branches: 4
Total branches: 24
Branch coverage: 16.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%210%
Read(...)0%272160%
Write(...)50%8881.25%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Contracts;
 4using Elsa.Expressions.Models;
 5using Elsa.Workflows.Models;
 6
 7namespace Elsa.Workflows.Serialization.Converters;
 8
 9/// <summary>
 10/// Serializes <see cref="Expression"/> objects.
 11/// </summary>
 12public class ExpressionJsonConverter : JsonConverter<Expression>
 13{
 14    private readonly IExpressionDescriptorRegistry _expressionDescriptorRegistry;
 15
 16    /// <inheritdoc />
 150117    public ExpressionJsonConverter(IExpressionDescriptorRegistry expressionDescriptorRegistry)
 18    {
 150119        _expressionDescriptorRegistry = expressionDescriptorRegistry;
 150120    }
 21
 22    /// <inheritdoc />
 023    public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert);
 24
 25    /// <inheritdoc />
 26    public override Expression Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 27    {
 028        if (!JsonDocument.TryParseValue(ref reader, out var doc))
 029            return default!;
 30
 031        var expressionElement = doc.RootElement;
 032        var expressionTypeNameElement = expressionElement.TryGetProperty("type", out var expressionTypeNameElementValue)
 033        var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeNameElem
 034        var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeName) :
 035        var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory?.Invoke();
 36
 037        if (memoryBlockReference == null)
 038            return default!;
 39
 040        var memoryBlockType = memoryBlockReference.GetType();
 041        var context = new ExpressionSerializationContext(expressionTypeName!, expressionElement, options, memoryBlockTyp
 042        var expression = expressionDescriptor!.Deserialize(context);
 43
 044        return expression;
 45    }
 46
 47    /// <inheritdoc />
 48    public override void Write(Utf8JsonWriter writer, Expression value, JsonSerializerOptions options)
 49    {
 168750        var expression = value;
 51
 168752        if(expression == null)
 53        {
 054            writer.WriteNullValue();
 055            return;
 56        }
 57
 168758        var expressionType = expression.Type;
 168759        var expressionDescriptor = expressionType != null ? _expressionDescriptorRegistry.Find(expressionType) : null;
 60
 168761        if (expressionDescriptor == null)
 062            throw new JsonException($"Could not find an expression descriptor for expression type '{expressionType}'.");
 63
 168764        var expressionValue = expressionDescriptor.IsSerializable ? expression.Value : null;
 65
 168766        var model = new
 168767        {
 168768            Type = expressionType,
 168769            Value = expressionValue
 168770        };
 71
 168772        JsonSerializer.Serialize(writer, model, options);
 168773    }
 74}