| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Encodings.Web; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using System.Text.Unicode; |
| | | 6 | | using Elsa.Common.Converters; |
| | | 7 | | using Elsa.Expressions.Contracts; |
| | | 8 | | using Elsa.Expressions.Helpers; |
| | | 9 | | using Elsa.Expressions.Models; |
| | | 10 | | using Elsa.Extensions; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Workflows.Expressions; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Evaluates an object expression. |
| | | 17 | | /// </summary> |
| | | 18 | | [UsedImplicitly] |
| | | 19 | | public class ObjectExpressionHandler : IExpressionHandler |
| | | 20 | | { |
| | | 21 | | private JsonSerializerOptions? _serializerOptions; |
| | | 22 | | |
| | | 23 | | private JsonSerializerOptions SerializerOptions => |
| | 220 | 24 | | _serializerOptions ??= new JsonSerializerOptions |
| | 220 | 25 | | { |
| | 220 | 26 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 220 | 27 | | ReferenceHandler = ReferenceHandler.Preserve, |
| | 220 | 28 | | PropertyNameCaseInsensitive = true, |
| | 220 | 29 | | Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) |
| | 220 | 30 | | }.WithConverters( |
| | 220 | 31 | | new IntegerJsonConverter(), |
| | 220 | 32 | | new DecimalJsonConverter(), |
| | 220 | 33 | | new JsonStringEnumConverter()); |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | [RequiresUnreferencedCode("The type is not known at compile time.")] |
| | | 37 | | public ValueTask<object?> EvaluateAsync(Expression expression, Type returnType, ExpressionExecutionContext context, |
| | | 38 | | { |
| | 220 | 39 | | var value = expression.Value.ConvertTo<string>() ?? ""; |
| | | 40 | | |
| | 220 | 41 | | if (string.IsNullOrWhiteSpace(value)) |
| | 0 | 42 | | return ValueTask.FromResult(default(object?)); |
| | | 43 | | |
| | 220 | 44 | | var converterOptions = new ObjectConverterOptions(SerializerOptions); |
| | 220 | 45 | | var model = value.ConvertTo(returnType, converterOptions); |
| | 220 | 46 | | return ValueTask.FromResult(model); |
| | | 47 | | } |
| | | 48 | | } |