| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Workflows.Activities.Flowchart.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Activities.Flowchart.Serialization; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Converts <see cref="Connection"/> to and from JSON. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ConnectionJsonConverter : JsonConverter<Connection> |
| | | 11 | | { |
| | | 12 | | private readonly IDictionary<string, IActivity> _activities; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 53153 | 15 | | public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Connection); |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 2263 | 18 | | public ConnectionJsonConverter(IDictionary<string, IActivity> activities) |
| | | 19 | | { |
| | 2263 | 20 | | _activities = activities; |
| | 2263 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override Connection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 25 | | { |
| | 472 | 26 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 27 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 28 | | |
| | 472 | 29 | | var root = doc.RootElement; |
| | | 30 | | |
| | | 31 | | // case‐insensitive get |
| | | 32 | | JsonElement Get(string name) |
| | | 33 | | { |
| | 944 | 34 | | if (root.TryGetProperty(name, out var e)) |
| | 944 | 35 | | return e; |
| | 0 | 36 | | var alt = char.ToUpperInvariant(name[0]) + name.Substring(1); |
| | 0 | 37 | | if (root.TryGetProperty(alt, out e)) |
| | 0 | 38 | | return e; |
| | 0 | 39 | | throw new JsonException($"Missing property '{name}' or '{alt}'"); |
| | | 40 | | } |
| | | 41 | | |
| | 472 | 42 | | var sourceElement = Get("source"); |
| | 472 | 43 | | 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 | | { |
| | 472 | 50 | | if (container.TryGetProperty(propName, out var p)) |
| | 472 | 51 | | return p.GetString()!; |
| | 0 | 52 | | var alt = char.ToUpperInvariant(propName[0]) + propName.Substring(1); |
| | 0 | 53 | | return container.GetProperty(alt).GetString()!; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | string? GetPort(JsonElement container, string propName) |
| | | 57 | | { |
| | 1416 | 58 | | if (container.TryGetProperty(propName, out var p)) |
| | 1416 | 59 | | return p.GetString(); |
| | 0 | 60 | | var alt = char.ToUpperInvariant(propName[0]) + propName.Substring(1); |
| | 0 | 61 | | return container.TryGetProperty(alt, out p) ? p.GetString() : null; |
| | | 62 | | } |
| | | 63 | | |
| | 472 | 64 | | var sourceId = GetId(sourceElement, "activity"); |
| | 472 | 65 | | var targetId = GetPort(targetElement, "activity"); // note: this could be null |
| | 472 | 66 | | var sourcePort = GetPort(sourceElement, "port"); |
| | 472 | 67 | | var targetPort = GetPort(targetElement, "port"); |
| | | 68 | | |
| | 472 | 69 | | var sourceAct = _activities.TryGetValue(sourceId, out var s) ? s : throw new JsonException($"Unknown activity ID |
| | 472 | 70 | | var targetAct = |
| | 472 | 71 | | targetId != null |
| | 472 | 72 | | ? _activities.TryGetValue(targetId, out var t) |
| | 472 | 73 | | ? t |
| | 472 | 74 | | : throw new JsonException($"Unknown activity ID '{targetId}'") |
| | 472 | 75 | | : null; |
| | | 76 | | |
| | 472 | 77 | | var source = new Endpoint(sourceAct, sourcePort); |
| | 472 | 78 | | var target = new Endpoint(targetAct!, targetPort); |
| | | 79 | | |
| | | 80 | | // vertices is already correct: |
| | 472 | 81 | | var vertices = Array.Empty<Position>(); |
| | 472 | 82 | | if (doc.RootElement.TryGetProperty("vertices", out var vertsEl) && vertsEl.ValueKind == JsonValueKind.Array) |
| | 0 | 83 | | vertices = vertsEl.Deserialize<Position[]>(options)!; |
| | | 84 | | |
| | 472 | 85 | | return new Connection(source, target) { Vertices = vertices }; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | public override void Write(Utf8JsonWriter writer, Connection value, JsonSerializerOptions options) |
| | | 90 | | { |
| | 159 | 91 | | if (value.Source.Activity == null! || value.Target.Activity == null!) |
| | 0 | 92 | | return; |
| | | 93 | | |
| | 159 | 94 | | var model = new |
| | 159 | 95 | | { |
| | 159 | 96 | | Source = new { Activity = value.Source.Activity.Id, Port = value.Source.Port }, |
| | 159 | 97 | | Target = new { Activity = value.Target.Activity.Id, Port = value.Target.Port }, |
| | 159 | 98 | | Vertices = value.Vertices, |
| | 159 | 99 | | }; |
| | | 100 | | |
| | 159 | 101 | | JsonSerializer.Serialize(writer, model, options); |
| | 159 | 102 | | } |
| | | 103 | | } |