< Summary

Information
Class: Elsa.Workflows.Activities.Flowchart.Serialization.ConnectionJsonConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Flowchart/Serialization/ConnectionJsonConverter.cs
Line coverage
78%
Covered lines: 39
Uncovered lines: 11
Coverable lines: 50
Total lines: 103
Line coverage: 78%
Branch coverage
42%
Covered branches: 11
Total branches: 26
Branch coverage: 42.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanConvert(...)100%11100%
.ctor(...)100%11100%
Read(...)50%121290.9%
Get()25%9433.33%
GetId()50%3250%
GetPort()25%6450%
Write(...)50%4490%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Flowchart/Serialization/ConnectionJsonConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Workflows.Activities.Flowchart.Models;
 4
 5namespace Elsa.Workflows.Activities.Flowchart.Serialization;
 6
 7/// <summary>
 8/// Converts <see cref="Connection"/> to and from JSON.
 9/// </summary>
 10public class ConnectionJsonConverter : JsonConverter<Connection>
 11{
 12    private readonly IDictionary<string, IActivity> _activities;
 13
 14    /// <inheritdoc />
 5315315    public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Connection);
 16
 17    /// <inheritdoc />
 226318    public ConnectionJsonConverter(IDictionary<string, IActivity> activities)
 19    {
 226320        _activities = activities;
 226321    }
 22
 23    /// <inheritdoc />
 24    public override Connection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 25    {
 47226        if (!JsonDocument.TryParseValue(ref reader, out var doc))
 027            throw new JsonException("Failed to parse JsonDocument");
 28
 47229        var root = doc.RootElement;
 30
 31        // case‐insensitive get
 32        JsonElement Get(string name)
 33        {
 94434            if (root.TryGetProperty(name, out var e))
 94435                return e;
 036            var alt = char.ToUpperInvariant(name[0]) + name.Substring(1);
 037            if (root.TryGetProperty(alt, out e))
 038                return e;
 039            throw new JsonException($"Missing property '{name}' or '{alt}'");
 40        }
 41
 47242        var sourceElement = Get("source");
 47243        var targetElement = Get("target");
 44
 45        // now inside sourceElement and targetElement, their children
 46        // are again PascalCased (“Activity”, “Port”), so do the same thing:
 47
 48        string GetId(JsonElement container, string propName)
 49        {
 47250            if (container.TryGetProperty(propName, out var p))
 47251                return p.GetString()!;
 052            var alt = char.ToUpperInvariant(propName[0]) + propName.Substring(1);
 053            return container.GetProperty(alt).GetString()!;
 54        }
 55
 56        string? GetPort(JsonElement container, string propName)
 57        {
 141658            if (container.TryGetProperty(propName, out var p))
 141659                return p.GetString();
 060            var alt = char.ToUpperInvariant(propName[0]) + propName.Substring(1);
 061            return container.TryGetProperty(alt, out p) ? p.GetString() : null;
 62        }
 63
 47264        var sourceId = GetId(sourceElement, "activity");
 47265        var targetId = GetPort(targetElement, "activity"); // note: this could be null
 47266        var sourcePort = GetPort(sourceElement, "port");
 47267        var targetPort = GetPort(targetElement, "port");
 68
 47269        var sourceAct = _activities.TryGetValue(sourceId, out var s) ? s : throw new JsonException($"Unknown activity ID
 47270        var targetAct =
 47271            targetId != null
 47272                ? _activities.TryGetValue(targetId, out var t)
 47273                    ? t
 47274                    : throw new JsonException($"Unknown activity ID '{targetId}'")
 47275                : null;
 76
 47277        var source = new Endpoint(sourceAct, sourcePort);
 47278        var target = new Endpoint(targetAct!, targetPort);
 79
 80        // vertices is already correct:
 47281        var vertices = Array.Empty<Position>();
 47282        if (doc.RootElement.TryGetProperty("vertices", out var vertsEl) && vertsEl.ValueKind == JsonValueKind.Array)
 083            vertices = vertsEl.Deserialize<Position[]>(options)!;
 84
 47285        return new Connection(source, target) { Vertices = vertices };
 86    }
 87
 88    /// <inheritdoc />
 89    public override void Write(Utf8JsonWriter writer, Connection value, JsonSerializerOptions options)
 90    {
 15991        if (value.Source.Activity == null! || value.Target.Activity == null!)
 092            return;
 93
 15994        var model = new
 15995        {
 15996            Source = new { Activity = value.Source.Activity.Id, Port = value.Source.Port },
 15997            Target = new { Activity = value.Target.Activity.Id, Port = value.Target.Port },
 15998            Vertices = value.Vertices,
 15999        };
 100
 159101        JsonSerializer.Serialize(writer, model, options);
 159102    }
 103}