| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using Elsa.Workflows.Management.Mappers; |
| | | 4 | | using Elsa.Workflows.Management.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Management.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A serializer that parses JSON. |
| | | 10 | | /// </summary> |
| | 0 | 11 | | public class WorkflowSerializer(IApiSerializer apiSerializer, WorkflowDefinitionMapper workflowDefinitionMapper) : IWork |
| | | 12 | | { |
| | | 13 | | private JsonSerializerOptions? _writeOptions; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public string Serialize(Workflow workflow) |
| | | 17 | | { |
| | 0 | 18 | | var model = workflowDefinitionMapper.Map(workflow); |
| | 0 | 19 | | var serializerOptions = GetWriteOptionsInternal(); |
| | 0 | 20 | | return JsonSerializer.Serialize(model, serializerOptions); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public Workflow Deserialize(string serializedWorkflow) |
| | | 25 | | { |
| | 0 | 26 | | var model = apiSerializer.Deserialize<WorkflowDefinitionModel>(serializedWorkflow); |
| | 0 | 27 | | return workflowDefinitionMapper.Map(model); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | private JsonSerializerOptions GetWriteOptionsInternal() |
| | | 31 | | { |
| | 0 | 32 | | if (_writeOptions != null) |
| | 0 | 33 | | return _writeOptions; |
| | | 34 | | |
| | 0 | 35 | | var options = apiSerializer.GetOptions(); |
| | 0 | 36 | | return _writeOptions = options; |
| | | 37 | | } |
| | | 38 | | } |