diff options
| author | Ryan Petris <ryan@petris.net> | 2020-11-15 19:55:30 -0700 |
|---|---|---|
| committer | Ryan Petris <ryan@petris.net> | 2020-11-15 19:55:30 -0700 |
| commit | 8204b25a56af7ddb20497f3d7ad8e1d8daaff7ca (patch) | |
| tree | f8da813808df73670baa4eecfc67db319783aaa1 /MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs | |
| parent | 81d5eb4db5918debe3194236288492f26ad135bb (diff) | |
| parent | 8d1d77db94a4fcfc6d8a37b38100ce0b7267690b (diff) | |
Merge branch 'master' into fix-hdhomerun
Diffstat (limited to 'MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs')
| -rw-r--r-- | MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs | 27 |
1 files changed, 27 insertions, 0 deletions
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 +{ + /// <summary> + /// Json nullable struct converter factory. + /// </summary> + public class JsonNullableStructConverterFactory : JsonConverterFactory + { + /// <inheritdoc /> + public override bool CanConvert(Type typeToConvert) + { + return typeToConvert.IsGenericType + && typeToConvert.GetGenericTypeDefinition() == typeof(Nullable<>) + && typeToConvert.GenericTypeArguments[0].IsValueType; + } + + /// <inheritdoc /> + 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 |
