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. /// Converts a number to a boolean.
/// This is needed for HDHomerun. /// This is needed for HDHomerun.
/// </summary> /// </summary>
/// <remarks>
/// Adding this to the JsonConverter list causes recursion.
/// </remarks>
public class JsonBoolNumberConverter : JsonConverter<bool> public class JsonBoolNumberConverter : JsonConverter<bool>
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -21,7 +18,7 @@ namespace MediaBrowser.Common.Json.Converters
return Convert.ToBoolean(reader.GetInt32()); return Convert.ToBoolean(reader.GetInt32());
} }
return JsonSerializer.Deserialize<bool>(ref reader, options); return reader.GetBoolean();
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -43,6 +43,7 @@ namespace MediaBrowser.Common.Json
options.Converters.Add(new JsonVersionConverter()); options.Converters.Add(new JsonVersionConverter());
options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new JsonNullableStructConverterFactory()); options.Converters.Add(new JsonNullableStructConverterFactory());
options.Converters.Add(new JsonBoolNumberConverter());
return options; return options;
} }

View File

@ -1,5 +1,5 @@
using System.Text.Json; using System.Text.Json;
using Jellyfin.Common.Tests.Models; using MediaBrowser.Common.Json.Converters;
using Xunit; using Xunit;
namespace Jellyfin.Common.Tests.Json namespace Jellyfin.Common.Tests.Json
@ -14,10 +14,10 @@ namespace Jellyfin.Common.Tests.Json
[InlineData("false", false)] [InlineData("false", false)]
public static void Deserialize_Number_Valid_Success(string input, bool? output) public static void Deserialize_Number_Valid_Success(string input, bool? output)
{ {
var inputJson = $"{{ \"Value\": {input} }}";
var options = new JsonSerializerOptions(); var options = new JsonSerializerOptions();
var value = JsonSerializer.Deserialize<BoolTypeModel>(inputJson, options); options.Converters.Add(new JsonBoolNumberConverter());
Assert.Equal(value?.Value, output); 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; }
}
}