From ac790cd77b81a8235f6d1faf9512c85c96fcd088 Mon Sep 17 00:00:00 2001 From: crobibero Date: Sun, 27 Sep 2020 09:45:11 -0600 Subject: Properly handle null structs in json --- .../JsonNullableStructConverterFactory.cs | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs (limited to 'MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs') diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs new file mode 100644 index 000000000..d5b54e3ca --- /dev/null +++ b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs @@ -0,0 +1,27 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace MediaBrowser.Common.Json.Converters +{ + /// + /// Json nullable struct converter factory. + /// + public class JsonNullableStructConverterFactory : JsonConverterFactory + { + /// + public override bool CanConvert(Type typeToConvert) + { + return typeToConvert.IsGenericType + && typeToConvert.GetGenericTypeDefinition() == typeof(Nullable<>) + && typeToConvert.GenericTypeArguments[0].IsValueType; + } + + /// + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + var structType = typeToConvert.GenericTypeArguments[0]; + return (JsonConverter)Activator.CreateInstance(typeof(JsonNullableStructConverter<>).MakeGenericType(structType)); + } + } +} \ No newline at end of file -- cgit v1.2.3