|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | +using OneBitSoftware.Utilities.Errors; |
| 9 | + |
| 10 | +namespace OneBitSoftware.Utilities |
| 11 | +{ |
| 12 | + public class PolymorphicOperationErrorListConverter<T> : JsonConverter<T> |
| 13 | + where T : IList<IOperationError> |
| 14 | + { |
| 15 | + private readonly Dictionary<string, Type> _valueMappings = new Dictionary<string, Type>(); |
| 16 | + private readonly Dictionary<Type, string> _typeMappings = new Dictionary<Type, string>(); |
| 17 | + protected virtual string TypePropertyName => "type"; |
| 18 | + |
| 19 | + public PolymorphicOperationErrorListConverter() |
| 20 | + { |
| 21 | + // Define the base OperationError custom type mapping discriminator |
| 22 | + this.AddMapping("operation_error", typeof(List<IOperationError>)); |
| 23 | + } |
| 24 | + |
| 25 | + public override bool CanConvert(Type typeToConvert) |
| 26 | + { |
| 27 | + if (typeToConvert is null) return false; |
| 28 | + return typeof(T).IsAssignableFrom(typeToConvert); |
| 29 | + } |
| 30 | + |
| 31 | + public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + // Deserialize the JSON to the specified type. |
| 36 | + var serializationOptions = this.ConstructSafeFallbackOptions(options); |
| 37 | + serializationOptions.Converters.Add(new ReadOnlyPartialConverter(this)); |
| 38 | + return (T)JsonSerializer.Deserialize(ref reader, typeToConvert, serializationOptions); |
| 39 | + } |
| 40 | + catch (Exception ex) |
| 41 | + { |
| 42 | + throw new InvalidOperationException("Invalid JSON in request.", ex); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) |
| 47 | + { |
| 48 | + if (value is null) |
| 49 | + { |
| 50 | + writer.WriteNullValue(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (this._typeMappings.TryGetValue(value.GetType(), out var typeValue) == false) throw new InvalidOperationException($"Model of type {value.GetType()} cannot be successfully serialized."); |
| 55 | + |
| 56 | + writer.WriteStartArray(); |
| 57 | + |
| 58 | + foreach (var error in value) |
| 59 | + { |
| 60 | + writer.WriteStartObject(); |
| 61 | + writer.WriteString(this.TypePropertyName, typeValue); |
| 62 | + writer.WriteString(propertyName: nameof(IOperationError.Code), value: error.Code.ToString()); |
| 63 | + writer.WriteString( |
| 64 | + propertyName: nameof(IOperationError.Message), |
| 65 | + value: String.IsNullOrEmpty(error.Message) ? string.Empty : error.Message.ToString()); |
| 66 | + writer.WriteString( |
| 67 | + propertyName: nameof(IOperationError.Details), |
| 68 | + value: String.IsNullOrEmpty(error.Details) ? string.Empty : error.Details.ToString()); |
| 69 | + writer.WriteEndObject(); |
| 70 | + } |
| 71 | + |
| 72 | + writer.WriteEndArray(); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + protected bool AddMapping(string typeValue, Type type) |
| 77 | + { |
| 78 | + if (string.IsNullOrWhiteSpace(typeValue) || type is null) return false; |
| 79 | + if (this._valueMappings.ContainsKey(typeValue) || this._typeMappings.ContainsKey(type)) return false; |
| 80 | + |
| 81 | + this._valueMappings[typeValue] = type; |
| 82 | + this._typeMappings[type] = typeValue; |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + private Type GetType(JsonElement typeElement) |
| 87 | + { |
| 88 | + if (typeElement.ValueKind != JsonValueKind.String) return null; |
| 89 | + |
| 90 | + var stringValue = typeElement.GetString(); |
| 91 | + if (string.IsNullOrWhiteSpace(stringValue)) return null; |
| 92 | + |
| 93 | + this._valueMappings.TryGetValue(stringValue, out var type); |
| 94 | + return type; |
| 95 | + } |
| 96 | + |
| 97 | + private JsonSerializerOptions ConstructSafeFallbackOptions(JsonSerializerOptions options) |
| 98 | + { |
| 99 | + var fallbackSerializationOptions = new JsonSerializerOptions(options); |
| 100 | + fallbackSerializationOptions.Converters.Remove(this); |
| 101 | + return fallbackSerializationOptions; |
| 102 | + } |
| 103 | + |
| 104 | + private class ReadOnlyPartialConverter : JsonConverter<T> |
| 105 | + { |
| 106 | + private readonly PolymorphicOperationErrorListConverter<T> _polymorphicConverter; |
| 107 | + |
| 108 | + internal ReadOnlyPartialConverter(PolymorphicOperationErrorListConverter<T> polymorphicConverter) |
| 109 | + { |
| 110 | + this._polymorphicConverter = polymorphicConverter ?? throw new ArgumentNullException(nameof(polymorphicConverter)); |
| 111 | + } |
| 112 | + |
| 113 | + public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(T); |
| 114 | + |
| 115 | + public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 116 | + { |
| 117 | + // We copy the value here so we can easily reuse the reader for the subsequent deserialization. |
| 118 | + var readerRestore = reader; |
| 119 | + |
| 120 | + // Get the `type` value by parsing the JSON string into a JsonDocument. |
| 121 | + var jsonDocument = JsonDocument.ParseValue(ref reader); |
| 122 | + jsonDocument.RootElement.TryGetProperty(this._polymorphicConverter.TypePropertyName, out var typeElement); |
| 123 | + |
| 124 | + var returnType = this._polymorphicConverter.GetType(typeElement); |
| 125 | + if (returnType is null) throw new InvalidOperationException("The received JSON cannot be deserialized to any known type."); |
| 126 | + |
| 127 | + try |
| 128 | + { |
| 129 | + // Deserialize the JSON to the specified type. |
| 130 | + return (T)JsonSerializer.Deserialize(ref readerRestore, returnType, options); |
| 131 | + } |
| 132 | + catch (Exception ex) |
| 133 | + { |
| 134 | + throw new InvalidOperationException("Invalid JSON in request.", ex); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => throw new InvalidOperationException("The `Read only partial converter` cannot be used for serialization."); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments