| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Converter for the <c>data</c> field in SD image responses. |
| | | 10 | | /// The Schedules Direct API may return a non-array value (e.g. a string error message) |
| | | 11 | | /// instead of the expected image data array for programs with errors. |
| | | 12 | | /// This converter returns an empty list for any non-array value. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class ImageDataArrayConverter : JsonConverter<IReadOnlyList<ImageDataDto>> |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public override IReadOnlyList<ImageDataDto> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOption |
| | | 18 | | { |
| | 1 | 19 | | if (reader.TokenType == JsonTokenType.StartArray) |
| | | 20 | | { |
| | 1 | 21 | | var result = new List<ImageDataDto>(); |
| | 5 | 22 | | while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) |
| | | 23 | | { |
| | 4 | 24 | | var item = JsonSerializer.Deserialize<ImageDataDto>(ref reader, options); |
| | 4 | 25 | | if (item is not null) |
| | | 26 | | { |
| | 4 | 27 | | result.Add(item); |
| | | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | 1 | 31 | | return result; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // Not an array (string error, null, object, etc.) — skip and return empty. |
| | 0 | 35 | | reader.TrySkip(); |
| | 0 | 36 | | return []; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public override void Write(Utf8JsonWriter writer, IReadOnlyList<ImageDataDto> value, JsonSerializerOptions options) |
| | 0 | 41 | | => JsonSerializer.Serialize(writer, value, options); |
| | | 42 | | } |