2021-06-19 16:02:33 +00:00
|
|
|
using System;
|
2021-01-03 16:35:22 +00:00
|
|
|
using System.ComponentModel;
|
2020-12-24 15:10:30 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
2021-06-19 16:02:33 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Omdb
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
|
|
|
|
/// </summary>
|
2021-01-04 14:52:44 +00:00
|
|
|
public class JsonOmdbNotAvailableInt32Converter : JsonConverter<int?>
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
/// <inheritdoc />
|
2021-01-04 14:52:44 +00:00
|
|
|
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
|
|
{
|
|
|
|
var str = reader.GetString();
|
2022-12-05 14:00:20 +00:00
|
|
|
if (str is null || str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-03 16:35:22 +00:00
|
|
|
|
|
|
|
var converter = TypeDescriptor.GetConverter(typeToConvert);
|
2021-01-04 14:52:44 +00:00
|
|
|
return (int?)converter.ConvertFromString(str);
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:14:02 +00:00
|
|
|
return JsonSerializer.Deserialize<int>(ref reader, options);
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-01-04 14:52:44 +00:00
|
|
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
2021-01-04 14:52:44 +00:00
|
|
|
if (value.HasValue)
|
|
|
|
{
|
|
|
|
writer.WriteNumberValue(value.Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writer.WriteNullValue();
|
|
|
|
}
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|