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