| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Expressions.Services; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A JSON converter factory that creates <see cref="PolymorphicObjectConverter"/> instances. |
| | | 11 | | /// </summary> |
| | | 12 | | public class PolymorphicObjectConverterFactory : JsonConverterFactory |
| | | 13 | | { |
| | | 14 | | private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// A JSON converter factory that creates <see cref="PolymorphicObjectConverter"/> instances. |
| | | 18 | | /// </summary> |
| | 16742 | 19 | | public PolymorphicObjectConverterFactory(IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 20 | | { |
| | 16742 | 21 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 16742 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Default constructor for use with attributes. |
| | | 26 | | /// </summary> |
| | 4022 | 27 | | public PolymorphicObjectConverterFactory() |
| | | 28 | | { |
| | 4022 | 29 | | _wellKnownTypeRegistry = WellKnownTypeRegistry.CreateDefault(); |
| | 4022 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override bool CanConvert(Type typeToConvert) |
| | | 34 | | { |
| | 75328 | 35 | | if (typeToConvert.IsClass |
| | 75328 | 36 | | && typeToConvert == typeof(object) |
| | 75328 | 37 | | || typeToConvert == typeof(ExpandoObject) |
| | 75328 | 38 | | || typeToConvert == typeof(Dictionary<string, object>)) |
| | 14506 | 39 | | return true; |
| | | 40 | | |
| | 60822 | 41 | | if (typeToConvert.IsInterface |
| | 60822 | 42 | | && typeToConvert == typeof(IDictionary<string, object>)) |
| | 8373 | 43 | | return true; |
| | | 44 | | |
| | 52449 | 45 | | return false; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | | 50 | | { |
| | 38933 | 51 | | if (typeof(IDictionary<string, object>).IsAssignableFrom(typeToConvert)) |
| | 14798 | 52 | | return new PolymorphicDictionaryConverter(options, _wellKnownTypeRegistry); |
| | | 53 | | |
| | 24135 | 54 | | return new PolymorphicObjectConverter(); |
| | | 55 | | } |
| | | 56 | | } |