| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Serializes <see cref="Type"/> objects to a simple alias representing the type. |
| | | 11 | | /// </summary> |
| | | 12 | | [UsedImplicitly] |
| | | 13 | | public class TypeJsonConverter : JsonConverter<Type> |
| | | 14 | | { |
| | | 15 | | private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 12868 | 18 | | public TypeJsonConverter(IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 19 | | { |
| | 12868 | 20 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 12868 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override bool CanConvert(Type typeToConvert) |
| | | 25 | | { |
| | 204907 | 26 | | return typeToConvert == typeof(Type) || typeToConvert.FullName == "System.RuntimeType"; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override Type? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 31 | | { |
| | 1571 | 32 | | var typeAlias = reader.GetString()!; |
| | | 33 | | |
| | | 34 | | // Handle array types. |
| | 1571 | 35 | | if (typeAlias.EndsWith("[]")) |
| | | 36 | | { |
| | 12 | 37 | | var elementTypeAlias = typeAlias[..^2]; |
| | 12 | 38 | | var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementT |
| | 12 | 39 | | return elementType.MakeArrayType(); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // Handle collection types. |
| | 1559 | 43 | | if (typeAlias.StartsWith("List<") && typeAlias.EndsWith(">")) |
| | | 44 | | { |
| | 9 | 45 | | var elementTypeAlias = typeAlias[5..^1]; |
| | 9 | 46 | | var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementT |
| | 9 | 47 | | return typeof(List<>).MakeGenericType(elementType); |
| | | 48 | | } |
| | | 49 | | |
| | 1550 | 50 | | return _wellKnownTypeRegistry.TryGetType(typeAlias, out var type) ? type : Type.GetType(typeAlias); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) |
| | | 55 | | { |
| | | 56 | | // Handle array types. |
| | 10854 | 57 | | if (value.IsArray) |
| | | 58 | | { |
| | 72 | 59 | | var elementType = value.GetElementType()!; |
| | 72 | 60 | | var elementTypeAlias = _wellKnownTypeRegistry.TryGetAlias(elementType, out var elementTypeAliasValue) ? elem |
| | 72 | 61 | | writer.WriteStringValue($"{elementTypeAlias}[]"); |
| | 72 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Handle collection types. |
| | 10782 | 66 | | if (value is { IsGenericType: true, GenericTypeArguments.Length: 1 }) |
| | | 67 | | { |
| | 780 | 68 | | var elementType = value.GenericTypeArguments.First(); |
| | 780 | 69 | | var typedEnumerable = typeof(IEnumerable<>).MakeGenericType(elementType); |
| | | 70 | | |
| | 780 | 71 | | if (typedEnumerable.IsAssignableFrom(value) && _wellKnownTypeRegistry.TryGetAlias(elementType, out var eleme |
| | | 72 | | { |
| | 92 | 73 | | writer.WriteStringValue($"List<{elementTypeAlias}>"); |
| | 92 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 10690 | 78 | | var typeAlias = _wellKnownTypeRegistry.TryGetAlias(value, out var alias) ? alias : value.GetSimpleAssemblyQualif |
| | 10690 | 79 | | writer.WriteStringValue(typeAlias); |
| | 10690 | 80 | | } |
| | | 81 | | } |