| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Serializes <see cref="Expression"/> objects. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ExpressionJsonConverter : JsonConverter<Expression> |
| | | 13 | | { |
| | | 14 | | private readonly IExpressionDescriptorRegistry _expressionDescriptorRegistry; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | 1501 | 17 | | public ExpressionJsonConverter(IExpressionDescriptorRegistry expressionDescriptorRegistry) |
| | | 18 | | { |
| | 1501 | 19 | | _expressionDescriptorRegistry = expressionDescriptorRegistry; |
| | 1501 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 0 | 23 | | public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert); |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public override Expression Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 27 | | { |
| | 0 | 28 | | if (!JsonDocument.TryParseValue(ref reader, out var doc)) |
| | 0 | 29 | | return default!; |
| | | 30 | | |
| | 0 | 31 | | var expressionElement = doc.RootElement; |
| | 0 | 32 | | var expressionTypeNameElement = expressionElement.TryGetProperty("type", out var expressionTypeNameElementValue) |
| | 0 | 33 | | var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeNameElem |
| | 0 | 34 | | var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeName) : |
| | 0 | 35 | | var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory?.Invoke(); |
| | | 36 | | |
| | 0 | 37 | | if (memoryBlockReference == null) |
| | 0 | 38 | | return default!; |
| | | 39 | | |
| | 0 | 40 | | var memoryBlockType = memoryBlockReference.GetType(); |
| | 0 | 41 | | var context = new ExpressionSerializationContext(expressionTypeName!, expressionElement, options, memoryBlockTyp |
| | 0 | 42 | | var expression = expressionDescriptor!.Deserialize(context); |
| | | 43 | | |
| | 0 | 44 | | return expression; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public override void Write(Utf8JsonWriter writer, Expression value, JsonSerializerOptions options) |
| | | 49 | | { |
| | 1687 | 50 | | var expression = value; |
| | | 51 | | |
| | 1687 | 52 | | if(expression == null) |
| | | 53 | | { |
| | 0 | 54 | | writer.WriteNullValue(); |
| | 0 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | 1687 | 58 | | var expressionType = expression.Type; |
| | 1687 | 59 | | var expressionDescriptor = expressionType != null ? _expressionDescriptorRegistry.Find(expressionType) : null; |
| | | 60 | | |
| | 1687 | 61 | | if (expressionDescriptor == null) |
| | 0 | 62 | | throw new JsonException($"Could not find an expression descriptor for expression type '{expressionType}'."); |
| | | 63 | | |
| | 1687 | 64 | | var expressionValue = expressionDescriptor.IsSerializable ? expression.Value : null; |
| | | 65 | | |
| | 1687 | 66 | | var model = new |
| | 1687 | 67 | | { |
| | 1687 | 68 | | Type = expressionType, |
| | 1687 | 69 | | Value = expressionValue |
| | 1687 | 70 | | }; |
| | | 71 | | |
| | 1687 | 72 | | JsonSerializer.Serialize(writer, model, options); |
| | 1687 | 73 | | } |
| | | 74 | | } |