瀏覽代碼

Simplify json converters

crobibero 4 年之前
父節點
當前提交
9ddf550b43

+ 0 - 41
MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs

@@ -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);
-        }
-    }
-}

+ 10 - 7
MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs → 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);
         }

+ 2 - 2
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;
         }