Add number to bool json converter

This commit is contained in:
crobibero
2020-12-06 20:26:21 -07:00
parent a7b461adb4
commit 66a1880a58
4 changed files with 77 additions and 0 deletions

View File

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

View File

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