| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Nodes; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using Elsa.Expressions.Contracts; |
| | | 6 | | using Elsa.Expressions.Extensions; |
| | | 7 | | using Elsa.Extensions; |
| | | 8 | | using Elsa.Workflows.Models; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Api.Serialization; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Converts <see cref="ArgumentDefinition"/> derivatives from and to JSON |
| | | 14 | | /// </summary> |
| | | 15 | | public class ArgumentJsonConverter : JsonConverter<ArgumentDefinition> |
| | | 16 | | { |
| | | 17 | | private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | 290 | 20 | | public ArgumentJsonConverter(IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 21 | | { |
| | 290 | 22 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 290 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public override void Write(Utf8JsonWriter writer, ArgumentDefinition value, JsonSerializerOptions options) |
| | | 27 | | { |
| | 0 | 28 | | var newOptions = new JsonSerializerOptions(options); |
| | 0 | 29 | | newOptions.Converters.RemoveWhere(x => x is ArgumentJsonConverterFactory); |
| | | 30 | | |
| | 0 | 31 | | var jsonObject = (JsonObject)JsonSerializer.SerializeToNode(value, value.GetType(), newOptions)!; |
| | 0 | 32 | | var typeName = value.Type; |
| | 0 | 33 | | var typeAlias = _wellKnownTypeRegistry.TryGetAlias(typeName, out var alias) ? alias : null; |
| | 0 | 34 | | var isArray = typeName.IsArray; |
| | 0 | 35 | | var isCollection = typeName.IsCollectionType(); |
| | 0 | 36 | | var elementTypeName = isArray ? typeName.GetElementType()! : isCollection ? typeName.GenericTypeArguments[0] : t |
| | 0 | 37 | | var elementTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(elementTypeName); |
| | 0 | 38 | | var isAliasedArray = (isArray || isCollection) && typeAlias != null; |
| | 0 | 39 | | var finalTypeAlias = isArray || isCollection ? typeAlias ?? elementTypeAlias : elementTypeAlias; |
| | | 40 | | |
| | 0 | 41 | | if (isArray && !isAliasedArray) jsonObject["isArray"] = isArray; |
| | 0 | 42 | | if (isCollection) jsonObject["isCollection"] = isCollection; |
| | | 43 | | |
| | 0 | 44 | | jsonObject["type"] = finalTypeAlias; |
| | 0 | 45 | | JsonSerializer.Serialize(writer, jsonObject, newOptions); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | [UnconditionalSuppressMessage("Trimming", "IL2055:Call to MakeGenericType can not be statically analyzed", Justifica |
| | | 50 | | public override ArgumentDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options |
| | | 51 | | { |
| | 114 | 52 | | var jsonObject = (JsonObject)JsonNode.Parse(ref reader)!; |
| | 114 | 53 | | var isArray = jsonObject["isArray"]?.GetValue<bool>() ?? false; |
| | 114 | 54 | | var isCollection = jsonObject["isCollection"]?.GetValue<bool>() ?? false; |
| | 114 | 55 | | var typeAlias = jsonObject["type"]!.GetValue<string>(); |
| | 114 | 56 | | var type = _wellKnownTypeRegistry.GetTypeOrDefault(typeAlias); |
| | | 57 | | |
| | 114 | 58 | | if (isArray) type = type.MakeArrayType(); |
| | 114 | 59 | | if (isCollection) type = type.MakeGenericType(type.GenericTypeArguments[0]); |
| | | 60 | | |
| | 114 | 61 | | var newOptions = new JsonSerializerOptions(options); |
| | 1710 | 62 | | newOptions.Converters.RemoveWhere(x => x is ArgumentJsonConverterFactory); |
| | 114 | 63 | | var inputDefinition = (ArgumentDefinition)jsonObject.Deserialize(typeToConvert, newOptions)!; |
| | 114 | 64 | | inputDefinition.Type = type; |
| | | 65 | | |
| | 114 | 66 | | return inputDefinition; |
| | | 67 | | } |
| | | 68 | | } |