| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Common.Serialization; |
| | | 5 | | using Elsa.Expressions.Contracts; |
| | | 6 | | using Elsa.Expressions.Services; |
| | | 7 | | using Elsa.Workflows.Serialization.Converters; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 11 | | |
| | | 12 | | /// <inheritdoc cref="ISafeSerializer" /> |
| | | 13 | | public class SafeSerializer : ConfigurableSerializer, ISafeSerializer |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public SafeSerializer(IServiceProvider serviceProvider) |
| | 228 | 17 | | : base(serviceProvider) { } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | [RequiresUnreferencedCode("The type T may be trimmed.")] |
| | | 21 | | public ValueTask<string> SerializeAsync(object? value, CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 0 | 23 | | return ValueTask.FromResult(Serialize(value)); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | [RequiresUnreferencedCode("The type T may be trimmed.")] |
| | | 28 | | public ValueTask<JsonElement> SerializeToElementAsync(object? value, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 0 | 30 | | return new(SerializeToElement(value)); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | [RequiresUnreferencedCode("The type T may be trimmed.")] |
| | | 35 | | public ValueTask<T> DeserializeAsync<T>(string json, CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 0 | 37 | | return new(Deserialize<T>(json)); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | [RequiresUnreferencedCode("The type T may be trimmed.")] |
| | | 42 | | public ValueTask<T> DeserializeAsync<T>(JsonElement element, CancellationToken cancellationToken = default) |
| | | 43 | | { |
| | 0 | 44 | | return new(Deserialize<T>(element)); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public string Serialize(object? value) |
| | | 48 | | { |
| | 2142 | 49 | | var options = GetOptions(); |
| | 2142 | 50 | | return JsonSerializer.Serialize(value, options); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public JsonElement SerializeToElement(object? value) |
| | | 54 | | { |
| | 0 | 55 | | var options = GetOptions(); |
| | 0 | 56 | | return JsonSerializer.SerializeToElement(value, options); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public T Deserialize<T>(string json) |
| | | 60 | | { |
| | 0 | 61 | | var options = GetOptions(); |
| | 0 | 62 | | return JsonSerializer.Deserialize<T>(json, options)!; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public T Deserialize<T>(JsonElement element) |
| | | 66 | | { |
| | 0 | 67 | | var options = GetOptions(); |
| | 0 | 68 | | return element.Deserialize<T>(options)!; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | | 72 | | protected override void AddConverters(JsonSerializerOptions options) |
| | | 73 | | { |
| | 114 | 74 | | var expressionDescriptorRegistry = ServiceProvider.GetRequiredService<IExpressionDescriptorRegistry>(); |
| | | 75 | | |
| | 114 | 76 | | options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); |
| | 114 | 77 | | options.Converters.Add(new TypeJsonConverter(WellKnownTypeRegistry.CreateDefault())); |
| | 114 | 78 | | options.Converters.Add(new SafeValueConverterFactory()); |
| | 114 | 79 | | options.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry)); |
| | 114 | 80 | | options.Converters.Add(new FuncExpressionValueConverter()); |
| | 114 | 81 | | } |
| | | 82 | | } |