diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2021-09-06 13:42:48 +0900 |
|---|---|---|
| committer | dkanada <dkanada@users.noreply.github.com> | 2021-09-06 13:42:48 +0900 |
| commit | f6c0db4bb5007182d79ceb809675b90909fd1fa0 (patch) | |
| tree | ab13e60d0cba2477585a6d0ec601f10ad9f112b2 /src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs | |
| parent | 776ce7c660a6d6bf975766378d6db7124f4ac232 (diff) | |
| parent | e9508616cc90c01a22ca28c13694587dd16b49d6 (diff) | |
merge branch 'master' into syncplay-sessions-fix
Diffstat (limited to 'src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs')
| -rw-r--r-- | src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs b/src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs new file mode 100644 index 000000000..e7749589a --- /dev/null +++ b/src/Jellyfin.Extensions/Json/Converters/JsonNullableStructConverterFactory.cs @@ -0,0 +1,27 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Jellyfin.Extensions.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)); + } + } +} |
