| | | 1 | | using System; |
| | | 2 | | using System.Net; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using Jellyfin.Extensions.Json; |
| | | 6 | | |
| | | 7 | | namespace MediaBrowser.Providers.Plugins.Omdb |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converts a string <c>N/A</c> to <c>string.Empty</c> and decodes HTML entities in every other string. |
| | | 11 | | /// </summary> |
| | | 12 | | public class JsonOmdbNotAvailableStringConverter : JsonConverter<string?> |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 16 | | { |
| | 79 | 17 | | if (reader.IsNull()) |
| | | 18 | | { |
| | 0 | 19 | | return null; |
| | | 20 | | } |
| | | 21 | | |
| | 79 | 22 | | if (reader.TokenType == JsonTokenType.String) |
| | | 23 | | { |
| | | 24 | | // GetString can't return null here because we already handled it above |
| | 79 | 25 | | var str = reader.GetString()!; |
| | 79 | 26 | | if (str.Equals("N/A", StringComparison.OrdinalIgnoreCase)) |
| | | 27 | | { |
| | 6 | 28 | | return null; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | // Some OMDb records are HTML encoded, e.g. the cast of tt0093058 lists "Vincent D'Onofrio". |
| | | 32 | | // Stored verbatim that name is a second person next to the correctly spelled one. |
| | 73 | 33 | | return WebUtility.HtmlDecode(str); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return JsonSerializer.Deserialize<string?>(ref reader, options); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) |
| | | 41 | | { |
| | 20 | 42 | | writer.WriteStringValue(value); |
| | 20 | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |