mirror of https://github.com/jellyfin/jellyfin.git
Add number to bool json converter
This commit is contained in:
parent
a7b461adb4
commit
66a1880a58
|
@ -8,10 +8,12 @@ using System.Linq;
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.Json.Converters;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
|
@ -340,8 +342,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|||
|
||||
public string URL { get; set; }
|
||||
|
||||
[JsonConverter(typeof(JsonBoolNumberConverter))]
|
||||
public bool Favorite { get; set; }
|
||||
|
||||
[JsonConverter(typeof(JsonBoolNumberConverter))]
|
||||
public bool DRM { get; set; }
|
||||
|
||||
public int HD { get; set; }
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue