| | | 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="ObsoleteConnection"/> to and from JSON. |
| | | 9 | | /// </summary> |
| | | 10 | | [Obsolete("Use ConnectionJsonConverter instead.")] |
| | | 11 | | public class ObsoleteConnectionJsonConverter : JsonConverter<ObsoleteConnection> |
| | | 12 | | { |
| | | 13 | | private readonly IDictionary<string, IActivity> _activities; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | 0 | 16 | | public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ObsoleteConnection); |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 0 | 19 | | public ObsoleteConnectionJsonConverter(IDictionary<string, IActivity> activities) |
| | | 20 | | { |
| | 0 | 21 | | _activities = activities; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override ObsoleteConnection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options |
| | | 26 | | { |
| | 0 | 27 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 28 | | throw new JsonException("Failed to parse JsonDocument"); |
| | | 29 | | |
| | 0 | 30 | | var sourceId = doc.RootElement.GetProperty("source").GetString()!; |
| | 0 | 31 | | var targetId = doc.RootElement.GetProperty("target").GetString()!; |
| | 0 | 32 | | var sourcePort = doc.RootElement.GetProperty("sourcePort").GetString()!; |
| | 0 | 33 | | var targetPort = doc.RootElement.GetProperty("targetPort").GetString()!; |
| | | 34 | | |
| | 0 | 35 | | var source = _activities.TryGetValue(sourceId, out var s) ? s : null!; |
| | 0 | 36 | | var target = _activities.TryGetValue(targetId, out var t) ? t : null!; |
| | | 37 | | |
| | 0 | 38 | | return new ObsoleteConnection(source, target, sourcePort, targetPort); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public override void Write(Utf8JsonWriter writer, ObsoleteConnection value, JsonSerializerOptions options) |
| | | 43 | | { |
| | 0 | 44 | | var (activity, target, sourcePort, targetPort) = value; |
| | | 45 | | |
| | 0 | 46 | | var model = new |
| | 0 | 47 | | { |
| | 0 | 48 | | Source = activity.Id, |
| | 0 | 49 | | Target = target.Id, |
| | 0 | 50 | | SourcePort = sourcePort, |
| | 0 | 51 | | TargetPort = targetPort |
| | 0 | 52 | | }; |
| | | 53 | | |
| | 0 | 54 | | JsonSerializer.Serialize(writer, model, options); |
| | 0 | 55 | | } |
| | | 56 | | } |