Handle empty tags for videos and channels

This commit is contained in:
DarkFighterLuke
2024-03-14 15:38:50 +01:00
parent a2429c89ff
commit c1969a949c
3 changed files with 56 additions and 0 deletions
@@ -70,6 +70,7 @@ namespace Jellyfin.Plugin.TubeArchivistMetadata.TubeArchivist
/// Gets channel tags.
/// </summary>
[JsonProperty(PropertyName = "channel_tags")]
[JsonConverter(typeof(TagsJsonConverter))]
public Collection<string> Tags { get; }
/// <summary>
@@ -0,0 +1,54 @@
using System;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Jellyfin.Plugin.TubeArchivistMetadata.TubeArchivist
{
/// <summary>
/// Custom JsonConverter to handle tags list that could be null.
/// </summary>
public class TagsJsonConverter : JsonConverter
{
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Collection<string>);
}
/// <inheritdoc/>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
var tags = new Collection<string>();
if (token.Type == JTokenType.Array)
{
var stringArray = token.ToObject<string[]>();
if (stringArray != null)
{
foreach (var tag in stringArray)
{
tags.Add(tag);
}
}
}
return tags;
}
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var tags = (Collection<string>)(value ?? Array.Empty<string>());
writer.WriteStartArray();
foreach (var tag in tags)
{
writer.WriteValue(tag);
}
writer.WriteEndArray();
}
}
}
@@ -54,6 +54,7 @@ namespace Jellyfin.Plugin.TubeArchivistMetadata.TubeArchivist
/// Gets video tags.
/// </summary>
[JsonProperty(PropertyName = "tags")]
[JsonConverter(typeof(TagsJsonConverter))]
public Collection<string> Tags { get; }
/// <summary>