Simplify converter

This commit is contained in:
crobibero 2020-12-07 14:58:27 -07:00
parent 66a1880a58
commit 6e98378447
4 changed files with 6 additions and 25 deletions

View File

@ -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 />

View File

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

View File

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

View File

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