| | | 1 | | using pva.SuperV.Engine.Processing; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace pva.SuperV.Engine.JsonConverters |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Json converter for field value processing |
| | | 10 | | /// </summary> |
| | | 11 | | /// <seealso cref="JsonConverter{IFieldValueProcessing}" /> |
| | | 12 | | public class FieldValueProcessingJsonConverter : JsonConverter<IFieldValueProcessing> |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The field converters cache. |
| | | 16 | | /// </summary> |
| | 2 | 17 | | private static readonly Dictionary<Type, dynamic> ArgConvertersCache = []; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Reads and converts the JSON to type. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="reader">The reader.</param> |
| | | 23 | | /// <param name="typeToConvert">The type to convert.</param> |
| | | 24 | | /// <param name="options">An object that specifies serialization options to use.</param> |
| | | 25 | | /// <returns> |
| | | 26 | | /// The converted value. |
| | | 27 | | /// </returns> |
| | | 28 | | /// <exception cref="JsonException"></exception> |
| | | 29 | | public override IFieldValueProcessing Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions |
| | 3 | 30 | | { |
| | 3 | 31 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.StartObject, false); |
| | | 32 | | |
| | 3 | 33 | | string? fieldValueProcessingTypeString = JsonHelpers.GetStringPropertyFromUtfReader(ref reader, "Type"); |
| | 3 | 34 | | string? fieldValueProcessingName = JsonHelpers.GetStringPropertyFromUtfReader(ref reader, "Name"); |
| | | 35 | | |
| | 3 | 36 | | List<object> ctorArguments = ReadParameters(ref reader, options); |
| | 3 | 37 | | Type? fieldType = Type.GetType(fieldValueProcessingTypeString!); |
| | | 38 | | |
| | 3 | 39 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.EndObject); |
| | | 40 | | |
| | 3 | 41 | | IFieldValueProcessing fieldValueProcessing = CreateInstance(fieldType!); |
| | 3 | 42 | | fieldValueProcessing.Name = fieldValueProcessingName!; |
| | 3 | 43 | | fieldValueProcessing.CtorArguments = ctorArguments; |
| | 3 | 44 | | return fieldValueProcessing; |
| | 3 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static List<object> ReadParameters(ref Utf8JsonReader reader, JsonSerializerOptions options) |
| | 3 | 48 | | { |
| | 3 | 49 | | List<object> ctorArguments = []; |
| | 3 | 50 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.PropertyName); |
| | 3 | 51 | | JsonHelpers.ReadPropertyName(ref reader, "Params"); |
| | | 52 | | |
| | 3 | 53 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.StartArray); |
| | 32 | 54 | | while (reader.Read()) |
| | 32 | 55 | | { |
| | 32 | 56 | | if (reader.TokenType == JsonTokenType.EndArray) |
| | 3 | 57 | | { |
| | 3 | 58 | | return ctorArguments; |
| | | 59 | | } |
| | 29 | 60 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.StartObject, false); |
| | 29 | 61 | | string? paramTypeString = JsonHelpers.GetStringPropertyFromUtfReader(ref reader, "Type"); |
| | | 62 | | |
| | 29 | 63 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.PropertyName); |
| | 29 | 64 | | JsonHelpers.ReadPropertyName(ref reader, "Value"); |
| | | 65 | | |
| | 29 | 66 | | reader.Read(); |
| | 29 | 67 | | Type? paramType = Type.GetType(paramTypeString!); |
| | 29 | 68 | | dynamic? argValue = JsonSerializer.Deserialize(ref reader, paramType!, options); |
| | 29 | 69 | | JsonHelpers.ReadTokenType(ref reader, JsonTokenType.EndObject); |
| | 29 | 70 | | ctorArguments.Add(argValue); |
| | 29 | 71 | | } |
| | 0 | 72 | | return ctorArguments; |
| | 3 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Writes the specified writer. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="writer">The writer.</param> |
| | | 79 | | /// <param name="fieldValueProcessing">The field processing.</param> |
| | | 80 | | /// <param name="options">The options.</param> |
| | | 81 | | public override void Write(Utf8JsonWriter writer, IFieldValueProcessing fieldValueProcessing, JsonSerializerOpti |
| | 6 | 82 | | { |
| | 6 | 83 | | writer.WriteStartObject(); |
| | 6 | 84 | | writer.WriteString("Type", fieldValueProcessing.GetType().ToString()); |
| | 6 | 85 | | writer.WriteString("Name", fieldValueProcessing.Name); |
| | 6 | 86 | | WriteCtorParameters(writer, fieldValueProcessing, options); |
| | 6 | 87 | | writer.WriteEndObject(); |
| | 6 | 88 | | } |
| | | 89 | | |
| | | 90 | | private static void WriteCtorParameters(Utf8JsonWriter writer, IFieldValueProcessing fieldValueProcessing, JsonS |
| | 6 | 91 | | { |
| | 6 | 92 | | writer.WriteStartArray("Params"); |
| | 6 | 93 | | fieldValueProcessing.CtorArguments.ForEach(arg => |
| | 58 | 94 | | { |
| | 58 | 95 | | Type argType = arg.GetType(); |
| | 58 | 96 | | writer.WriteStartObject(); |
| | 58 | 97 | | writer.WriteString("Type", argType.ToString()); |
| | 58 | 98 | | if (!ArgConvertersCache.TryGetValue(argType, out dynamic? argConverter)) |
| | 2 | 99 | | { |
| | 2 | 100 | | argConverter = JsonSerializerOptions.Default.GetConverter(argType); |
| | 2 | 101 | | ArgConvertersCache.Add(argType, argConverter); |
| | 2 | 102 | | } |
| | 58 | 103 | | writer.WritePropertyName("Value"); |
| | 58 | 104 | | JsonSerializer.Serialize(writer, arg, options); |
| | 58 | 105 | | writer.WriteEndObject(); |
| | 64 | 106 | | }); |
| | 6 | 107 | | writer.WriteEndArray(); |
| | 6 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Creates an instance for targetType's <see cref="FieldValueProcessing{T}"/>. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="targetType">Type of the target.</param> |
| | | 114 | | /// <returns><see cref="IFieldValueProcessing"/> created instance.</returns> |
| | | 115 | | private static IFieldValueProcessing CreateInstance(Type targetType) |
| | 3 | 116 | | { |
| | 3 | 117 | | var ctor = GetConstructor(targetType); |
| | 3 | 118 | | return (IFieldValueProcessing)ctor.Invoke([]); |
| | 3 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Gets the constructor for targetType's <see cref="FieldValueProcessing{T}"/>. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="fieldProcesingType">Type of the target.</param> |
| | | 125 | | /// <returns></returns> |
| | | 126 | | /// <exception cref="InvalidOperationException">No constructor found for FieldValueProcessing{targetType.Name}.< |
| | | 127 | | private static ConstructorInfo GetConstructor(Type fieldProcesingType) |
| | 3 | 128 | | { |
| | 3 | 129 | | return fieldProcesingType |
| | 3 | 130 | | .GetConstructor([]) |
| | 3 | 131 | | ?? throw new InvalidOperationException($"No constructor found for {fieldProcesingType.Name}."); |
| | 3 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |