diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs
index cffc41ba3..0501f7b2a 100644
--- a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs
+++ b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs
@@ -8,37 +8,38 @@ namespace MediaBrowser.Common.Json.Converters
/// Converts a nullable struct or value to/from JSON.
/// Required - some clients send an empty string.
///
- /// The struct type.
- public class JsonNullableStructConverter : JsonConverter
- where T : struct
+ /// The struct type.
+ public class JsonNullableStructConverter : JsonConverter
+ where TStruct : struct
{
- private readonly JsonConverter _baseJsonConverter;
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The base json converter.
- public JsonNullableStructConverter(JsonConverter baseJsonConverter)
- {
- _baseJsonConverter = baseJsonConverter;
- }
-
///
- public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- // Handle empty string.
+ if (reader.TokenType == JsonTokenType.Null)
+ {
+ return null;
+ }
+
+ // Token is empty string.
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
{
return null;
}
- return _baseJsonConverter.Read(ref reader, typeToConvert, options);
+ return JsonSerializer.Deserialize(ref reader, options);
}
///
- public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
+ public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options)
{
- _baseJsonConverter.Write(writer, value, options);
+ if (value.HasValue)
+ {
+ JsonSerializer.Serialize(writer, value.Value, options);
+ }
+ else
+ {
+ writer.WriteNullValue();
+ }
}
}
}
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
diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs
index 67f7e8f14..6605ae962 100644
--- a/MediaBrowser.Common/Json/JsonDefaults.cs
+++ b/MediaBrowser.Common/Json/JsonDefaults.cs
@@ -39,14 +39,9 @@ namespace MediaBrowser.Common.Json
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
- // Get built-in converters for fallback converting.
- var baseNullableInt32Converter = (JsonConverter)options.GetConverter(typeof(int?));
- var baseNullableInt64Converter = (JsonConverter)options.GetConverter(typeof(long?));
-
options.Converters.Add(new JsonGuidConverter());
options.Converters.Add(new JsonStringEnumConverter());
- options.Converters.Add(new JsonNullableStructConverter(baseNullableInt32Converter));
- options.Converters.Add(new JsonNullableStructConverter(baseNullableInt64Converter));
+ options.Converters.Add(new JsonNullableStructConverterFactory());
return options;
}