| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization.Metadata; |
| | | 4 | | using Elsa.Common.Serialization; |
| | | 5 | | using Elsa.Workflows.Serialization.Converters; |
| | | 6 | | using Elsa.Workflows.Serialization.Helpers; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 10 | | |
| | | 11 | | /// <inheritdoc cref="IActivitySerializer" /> |
| | 143 | 12 | | public class JsonActivitySerializer(IServiceProvider serviceProvider) : ConfigurableSerializer(serviceProvider), IActivi |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public string Serialize(IActivity activity) |
| | | 16 | | { |
| | 2871 | 17 | | var options = GetOptionsInternal(); |
| | 2871 | 18 | | var descriptor = ServiceProvider.GetRequiredService<IActivityRegistry>().Find(activity.Type, activity.Version); |
| | 4986 | 19 | | if (descriptor is null || !descriptor.Inputs.Any(input => input.IsSynthetic) && !descriptor.Outputs.Any(output = |
| | | 20 | | { |
| | 2858 | 21 | | return JsonSerializer.Serialize(activity, activity.GetType(), options); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // Keep normal CLR properties on System.Text.Json's metadata-aware path. |
| | | 25 | | // Only synthetic descriptor properties need the activity-specific writer. |
| | 13 | 26 | | options = new JsonSerializerOptions(options); |
| | 13 | 27 | | options = descriptor.ConfigureSerializerOptions?.Invoke(options) ?? options; |
| | 13 | 28 | | var serialized = JsonSerializer.SerializeToElement(activity, activity.GetType(), options); |
| | | 29 | | // A custom converter owns the complete representation, including synthetic fields. |
| | 13 | 30 | | if (options.GetTypeInfo(activity.GetType()).Kind != JsonTypeInfoKind.Object) |
| | | 31 | | { |
| | 1 | 32 | | ValidatePropertyNames(serialized, options); |
| | 1 | 33 | | return serialized.GetRawText(); |
| | | 34 | | } |
| | 12 | 35 | | using var stream = new MemoryStream(); |
| | 12 | 36 | | using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = options.WriteIndented, Encoder |
| | | 37 | | { |
| | 12 | 38 | | writer.WriteStartObject(); |
| | 238 | 39 | | foreach (var property in serialized.EnumerateObject()) |
| | | 40 | | { |
| | 107 | 41 | | property.WriteTo(writer); |
| | | 42 | | } |
| | 12 | 43 | | ServiceProvider.GetRequiredService<SyntheticPropertiesWriter>().WriteSyntheticProperties(writer, activity, d |
| | 12 | 44 | | writer.WriteEndObject(); |
| | 12 | 45 | | } |
| | 12 | 46 | | var json = Encoding.UTF8.GetString(stream.ToArray()); |
| | 12 | 47 | | using var document = JsonDocument.Parse(json, new JsonDocumentOptions { MaxDepth = options.MaxDepth }); |
| | 12 | 48 | | ValidatePropertyNames(document.RootElement, options); |
| | 8 | 49 | | return json; |
| | 8 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static void ValidatePropertyNames(JsonElement element, JsonSerializerOptions options) |
| | | 53 | | { |
| | 13 | 54 | | if (element.ValueKind != JsonValueKind.Object) |
| | | 55 | | { |
| | 0 | 56 | | throw new JsonException("An activity must serialize to a JSON object."); |
| | | 57 | | } |
| | | 58 | | |
| | 13 | 59 | | var names = new HashSet<string>(options.PropertyNameCaseInsensitive ? StringComparer.OrdinalIgnoreCase : StringC |
| | 296 | 60 | | foreach (var property in element.EnumerateObject()) |
| | | 61 | | { |
| | 137 | 62 | | if (!names.Add(property.Name)) |
| | | 63 | | { |
| | 4 | 64 | | throw new JsonException($"Activity serialization contains the duplicate property '{property.Name}'."); |
| | | 65 | | } |
| | | 66 | | } |
| | 9 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public string Serialize(object value) |
| | | 71 | | { |
| | 3 | 72 | | if (value is IActivity activity) |
| | | 73 | | { |
| | 2 | 74 | | return Serialize(activity); |
| | | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | var options = GetOptionsInternal(); |
| | 1 | 78 | | return JsonSerializer.Serialize(value, options); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | 894 | 82 | | public IActivity Deserialize(string serializedActivity) => JsonSerializer.Deserialize<IActivity>(serializedActivity, |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | 0 | 85 | | public object Deserialize(string serializedValue, Type type) => JsonSerializer.Deserialize(serializedValue, type, Ge |
| | | 86 | | |
| | | 87 | | /// <inheritdoc /> |
| | 936 | 88 | | public T Deserialize<T>(string serializedValue) => JsonSerializer.Deserialize<T>(serializedValue, GetOptions())!; |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | protected override void AddConverters(JsonSerializerOptions options) |
| | | 92 | | { |
| | 2872 | 93 | | options.Converters.Add(CreateInstance<TypeJsonConverter>()); |
| | 2872 | 94 | | options.Converters.Add(CreateInstance<InputJsonConverterFactory>()); |
| | 2872 | 95 | | options.Converters.Add(CreateInstance<OutputJsonConverterFactory>()); |
| | 2872 | 96 | | options.Converters.Add(CreateInstance<ExpressionJsonConverterFactory>()); |
| | 2872 | 97 | | options.Converters.Add(CreateInstance<FuncExpressionValueConverter>()); |
| | 2872 | 98 | | } |
| | | 99 | | } |