From 66a1880a580b25db7add41ed3947166b10415f37 Mon Sep 17 00:00:00 2001 From: crobibero Date: Sun, 6 Dec 2020 20:26:21 -0700 Subject: Add number to bool json converter --- .../Json/JsonBoolNumberTests.cs | 23 ++++++++++++++++++++++ .../Jellyfin.Common.Tests/Models/BoolTypeModel.cs | 17 ++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/Jellyfin.Common.Tests/Json/JsonBoolNumberTests.cs create mode 100644 tests/Jellyfin.Common.Tests/Models/BoolTypeModel.cs (limited to 'tests') diff --git a/tests/Jellyfin.Common.Tests/Json/JsonBoolNumberTests.cs b/tests/Jellyfin.Common.Tests/Json/JsonBoolNumberTests.cs new file mode 100644 index 0000000000..1cae5b8491 --- /dev/null +++ b/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(inputJson, options); + Assert.Equal(value?.Value, output); + } + } +} \ No newline at end of file diff --git a/tests/Jellyfin.Common.Tests/Models/BoolTypeModel.cs b/tests/Jellyfin.Common.Tests/Models/BoolTypeModel.cs new file mode 100644 index 0000000000..feb952efc5 --- /dev/null +++ b/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 +{ + /// + /// The bool type model. + /// + public class BoolTypeModel + { + /// + /// Gets or sets a value indicating whether the value is true or false. + /// + [JsonConverter(typeof(JsonBoolNumberConverter))] + public bool Value { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3