diff options
| -rw-r--r-- | MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs | 41 | ||||
| -rw-r--r-- | MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs (renamed from MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs) | 17 | ||||
| -rw-r--r-- | MediaBrowser.Common/Json/JsonDefaults.cs | 4 |
3 files changed, 12 insertions, 50 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs deleted file mode 100644 index 38e1f8e2f..000000000 --- a/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace MediaBrowser.Common.Json.Converters -{ - /// <summary> - /// Converts a nullable int64 object or value to/from JSON. - /// Required - some clients send an empty string. - /// </summary> - public class JsonNullableInt64Converter : JsonConverter<long?> - { - private readonly JsonConverter<long?> _baseJsonConverter; - - /// <summary> - /// Initializes a new instance of the <see cref="JsonNullableInt64Converter"/> class. - /// </summary> - /// <param name="baseJsonConverter">The base json converter.</param> - public JsonNullableInt64Converter(JsonConverter<long?> baseJsonConverter) - { - _baseJsonConverter = baseJsonConverter; - } - - /// <inheritdoc /> - public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) - { - return null; - } - - return _baseJsonConverter.Read(ref reader, typeToConvert, options); - } - - /// <inheritdoc /> - public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options) - { - _baseJsonConverter.Write(writer, value, options); - } - } -} diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs index a4ecc542e..cffc41ba3 100644 --- a/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs @@ -5,25 +5,28 @@ using System.Text.Json.Serialization; namespace MediaBrowser.Common.Json.Converters { /// <summary> - /// Converts a nullable int32 object or value to/from JSON. + /// Converts a nullable struct or value to/from JSON. /// Required - some clients send an empty string. /// </summary> - public class JsonNullableInt32Converter : JsonConverter<int?> + /// <typeparam name="T">The struct type.</typeparam> + public class JsonNullableStructConverter<T> : JsonConverter<T?> + where T : struct { - private readonly JsonConverter<int?> _baseJsonConverter; + private readonly JsonConverter<T?> _baseJsonConverter; /// <summary> - /// Initializes a new instance of the <see cref="JsonNullableInt32Converter"/> class. + /// Initializes a new instance of the <see cref="JsonNullableStructConverter{T}"/> class. /// </summary> /// <param name="baseJsonConverter">The base json converter.</param> - public JsonNullableInt32Converter(JsonConverter<int?> baseJsonConverter) + public JsonNullableStructConverter(JsonConverter<T?> baseJsonConverter) { _baseJsonConverter = baseJsonConverter; } /// <inheritdoc /> - public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + // Handle empty string. if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) { return null; @@ -33,7 +36,7 @@ namespace MediaBrowser.Common.Json.Converters } /// <inheritdoc /> - public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) { _baseJsonConverter.Write(writer, value, options); } diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index bbdd1029a..5867cd4a0 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -35,8 +35,8 @@ namespace MediaBrowser.Common.Json options.Converters.Add(new JsonGuidConverter()); options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new JsonNullableInt32Converter(baseNullableInt32Converter)); - options.Converters.Add(new JsonNullableInt64Converter(baseNullableInt64Converter)); + options.Converters.Add(new JsonNullableStructConverter<int>(baseNullableInt32Converter)); + options.Converters.Add(new JsonNullableStructConverter<long>(baseNullableInt64Converter)); return options; } |
