diff options
| author | Bond-009 <bond.009@outlook.com> | 2020-10-02 19:57:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-02 19:57:47 +0200 |
| commit | c3655e0e2ef5d29eed07c228be17be942dff7c7f (patch) | |
| tree | 169b699b2ae0d3bdbd4fc01b0fd8065a08c281f5 /MediaBrowser.Common/Json/Converters/JsonNullableStructConverterFactory.cs | |
| parent | c7b3d4a90c946f9a2438622cc0ca43d19b84bef8 (diff) | |
| parent | ac790cd77b81a8235f6d1faf9512c85c96fcd088 (diff) | |
Merge pull request #4217 from crobibero/json-nullable-struct-converter
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 |
