Bladeren bron

Simplify converter

crobibero 4 jaren geleden
bovenliggende
commit
6e98378447

+ 1 - 4
MediaBrowser.Common/Json/Converters/JsonBoolNumberConverter.cs

@@ -8,9 +8,6 @@ namespace MediaBrowser.Common.Json.Converters
     /// Converts a number to a boolean.
     /// This is needed for HDHomerun.
     /// </summary>
-    /// <remarks>
-    /// Adding this to the JsonConverter list causes recursion.
-    /// </remarks>
     public class JsonBoolNumberConverter : JsonConverter<bool>
     {
         /// <inheritdoc />
@@ -21,7 +18,7 @@ namespace MediaBrowser.Common.Json.Converters
                 return Convert.ToBoolean(reader.GetInt32());
             }
 
-            return JsonSerializer.Deserialize<bool>(ref reader, options);
+            return reader.GetBoolean();
         }
 
         /// <inheritdoc />

+ 1 - 0
MediaBrowser.Common/Json/JsonDefaults.cs

@@ -43,6 +43,7 @@ namespace MediaBrowser.Common.Json
             options.Converters.Add(new JsonVersionConverter());
             options.Converters.Add(new JsonStringEnumConverter());
             options.Converters.Add(new JsonNullableStructConverterFactory());
+            options.Converters.Add(new JsonBoolNumberConverter());
 
             return options;
         }

+ 4 - 4
tests/Jellyfin.Common.Tests/Json/JsonBoolNumberTests.cs

@@ -1,5 +1,5 @@
 using System.Text.Json;
-using Jellyfin.Common.Tests.Models;
+using MediaBrowser.Common.Json.Converters;
 using Xunit;
 
 namespace Jellyfin.Common.Tests.Json
@@ -14,10 +14,10 @@ namespace Jellyfin.Common.Tests.Json
         [InlineData("false", false)]
         public static void Deserialize_Number_Valid_Success(string input, bool? output)
         {
-            var inputJson = $"{{ \"Value\": {input} }}";
             var options = new JsonSerializerOptions();
-            var value = JsonSerializer.Deserialize<BoolTypeModel>(inputJson, options);
-            Assert.Equal(value?.Value, output);
+            options.Converters.Add(new JsonBoolNumberConverter());
+            var value = JsonSerializer.Deserialize<bool>(input, options);
+            Assert.Equal(value, output);
         }
     }
 }

+ 0 - 17
tests/Jellyfin.Common.Tests/Models/BoolTypeModel.cs

@@ -1,17 +0,0 @@
-using System.Text.Json.Serialization;
-using MediaBrowser.Common.Json.Converters;
-
-namespace Jellyfin.Common.Tests.Models
-{
-    /// <summary>
-    /// The bool type model.
-    /// </summary>
-    public class BoolTypeModel
-    {
-        /// <summary>
-        /// Gets or sets a value indicating whether the value is true or false.
-        /// </summary>
-        [JsonConverter(typeof(JsonBoolNumberConverter))]
-        public bool Value { get; set; }
-    }
-}