| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Common.Serialization; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Workflows.Serialization.Converters; |
| | | 6 | | using Elsa.Workflows.Serialization.ReferenceHandlers; |
| | | 7 | | using Elsa.Workflows.State; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Serializes and deserializes workflow states from and to JSON. |
| | | 14 | | /// </summary> |
| | | 15 | | public class JsonWorkflowStateSerializer : ConfigurableSerializer, IWorkflowStateSerializer |
| | | 16 | | { |
| | | 17 | | private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry; |
| | | 18 | | private readonly ILoggerFactory _loggerFactory; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="JsonWorkflowStateSerializer"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | public JsonWorkflowStateSerializer(IServiceProvider serviceProvider, IWellKnownTypeRegistry wellKnownTypeRegistry, I |
| | 114 | 24 | | : base(serviceProvider) |
| | | 25 | | { |
| | 114 | 26 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 114 | 27 | | _loggerFactory = loggerFactory; |
| | 114 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access |
| | | 32 | | [Obsolete("Use the non-async version Serialize instead.")] |
| | | 33 | | public Task<string> SerializeAsync(WorkflowState workflowState, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 0 | 35 | | return Task.FromResult(Serialize(workflowState)); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access |
| | | 40 | | [Obsolete("Use the non-async version SerializeToUtfBytes instead.")] |
| | | 41 | | public Task<byte[]> SerializeToUtfBytesAsync(WorkflowState workflowState, CancellationToken cancellationToken = defa |
| | | 42 | | { |
| | 0 | 43 | | return Task.FromResult(SerializeToUtfBytes(workflowState)); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access |
| | | 48 | | [Obsolete("Use the non-async version SerializeToElement instead.")] |
| | | 49 | | public Task<JsonElement> SerializeToElementAsync(WorkflowState workflowState, CancellationToken cancellationToken = |
| | | 50 | | { |
| | 0 | 51 | | return Task.FromResult(SerializeToElement(workflowState)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access |
| | | 56 | | [Obsolete("Use the non-async version Serialize instead.")] |
| | | 57 | | public Task<string> SerializeAsync(object workflowState, CancellationToken cancellationToken = default) |
| | | 58 | | { |
| | 0 | 59 | | return Task.FromResult(Serialize(workflowState)); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce |
| | | 64 | | [Obsolete("Use the non-async version Deserialize instead.")] |
| | | 65 | | public Task<WorkflowState> DeserializeAsync(string serializedState, CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 0 | 67 | | return Task.FromResult(Deserialize(serializedState)); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce |
| | | 72 | | [Obsolete("Use the non-async version Deserialize instead.")] |
| | | 73 | | public Task<WorkflowState> DeserializeAsync(JsonElement serializedState, CancellationToken cancellationToken = defau |
| | | 74 | | { |
| | 0 | 75 | | return Task.FromResult(Deserialize(serializedState)); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce |
| | | 80 | | [Obsolete("Use the non-async version Deserialize instead.")] |
| | | 81 | | public Task<T> DeserializeAsync<T>(string serializedState, CancellationToken cancellationToken = default) |
| | | 82 | | { |
| | 0 | 83 | | return Task.FromResult(Deserialize<T>(serializedState)); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public string Serialize(WorkflowState workflowState) |
| | | 87 | | { |
| | 349 | 88 | | var options = GetOptions(); |
| | 349 | 89 | | return JsonSerializer.Serialize(workflowState, options); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public byte[] SerializeToUtfBytes(WorkflowState workflowState) |
| | | 93 | | { |
| | 0 | 94 | | var options = GetOptions(); |
| | 0 | 95 | | return JsonSerializer.SerializeToUtf8Bytes(workflowState, options); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public JsonElement SerializeToElement(WorkflowState workflowState) |
| | | 99 | | { |
| | 0 | 100 | | var options = GetOptions(); |
| | 0 | 101 | | return JsonSerializer.SerializeToElement(workflowState, options); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | public string Serialize(object workflowState) |
| | | 105 | | { |
| | 0 | 106 | | var options = GetOptions(); |
| | 0 | 107 | | return JsonSerializer.Serialize(workflowState, workflowState.GetType(), options); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public WorkflowState Deserialize(string serializedState) |
| | | 111 | | { |
| | 95 | 112 | | var options = GetOptions(); |
| | 95 | 113 | | return JsonSerializer.Deserialize<WorkflowState>(serializedState, options)!; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public WorkflowState Deserialize(JsonElement serializedState) |
| | | 117 | | { |
| | 0 | 118 | | var options = GetOptions(); |
| | 0 | 119 | | return serializedState.Deserialize<WorkflowState>(options)!; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public T Deserialize<T>(string serializedState) |
| | | 123 | | { |
| | 0 | 124 | | var options = GetOptions(); |
| | 0 | 125 | | return JsonSerializer.Deserialize<T>(serializedState, options)!; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | public override JsonSerializerOptions GetOptions() |
| | | 130 | | { |
| | 444 | 131 | | var options = base.GetOptions(); |
| | 444 | 132 | | return new(options) { ReferenceHandler = new CrossScopedReferenceHandler() }; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc /> |
| | | 136 | | protected override void AddConverters(JsonSerializerOptions options) |
| | | 137 | | { |
| | 1 | 138 | | options.Converters.Add(new TypeJsonConverter(_wellKnownTypeRegistry)); |
| | 1 | 139 | | options.Converters.Add(new PolymorphicObjectConverterFactory(_wellKnownTypeRegistry)); |
| | 1 | 140 | | options.Converters.Add(new VariableConverterFactory(_wellKnownTypeRegistry, _loggerFactory)); |
| | 1 | 141 | | options.Converters.Add(new FuncExpressionValueConverter()); |
| | 1 | 142 | | } |
| | | 143 | | } |