| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Serialization.Helpers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// The ActivityWriter class is responsible for writing an activity to a JSON writer using the provided options. |
| | | 10 | | /// </summary> |
| | 14 | 11 | | public class ActivityWriter(IActivityRegistry activityRegistry, SyntheticPropertiesWriter syntheticPropertiesWriter, ILo |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Writes an activity to a JSON writer using the provided options. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="writer">The JSON writer.</param> |
| | | 17 | | /// <param name="value">The activity to write.</param> |
| | | 18 | | /// <param name="options">The JSON serialization options.</param> |
| | | 19 | | /// <param name="ignoreSpecializedConverters">Controls whether to ignore the availability of converters that can han |
| | | 20 | | /// <param name="excludeChildren">A flag indicating whether to exclude child activities.</param> |
| | | 21 | | /// <param name="propertyFilter">An additional property filter. Returning true will skip the property.</param> |
| | | 22 | | public void WriteActivity(Utf8JsonWriter writer, IActivity? value, JsonSerializerOptions options, bool ignoreSpecial |
| | | 23 | | { |
| | 2335 | 24 | | if (value == null) |
| | | 25 | | { |
| | 0 | 26 | | writer.WriteNullValue(); |
| | 0 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | |
| | 2335 | 30 | | if (!ignoreSpecializedConverters) |
| | | 31 | | { |
| | | 32 | | // Check if there's a specialized converter for the activity. |
| | 1151 | 33 | | var valueType = value.GetType(); |
| | 22344 | 34 | | var specializedConverter = options.Converters.FirstOrDefault(x => x.CanConvert(valueType)); |
| | 1151 | 35 | | if (specializedConverter != null) |
| | | 36 | | { |
| | 1150 | 37 | | JsonSerializer.Serialize(writer, value, valueType, options); |
| | 1150 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 1185 | 42 | | writer.WriteStartObject(); |
| | | 43 | | |
| | 1185 | 44 | | var properties = value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| | | 45 | | |
| | 37984 | 46 | | foreach (var property in properties) |
| | | 47 | | { |
| | 17807 | 48 | | if (property.GetCustomAttribute<JsonIgnoreAttribute>() != null) |
| | | 49 | | continue; |
| | | 50 | | |
| | 11760 | 51 | | if (excludeChildren) |
| | | 52 | | { |
| | 0 | 53 | | if (typeof(IActivity).IsAssignableFrom(property.PropertyType)) |
| | | 54 | | continue; |
| | | 55 | | |
| | 0 | 56 | | if (typeof(IEnumerable<IActivity>).IsAssignableFrom(property.PropertyType)) |
| | | 57 | | continue; |
| | | 58 | | } |
| | | 59 | | |
| | 11760 | 60 | | if (propertyFilter?.Invoke(property) == true) |
| | | 61 | | continue; |
| | | 62 | | |
| | 11700 | 63 | | var propName = options.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name; |
| | 11700 | 64 | | writer.WritePropertyName(propName); |
| | 11700 | 65 | | var input = property.GetValue(value); |
| | | 66 | | |
| | 11700 | 67 | | if (input == null) |
| | | 68 | | { |
| | 2127 | 69 | | writer.WriteNullValue(); |
| | 2127 | 70 | | continue; |
| | | 71 | | } |
| | | 72 | | |
| | 9573 | 73 | | if (property.Name == nameof(IActivity.CustomProperties)) |
| | | 74 | | { |
| | 1185 | 75 | | var customProperties = new Dictionary<string, object>(value.CustomProperties); |
| | 5410 | 76 | | foreach (var kvp in customProperties) |
| | | 77 | | { |
| | 1520 | 78 | | if (kvp.Value is IActivity or IEnumerable<IActivity>) |
| | 0 | 79 | | customProperties.Remove(kvp.Key); |
| | | 80 | | } |
| | | 81 | | |
| | 1185 | 82 | | input = customProperties; |
| | | 83 | | } |
| | | 84 | | |
| | 9573 | 85 | | JsonSerializer.Serialize(writer, input, options); |
| | | 86 | | } |
| | | 87 | | |
| | 1185 | 88 | | var activityDescriptor = activityRegistry.Find(value.Type, value.Version); |
| | | 89 | | |
| | 1185 | 90 | | if (activityDescriptor == null) |
| | 0 | 91 | | logger.LogDebug("No descriptor found for activity {ActivityType}", value.GetType().Name); |
| | | 92 | | else |
| | 1185 | 93 | | syntheticPropertiesWriter.WriteSyntheticProperties(writer, value, activityDescriptor, options); |
| | | 94 | | |
| | 1185 | 95 | | writer.WriteEndObject(); |
| | 1185 | 96 | | } |
| | | 97 | | } |