فهرست منبع

Add number to bool json converter

crobibero 4 سال پیش
والد
کامیت
66a1880a58

+ 4 - 0
Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs

@@ -8,10 +8,12 @@ using System.Linq;
 using System.Net;
 using System.Net;
 using System.Net.Http;
 using System.Net.Http;
 using System.Text.Json;
 using System.Text.Json;
+using System.Text.Json.Serialization;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Common.Extensions;
 using MediaBrowser.Common.Extensions;
+using MediaBrowser.Common.Json.Converters;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Controller;
 using MediaBrowser.Controller;
 using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Configuration;
@@ -340,8 +342,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
 
 
             public string URL { get; set; }
             public string URL { get; set; }
 
 
+            [JsonConverter(typeof(JsonBoolNumberConverter))]
             public bool Favorite { get; set; }
             public bool Favorite { get; set; }
 
 
+            [JsonConverter(typeof(JsonBoolNumberConverter))]
             public bool DRM { get; set; }
             public bool DRM { get; set; }
 
 
             public int HD { get; set; }
             public int HD { get; set; }

+ 33 - 0
MediaBrowser.Common/Json/Converters/JsonBoolNumberConverter.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace MediaBrowser.Common.Json.Converters
+{
+    /// <summary>
+    /// 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 />
+        public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+        {
+            if (reader.TokenType == JsonTokenType.Number)
+            {
+                return Convert.ToBoolean(reader.GetInt32());
+            }
+
+            return JsonSerializer.Deserialize<bool>(ref reader, options);
+        }
+
+        /// <inheritdoc />
+        public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
+        {
+            JsonSerializer.Serialize(writer, value, options);
+        }
+    }
+}

+ 23 - 0
tests/Jellyfin.Common.Tests/Json/JsonBoolNumberTests.cs

@@ -0,0 +1,23 @@
+using System.Text.Json;
+using Jellyfin.Common.Tests.Models;
+using Xunit;
+
+namespace Jellyfin.Common.Tests.Json
+{
+    public static class JsonBoolNumberTests
+    {
+        [Theory]
+        [InlineData("1", true)]
+        [InlineData("0", false)]
+        [InlineData("2", true)]
+        [InlineData("true", true)]
+        [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);
+        }
+    }
+}

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

@@ -0,0 +1,17 @@
+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; }
+    }
+}