You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
3 years ago
|
#nullable enable
|
||
|
|
||
|
using System;
|
||
4 years ago
|
using System.ComponentModel;
|
||
4 years ago
|
using System.Text.Json;
|
||
|
using System.Text.Json.Serialization;
|
||
|
|
||
3 years ago
|
namespace MediaBrowser.Providers.Plugins.Omdb
|
||
4 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
|
||
|
/// </summary>
|
||
4 years ago
|
public class JsonOmdbNotAvailableInt32Converter : JsonConverter<int?>
|
||
4 years ago
|
{
|
||
|
/// <inheritdoc />
|
||
4 years ago
|
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||
4 years ago
|
{
|
||
|
if (reader.TokenType == JsonTokenType.String)
|
||
|
{
|
||
|
var str = reader.GetString();
|
||
|
if (str != null && str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
4 years ago
|
|
||
|
var converter = TypeDescriptor.GetConverter(typeToConvert);
|
||
4 years ago
|
return (int?)converter.ConvertFromString(str);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
return JsonSerializer.Deserialize<int>(ref reader, options);
|
||
4 years ago
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
4 years ago
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
||
4 years ago
|
{
|
||
4 years ago
|
if (value.HasValue)
|
||
|
{
|
||
|
writer.WriteNumberValue(value.Value);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
writer.WriteNullValue();
|
||
|
}
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|