diff options
| author | crobibero <cody@robibe.ro> | 2020-09-27 09:45:11 -0600 |
|---|---|---|
| committer | crobibero <cody@robibe.ro> | 2020-09-27 09:45:11 -0600 |
| commit | ac790cd77b81a8235f6d1faf9512c85c96fcd088 (patch) | |
| tree | ca103ecc801056489605bb0916664aff0c259515 /MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs | |
| parent | 800c03961281d4f2ee6d3d7c9d9c0db6f45f506a (diff) | |
Properly handle null structs in json
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 |
