| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | using System.Text.Json.Serialization.Metadata; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Options; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Alterations.Core.Serialization; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Add additional <see cref="JsonConverter"/> objects. |
| | | 13 | | /// </summary> |
| | | 14 | | [UsedImplicitly] |
| | 1 | 15 | | public class AlterationSerializationOptionConfigurator(IOptions<AlterationOptions> options) : SerializationOptionsConfig |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public override IEnumerable<Action<JsonTypeInfo>> GetModifiers() |
| | | 19 | | { |
| | 588 | 20 | | var alterationTypes = options.Value.AlterationTypes; |
| | | 21 | | |
| | 588 | 22 | | yield return typeInfo => |
| | 588 | 23 | | { |
| | 143332 | 24 | | if (typeInfo.Type != typeof(IAlteration)) |
| | 143332 | 25 | | return; |
| | 588 | 26 | | |
| | 0 | 27 | | if (typeInfo.Kind != JsonTypeInfoKind.Object) |
| | 0 | 28 | | return; |
| | 588 | 29 | | |
| | 0 | 30 | | var polymorphismOptions = new JsonPolymorphismOptions |
| | 0 | 31 | | { |
| | 0 | 32 | | TypeDiscriminatorPropertyName = "type" |
| | 0 | 33 | | }; |
| | 588 | 34 | | |
| | 0 | 35 | | foreach (var alterationType in alterationTypes.ToList()) |
| | 588 | 36 | | { |
| | 0 | 37 | | polymorphismOptions.DerivedTypes.Add(new(alterationType, alterationType.Name)); |
| | 588 | 38 | | } |
| | 588 | 39 | | |
| | 0 | 40 | | typeInfo.PolymorphismOptions = polymorphismOptions; |
| | 588 | 41 | | }; |
| | 588 | 42 | | } |
| | | 43 | | } |