2024-08-30 15:29:48 +02:00
|
|
|
using System;
|
2025-02-14 04:24:55 +01:00
|
|
|
using System.Collections.Generic;
|
2020-11-16 20:29:46 -07:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
2021-06-19 18:02:33 +02:00
|
|
|
namespace Jellyfin.Extensions.Json.Converters
|
2020-11-16 20:29:46 -07:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2025-02-14 04:24:55 +01:00
|
|
|
/// Json Pipe delimited collection converter factory.
|
2020-11-16 20:29:46 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This must be applied as an attribute, adding to the JsonConverter list causes stack overflow.
|
|
|
|
|
/// </remarks>
|
2025-02-14 04:24:55 +01:00
|
|
|
public class JsonPipeDelimitedCollectionConverterFactory : JsonConverterFactory
|
2020-11-16 20:29:46 -07:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool CanConvert(Type typeToConvert)
|
|
|
|
|
{
|
2025-02-14 04:24:55 +01:00
|
|
|
return typeToConvert.IsArray
|
|
|
|
|
|| (typeToConvert.IsGenericType
|
|
|
|
|
&& (typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyCollection<>)) || typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyList<>))));
|
2020-11-16 20:29:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-05-05 12:57:01 +02:00
|
|
|
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
2020-11-16 20:29:46 -07:00
|
|
|
{
|
|
|
|
|
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
|
2025-02-14 04:24:55 +01:00
|
|
|
return (JsonConverter?)Activator.CreateInstance(typeof(JsonPipeDelimitedCollectionConverter<>).MakeGenericType(structType));
|
2020-11-16 20:29:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|