< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.OutputJsonConverter<T>
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/OutputJsonConverter.cs
Line coverage
51%
Covered lines: 15
Uncovered lines: 14
Coverable lines: 29
Total lines: 66
Line coverage: 51.7%
Branch coverage
12%
Covered branches: 1
Total branches: 8
Branch coverage: 12.5%
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%4260%
Write(...)50%22100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Contracts;
 4using Elsa.Expressions.Extensions;
 5using Elsa.Workflows.Memory;
 6using Elsa.Workflows.Models;
 7
 8namespace Elsa.Workflows.Serialization.Converters;
 9
 10/// <summary>
 11/// Serializes <see cref="Input"/> objects.
 12/// </summary>
 13public class OutputJsonConverter<T> : JsonConverter<Output<T>?>
 14{
 15    private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
 16
 17    /// <inheritdoc />
 152218    public OutputJsonConverter(IWellKnownTypeRegistry wellKnownTypeRegistry)
 19    {
 152220        _wellKnownTypeRegistry = wellKnownTypeRegistry;
 152221    }
 22
 23    /// <inheritdoc />
 024    public override bool CanConvert(Type typeToConvert) => typeof(Output).IsAssignableFrom(typeToConvert);
 25
 26    /// <inheritdoc />
 27    public override Output<T>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 28    {
 029        if (!JsonDocument.TryParseValue(ref reader, out var doc))
 030            return null;
 31
 032        if (!doc.RootElement.TryGetProperty("typeName", out _))
 033            return null;
 34
 035        var memoryReferenceElement = doc.RootElement.GetProperty("memoryReference");
 36
 037        if (!memoryReferenceElement.TryGetProperty("id", out var memoryReferenceIdElement))
 038            return default;
 39
 040        var variable = new Variable
 041        {
 042            Id = memoryReferenceIdElement.GetString()!
 043        };
 044        variable.Name = variable.Id;
 45
 046        return (Output<T>)Activator.CreateInstance(typeof(Output<T>), variable)!;
 47    }
 48
 49    /// <inheritdoc />
 50    public override void Write(Utf8JsonWriter writer, Output<T>? value, JsonSerializerOptions options)
 51    {
 4052        var valueType = typeof(T);
 4053        var valueTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(valueType);
 54
 4055        var model = new
 4056        {
 4057            TypeName = valueTypeAlias,
 4058            MemoryReference = value == null ? null : new
 4059            {
 4060                Id = value.MemoryBlockReference().Id
 4061            }
 4062        };
 63
 4064        JsonSerializer.Serialize(writer, model, options);
 4065    }
 66}