2021-06-19 16:02:33 +00:00
|
|
|
using System;
|
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-05-05 10:57:01 +00:00
|
|
|
public class JsonOmdbNotAvailableStringConverter : JsonConverter<string?>
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
/// <inheritdoc />
|
2021-05-05 10:57:01 +00:00
|
|
|
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
2021-05-05 10:57:01 +00:00
|
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-24 15:10:30 +00:00
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
|
|
{
|
2021-05-05 10:57:01 +00:00
|
|
|
// GetString can't return null here because we already handled it above
|
|
|
|
var str = reader.GetString()!;
|
|
|
|
if (str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-25 18:21:58 +00:00
|
|
|
|
|
|
|
return str;
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 10:57:01 +00:00
|
|
|
return JsonSerializer.Deserialize<string?>(ref reader, options);
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-05-05 10:57:01 +00:00
|
|
|
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
|
2020-12-24 15:10:30 +00:00
|
|
|
{
|
2021-01-04 14:52:44 +00:00
|
|
|
writer.WriteStringValue(value);
|
2020-12-24 15:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|