| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Expressions.Helpers; |
| | | 6 | | using Elsa.Expressions.Models; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Serializes <see cref="Input"/> objects. |
| | | 13 | | /// </summary> |
| | | 14 | | public class InputJsonConverter<T> : JsonConverter<Input<T>> |
| | | 15 | | { |
| | | 16 | | private readonly IExpressionDescriptorRegistry _expressionDescriptorRegistry; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 3873 | 19 | | public InputJsonConverter(IExpressionDescriptorRegistry expressionDescriptorRegistry) |
| | | 20 | | { |
| | 3873 | 21 | | _expressionDescriptorRegistry = expressionDescriptorRegistry; |
| | 3873 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 0 | 25 | | public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert); |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require |
| | | 29 | | public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 30 | | { |
| | 1991 | 31 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 32 | | return default!; |
| | | 33 | | |
| | | 34 | | // If the value is an object, it represents an Input-wrapped property. |
| | 1991 | 35 | | if (doc.RootElement.ValueKind == JsonValueKind.Object) |
| | | 36 | | { |
| | 1991 | 37 | | if (!doc.RootElement.TryGetProperty("typeName", out _)) |
| | 0 | 38 | | return default!; |
| | | 39 | | |
| | 1991 | 40 | | var expressionElement = doc.RootElement.TryGetProperty("expression", out var expressionElementValue) ? expre |
| | 1991 | 41 | | var expressionTypeNameElement = expressionElement.ValueKind != JsonValueKind.Undefined ? expressionElement.T |
| | 1991 | 42 | | var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeName |
| | 1991 | 43 | | var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeNam |
| | 1991 | 44 | | var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory?.Invoke(); |
| | | 45 | | |
| | 1991 | 46 | | if (memoryBlockReference == null) |
| | 0 | 47 | | return default!; |
| | | 48 | | |
| | 1991 | 49 | | var memoryBlockType = memoryBlockReference.GetType(); |
| | 1991 | 50 | | var context = new ExpressionSerializationContext(expressionTypeName!, expressionElement, options, memoryBloc |
| | 1991 | 51 | | var expression = expressionDescriptor!.Deserialize(context); |
| | 1991 | 52 | | return (Input<T>)Activator.CreateInstance(typeof(Input<T>), expression, memoryBlockReference)!; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | var convertedValue = doc.RootElement.ToString().TryConvertTo<T>().Value; |
| | 0 | 56 | | return (Input<T>)Activator.CreateInstance(typeof(Input<T>), new Literal(convertedValue))!; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public override void Write(Utf8JsonWriter writer, Input<T> value, JsonSerializerOptions options) |
| | | 61 | | { |
| | 1840 | 62 | | var expression = value.Expression; |
| | | 63 | | |
| | 1840 | 64 | | if (expression == null) |
| | | 65 | | { |
| | 0 | 66 | | writer.WriteNullValue(); |
| | 0 | 67 | | return; |
| | | 68 | | } |
| | | 69 | | |
| | 1840 | 70 | | var expressionType = expression.Type; |
| | 1840 | 71 | | var expressionDescriptor = expressionType != null ? _expressionDescriptorRegistry.Find(expressionType) : default |
| | | 72 | | |
| | 1840 | 73 | | if (expressionDescriptor == null) |
| | 0 | 74 | | throw new JsonException($"Could not find an expression descriptor for expression type '{expressionType}'."); |
| | | 75 | | |
| | 1840 | 76 | | var targetType = value.Type; |
| | 1840 | 77 | | var expressionValue = expressionDescriptor.IsSerializable ? expression : null; |
| | | 78 | | |
| | 1840 | 79 | | var model = new |
| | 1840 | 80 | | { |
| | 1840 | 81 | | TypeName = targetType, |
| | 1840 | 82 | | Expression = expressionValue! |
| | 1840 | 83 | | }; |
| | | 84 | | |
| | 1840 | 85 | | JsonSerializer.Serialize(writer, model, options); |
| | 1840 | 86 | | } |
| | | 87 | | } |