Files
Jellyfin_Xtream/Jellyfin.Xtream/Client/OnlyObjectConverter.cs
Kevin Jilissen 71eb01b682 Fix parsing episodes with missing info
Currently, parsing some of the seasons fails, as episodes are missing
information. In such case, they contain an empty array instead of an
object or null on the API endpoint. Prevent this problem by writing a
cuustom converter.
2024-05-15 19:56:42 +02:00

52 lines
1.6 KiB
C#

// Copyright (C) 2022 Kevin Jilissen
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Jellyfin.Xtream.Client;
/// <summary>
/// Class OnlyObjectConverter converts only object tokens.
/// </summary>
/// <typeparam name="T">The object which should be converted.</typeparam>
public class OnlyObjectConverter<T> : JsonConverter
{
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
/// <inheritdoc/>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Object)
{
return token.ToObject<T>();
}
return null;
}
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}